我有这个查询正确地提取数据但我希望时间戳字段(TIM)采用另一种格式而不是Day(TIM)。但是我得到错误,因为查询是以这种方式聚合的。
SELECT
DAY(TIM) As 'Day',
[G.TRU] = SUM(case when SEC = 'TRU' and TYP = 'GEO' then 1 else 0 end),
[G.LOA] = SUM(case when SEC = 'LOA' and TYP = 'GEO' then 1 else 0 end),
[G.SEA] = SUM(case when SEC = 'SEA' and TYP = 'GEO' then 1 else 0 end),
[R.SEA] = SUM(case when SEC = 'SEA' and TYP = 'RTE' then 1 else 0 end),
TOTAL = COUNT(TIM)
FROM AGEO
WHERE SRC = 'EW'
GROUP BY DAY(TIM)
结果:
Day G.TRU G.LOA G.SEA R.SEA TOTAL
-----------------------------------------------
25 2 4 14 1 21
26 3 0 2 9 14
-----------------------------------------------
我更喜欢25和26采用YYYY-MM-DD格式。有办法吗?
答案 0 :(得分:1)
不 - 你是按照DAY(TIM)分组所以可能有不同的月份和年份给出同一天。例如,假设您有两行,一个在1月的第一行,一个在2月的第一行 - 它们将被聚合在一起,那么您希望为该结果行检索哪一行?
编辑:好的,我不是一个SQL人,但我想你想要的东西:
SELECT
CONVERT(Date, TIM) As TIMDATE,
[G.TRU] = SUM(case when SEC = 'TRU' and TYP = 'GEO' then 1 else 0 end),
[G.LOA] = SUM(case when SEC = 'LOA' and TYP = 'GEO' then 1 else 0 end),
[G.SEA] = SUM(case when SEC = 'SEA' and TYP = 'GEO' then 1 else 0 end),
[R.SEA] = SUM(case when SEC = 'SEA' and TYP = 'RTE' then 1 else 0 end),
TOTAL = COUNT(TIM)
FROM AGEO
WHERE SRC = 'EW'
GROUP BY TIMDATE