我在下面有以下查询。如何获得第1列和第3列的总和?和'第2列'? X和Y只是样本数据,使问题更容易理解。
int randomNumber;
randomNumber = new Random().Next(1, n + 1);
switch(randomNumber)
{
case 1: ShowPage1();
break;
case 2: ShowPage2();
break;
case 3: ShowPage3();
break;
...
case n: ShowPageN();
break;
}
答案 0 :(得分:0)
如果您希望第1列+第2列的结果用于table1中的每一行,您可以使用此查询:
SELECT [Column 1] + [Column 2]
FROM (
SELECT
CASE
WHEN x = 0 then 2
WHEN x = 1 then 4
ELSE 6
END AS [Column 1]
, CASE
WHEN y = 0 THEN 3
WHEN y = 1 THEN 6
ELSE 9
END AS [Column 2]
FROM table1
) AS IntermediateResult
如果您想要第1列中所有值的总和以及第2列中的所有值,则可以使用第1列和第2列。
SELECT SUM([Column 1] + [Column 2])
FROM (
SELECT
CASE
WHEN x = 0 then 2
WHEN x = 1 then 4
ELSE 6
END AS [Column 1]
, CASE
WHEN y = 0 THEN 3
WHEN y = 1 THEN 6
ELSE 9
END AS [Column 2]
FROM table1
) AS IntermediateResult
答案 1 :(得分:0)
尝试:
SELECT
SUM
(
CASE x
WHEN 0 THEN 2
WHEN 1 THEN 4
ELSE 6
END
) AS [Column 1],
SUM
(
CASE y
WHEN 0 THEN 3
WHEN 1 THEN 6
ELSE 9
END
) AS [Column 2]
FROM
table1
SUM 两列
SELECT
(
CASE x
WHEN 0 THEN 2
WHEN 1 THEN 4
ELSE 6
END
)
+
(
CASE y
WHEN 0 THEN 3
WHEN 1 THEN 6
ELSE 9
END
) AS [Column 1]
FROM
table1