简体   繁体   English

SQL查询汇总日期的格式日期

[英]Format date for SQL query aggregating date

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). 我有此查询可正确提取数据,但我希望时间戳字段(TIM)的格式不是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. 我希望25和26采用YYYY-MM-DD格式。 Is there a way? 有办法吗?

No - you're grouping by DAY(TIM) so there could be various different months and years giving the same day. 不-您要按 DAY(TIM) 分组 ,因此同一天可能会有不同的月份和年份。 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? 例如,假设您有两行,一排在1月1日,一排在2月1日-它们将汇总在一起,那么您希望为该结果行检索哪一行?

EDIT: Okay, I'm not a SQL guy, but I think you want something like: 编辑:好的,我不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM