I have this query which pulls the data correctly but I’d like the timestamp field (TIM) to be in another format than Day(TIM). But I get errors because the query is aggregating that way.
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)
Result:
Day G.TRU G.LOA G.SEA R.SEA TOTAL
-----------------------------------------------
25 2 4 14 1 21
26 3 0 2 9 14
-----------------------------------------------
I’d prefer the 25 and 26 to be in a YYYY-MM-DD format. Is there a way?
No – you’re grouping by DAY(TIM) so there could be various different months and years giving the same day. For example, suppose you had two rows, one on the first of January and one on the first of February – they will be aggregated together, so which one would you expect to retrieve for that result row?
EDIT: Okay, I’m not a SQL guy, but I think you want something like: