The question is referring to a T-SQL query.
问题是指T-SQL查询。
There are two tables:
有两个表:
Table_A:
A_ID | Category | SomeValue .
-----+----------+-----------.
0 | 1 | ... .
1 | 1 | ... .
2 | 2 | ... .
2 | 2 | ... .
and Table_B that should get filled by the Category condition of Table_A. Depending on the categories of Table_A I would like to write some data into column 1 OR column 2 of a second table. The other column shall remain null:
和Table_B应该由Table_A的Category条件填充。根据Table_A的类别,我想将一些数据写入第二列的第1列或第2列。另一列应保持为null:
B_ID | cat_x | cat_y .
-----+----------+-----------.
0 | someval | null . //Category was 1
1 | null | someval . //Category was 2
2 | null | someval . //Category was 2
2 | someval | null . //Category was 1
My input is a whole table. For every row it has to look up the goal column to write in.
我的输入是整张表。对于每一行,它必须查找目标列才能写入。
I tried to work with CASE, but I didn't got it. Here is some pseudo code that might help you to understand:
我尝试使用CASE,但我没有得到它。以下是一些可能有助于您理解的伪代码:
CASE
WHEN Category=1
THEN
INSERT INTO (B_ID,cat_x)
SELECT (someval) //ID could get filled automatically
FROM sometable
WHEN Category = 2
THEN
INSERT INTO (B_ID,cat_y)
SELECT (someval) //ID could get filled automatically
FROM sometable
END
My biggest problem I guess is how to write the syntax for the condition/when-clause.
我猜的最大问题是如何编写condition / when-clause的语法。
1 个解决方案
#1
1
Not quite. The case
expression should be part of the `select query:
不完全的。 case表达式应该是`select查询的一部分:
INSERT INTO (B_ID, cat_x, cat_y)
SELECT (case when a.category = 1 then someval end),
(case when a.category = 2 then someval end)
FROM A
WHERE category IN (1, 2);
#1
1
Not quite. The case
expression should be part of the `select query:
不完全的。 case表达式应该是`select查询的一部分:
INSERT INTO (B_ID, cat_x, cat_y)
SELECT (case when a.category = 1 then someval end),
(case when a.category = 2 then someval end)
FROM A
WHERE category IN (1, 2);