我有一个复杂的查询,提供如下输出。
Name Date Value1 Value2
D 1493803432 12 14
S 1493803432 20 22
T 1493803432 24 42
D 1493803424 20 15
S 1493803424 30 23
T 1493803424 65 42
D 1493804556 22 11
S 1493804556 45 23
T 1493804556 55 24
我想要的输出是将行添加为两行或更多行的总和。 和按日期分组字段,示例如下。
Name Date Value1 Value2
D 1493803432 12 14
S 1493803432 20 22
T 1493803432 24 42
TS 1493803432 44 64
D 1493803424 20 15
S 1493803424 30 23
T 1493803424 65 42
TS 1493803424 95 65
D 1493804556 22 11
S 1493804556 45 23
T 1493804556 55 24
TS 1493804556 100 57
我可以做联合但不能按日期分组。所以TS是T& S的总和,不包括D和个别日期字段。
答案 0 :(得分:1)
我想你可以试试这个:
SELECT NAME, DATE, VALUE1, VALUE2
FROM <your complex query>
UNION ALL
SELECT 'TS' AS NAME, DATE, SUM(VALUE1) AS VALUE1, SUM(VALUE2) AS VALUE2
FROM <your complex query>
WHERE NAME IN ('T','S')
GROUP BY DATE
ORDER BY DATE, NAME
答案 1 :(得分:0)
您可以使用如下查询:
onSubscribe
查询使用条件聚合,以便对每个组中仅 publish(Function)
,SELECT Name, Date, Value1, Value2, 1 AS grp
FROM mytable
UNION ALL
SELECT 'TS' AS Name, Date,
SUM(CASE WHEN Name IN ('T', 'S') THEN Value1 ELSE 0 END) AS Value1,
SUM(CASE WHEN Name IN ('T', 'S') THEN Value2 ELSE 0 END) AS Value2,
2 AS grp
FROM mytable
GROUP BY Date, grp
ORDER BY Date, grp
个记录求和。字段'T'
是'S'
子句使用的数字文字,用于实现所需的输出。
答案 2 :(得分:0)
尝试
SELECT name,
date_col,
TO_NUMBER (value1),
TO_NUMBER (value2)
FROM my_table2
UNION
SELECT 'TS' name,
(date_col),
SUM (CASE WHEN name IN ('T', 'S') THEN value1 ELSE '0' END) value1,
SUM (CASE WHEN name IN ('T', 'S') THEN value2 ELSE '0' END) value2
FROM my_table2
GROUP BY date_col
ORDER BY date_col, name