我有以下数据集,即日期时间,坦克和重量。 我想为每个罐子和每天添加一个重量小计(根据日期时间对罐子进行分类。
因此,期望的结果将在小计的每个部分的末尾有一行,这将给出第1天的总数,然后是第2天的总数
datum tk gewicht
20/01/2018 9:00 TK1 1000
20/01/2018 10:00 TK1 2000
20/01/2018 4:00 TK10 3000
20/01/2018 8:00 TK2 4000
21/01/2018 6:00 TK3 5000
21/01/2018 7:00 TK5 6000
到目前为止,我的查询得出以下结果:
select cast(datum as datetime) as date, tk, sum(gewicht) as weight
from tblReclassificatie
group by rollup(tk,cast(datum as datetime))
date tk weight
20/01/2018 9:00 TK1 1000
20/01/2018 10:00 TK1 2000
NULL TK1 3000
20/01/2018 4:00 TK10 3000
NULL TK10 3000
20/01/2018 8:00 TK2 4000
NULL TK2 4000
21/01/2018 6:00 TK3 5000
NULL TK3 5000
21/01/2018 7:00 TK5 6000
NULL TK5 6000
NULL NULL 21000
但顺序错了 - 首先是tk10然后是tk2而不是tk1作为第一个
答案 0 :(得分:0)
This query solves my problem (with new id field):
select [Date], tk, weegbon, gewicht, weeklotnr, id from (select [Date], tk, weegbon, sum(gewicht) as gewicht, min(datum) as Datum, min(weeklotnr) as weeklotnr, min(id) as id from (select cast(datum as date) as [Date], datum, tk, weegbon, gewicht, weeklotnr, id from tblReclassificatie )a group by [Date], tk , weegbon with rollup)aa order by Datum, [Date],tk, weegbon