我正在运行查询,想查看共享相同值的行的总和。
select ca.createddate
from sss s
join ttt t on t.sssId = s.id
join ccc c on c.tttId = t.id
join ccc_assoc ca on ca.refid = c.id
where s.name = 'xyz'
查询结果示例:
Created_Date
11/29/2018 1:27:58 AM
11/29/2018 1:27:58 AM
11/29/2018 1:27:58 AM
11/29/2018 1:27:58 AM
11/29/2018 1:27:59 AM
11/29/2018 1:27:59 AM
11/29/2018 1:27:59 AM
我想看的东西
Created_Date | Count
11/29/2018 1:27:58 AM | 4
11/29/2018 1:27:59 AM | 3
我曾经尝试过使用COUNT和GROUP BY,但是我什至不确定自己是否在使用这些功能的合适位置。任何帮助表示赞赏!
答案 0 :(得分:2)
您尝试过类似的东西吗?
SELECT ca.createddate AS Created_Date
COUNT(*) AS Count
FROM sss s
JOIN ttt t ON t.sssId = s.id
JOIN ccc c ON c.tttId = t.id
JOIN ccc_assoc ca On ca.refid = c.id
WHERE s.name = 'xyz'
GROUP BY ca.createddate
答案 1 :(得分:1)
一种新颖的方法是使用CTE生成日期列表,然后汇总CTE。您可以这样尝试:
;WITH Dates AS
(
select ca.createddate
from sss s
join ttt t on t.sssId = s.id
join ccc c on c.tttId = t.id
join ccc_assoc ca on ca.refid = c.id
where s.name = 'xyz'
)
SELECT
createddate,
COUNT(*) AS CountOfDates
GROUP BY createddate
编辑:不过,这实际上并不是最好的方法。它更像是一种“快速而肮脏的”方法。理想情况下,您将跟踪要重复的行,然后按单例键进行分组
答案 2 :(得分:1)
问题是您看到的日期实际上不是那里的日期-可能是毫秒。
如果要精确到一秒,则可以使用日期算术或转换为字符串来删除较小的时间单位。试试这个:
select dateadd(second, datediff(second, 0, ca.createddate), 0) as ts,
count(*)
from sss s join
ttt t on t.sssId = s.id join
ccc c on c.tttId = t.id join
ccc_assoc ca on ca.refid = c.id
where s.name = 'xyz'
group by dateadd(second, datediff(second, 0, ca.createddate), 0)
order by ts;