我有一个问题,对于学校我们继续研究另一个小组上学期开始的项目。 第一个表:testresult4values
SELECT sec_user.accountid, MIN(sec_user.firstname) as firstname, MIN(sec_user.lastname) as lastname, convert(date, event.starttime) as startdate, sum(cast(value as int)) as value , sum(cast(value2 as int)) as value2, value3, value4
FROM testresult4values as result
JOIN event
ON result.eventid = event.eventid
JOIN sec_user
ON result.userid = sec_user.accountid
group by value3, value4, sec_user.accountid, starttime
我得到以下输出:
正如你在第8,9和10行所看到的那样,当我只想要2时,有一个32的人有3个输出。我知道这是为什么,这是因为他在同一天参与了2个事件。由于连接错误而发生这种情况,我们无能为力。优先级取决于应用程序的其他部分。我想要的是将这两行与相同的日期结合起来,结果是:
8 | 32 |名字|姓氏| 2017-12-13 | 16 | 22
9 | 32 |名字|姓氏| 2018-03-09 | 18 | 30
提前致谢!
答案 0 :(得分:2)
删除时间后,您必须GROUP BY
日期。在您的分组中交换starttime
:
GROUP BY convert(date, event.starttime)
现在,由于您的选择,您看到删除时间的日期,但由于分组中正在考虑时间,因此这些行没有滚动。
答案 1 :(得分:1)
GROUP By
startdate不是starttime,你应该得到预期的结果。
SELECT sec_user.accountid, MIN(sec_user.firstname) as firstname, MIN(sec_user.lastname) as lastname, convert(date, event.starttime) as startdate, sum(cast(value as int)) as value , sum(cast(value2 as int)) as value2, value3, value4
FROM testresult4values as result
JOIN event
ON result.eventid = event.eventid
JOIN sec_user
ON result.userid = sec_user.accountid
group by value3, value4, sec_user.accountid, convert(date, event.starttime)