U-SQL - CASE 语句的 SUM
U-SQL - SUM of a CASE Statement
我有一个 table 客户交易,我试图按客户和部门汇总。
Cust_id trans_num sku dept qty price
123 234 345 1 2 15.99
123 345 887 1 1 12.99
123 678 445 2 1 21.89
234 345 998 1 1 7.99
在 SQL 我会做这样的事情:
SELECT Cust_id
, SUM(CASE WHEN dept = 1 THEN (price * qty) ELSE 0 END ) dept_1_spend
, SUM(CASE WHEN dept = 2 THEN (price * qty) ELSE 0 END ) dept_2_spend
from tab1
group by Cust_id
U-SQL 文档 here 提到 ?
作为 C#
等价物,但我不确定如何 SUM
值。
U-SQL 中的等价物是什么?
您可以在 C# 中尝试三元运算符:
SELECT Cust_id
, SUM(dept == 1 ? price * qty : 0) AS dept_1_spend
, SUM(dept == 2 ? price * qty : 0) AS dept_2_spend
from tab1
group by Cust_id
您甚至可以使用 SQL 的 CASE
表达式。您将需要 C# ==
并使用 AS
来指定列别名并为关键字使用大写字母。但在其他方面看起来像您的查询:
@tab1 =
SELECT *
FROM(
VALUES
(123,234,345,1,2,15.99),
(123,345,887,1,1,12.99),
(123,678,445,2,1,21.89),
(234,345,998,1,1,7.99)) AS T(Cust_id,trans_num,sku,dept,qty,price);
@res =
SELECT Cust_id,
SUM(CASE WHEN dept == 1 THEN(price * qty) ELSE 0 END) AS dept_1_spend,
SUM(CASE WHEN dept == 2 THEN(price * qty) ELSE 0 END) AS dept_2_spend
FROM @tab1
GROUP BY Cust_id;
OUTPUT @res
TO "/output/sum_case.csv"
USING Outputters.Csv();
我个人更喜欢 C# 三元 if。
您也可以使用 U-SQL PIVOT
运算符,例如
@tab1 =
SELECT *
FROM(
VALUES
(123,234,345,1,2,15.99),
(123,345,887,1,1,12.99),
(123,678,445,2,1,21.89),
(234,345,998,1,1,7.99)) AS T(Cust_id,trans_num,sku,dept,qty,price);
@res =
SELECT Cust_id,
SUM([1]) AS dept_1_spend,
SUM([2]) AS dept_2_spend
FROM
(
SELECT Cust_id, dept, price * qty AS spend
FROM @tab1
) AS t
PIVOT (SUM(spend) FOR dept IN ( 1 AS [1], 2 AS [2] )
) AS pvt
GROUP BY Cust_id;
OUTPUT @res
TO "/output/sum_case.csv"
USING Outputters.Csv();
有关 U-SQL PIVOT
的更多信息可用 here。
我有一个 table 客户交易,我试图按客户和部门汇总。
Cust_id trans_num sku dept qty price
123 234 345 1 2 15.99
123 345 887 1 1 12.99
123 678 445 2 1 21.89
234 345 998 1 1 7.99
在 SQL 我会做这样的事情:
SELECT Cust_id
, SUM(CASE WHEN dept = 1 THEN (price * qty) ELSE 0 END ) dept_1_spend
, SUM(CASE WHEN dept = 2 THEN (price * qty) ELSE 0 END ) dept_2_spend
from tab1
group by Cust_id
U-SQL 文档 here 提到 ?
作为 C#
等价物,但我不确定如何 SUM
值。
U-SQL 中的等价物是什么?
您可以在 C# 中尝试三元运算符:
SELECT Cust_id
, SUM(dept == 1 ? price * qty : 0) AS dept_1_spend
, SUM(dept == 2 ? price * qty : 0) AS dept_2_spend
from tab1
group by Cust_id
您甚至可以使用 SQL 的 CASE
表达式。您将需要 C# ==
并使用 AS
来指定列别名并为关键字使用大写字母。但在其他方面看起来像您的查询:
@tab1 =
SELECT *
FROM(
VALUES
(123,234,345,1,2,15.99),
(123,345,887,1,1,12.99),
(123,678,445,2,1,21.89),
(234,345,998,1,1,7.99)) AS T(Cust_id,trans_num,sku,dept,qty,price);
@res =
SELECT Cust_id,
SUM(CASE WHEN dept == 1 THEN(price * qty) ELSE 0 END) AS dept_1_spend,
SUM(CASE WHEN dept == 2 THEN(price * qty) ELSE 0 END) AS dept_2_spend
FROM @tab1
GROUP BY Cust_id;
OUTPUT @res
TO "/output/sum_case.csv"
USING Outputters.Csv();
我个人更喜欢 C# 三元 if。
您也可以使用 U-SQL PIVOT
运算符,例如
@tab1 =
SELECT *
FROM(
VALUES
(123,234,345,1,2,15.99),
(123,345,887,1,1,12.99),
(123,678,445,2,1,21.89),
(234,345,998,1,1,7.99)) AS T(Cust_id,trans_num,sku,dept,qty,price);
@res =
SELECT Cust_id,
SUM([1]) AS dept_1_spend,
SUM([2]) AS dept_2_spend
FROM
(
SELECT Cust_id, dept, price * qty AS spend
FROM @tab1
) AS t
PIVOT (SUM(spend) FOR dept IN ( 1 AS [1], 2 AS [2] )
) AS pvt
GROUP BY Cust_id;
OUTPUT @res
TO "/output/sum_case.csv"
USING Outputters.Csv();
有关 U-SQL PIVOT
的更多信息可用 here。