简体   繁体   English

在使用格式将SQL日期减少到MMM-yyyy之后,如何按月订购

[英]After reducing an SQL date down to MMM-yyyy using format how can I then order by Month

I am trying to group some aggregate numbers into months. 我正在尝试将一些汇总数字分为几个月。 The values stored are full datetime. 存储的值是完整的日期时间。 I have successfully done this using the below statements but I can no longer order by date (instead it uses August as the first in the list as it begins with A). 我已经使用下面的语句成功完成了此操作,但是我无法再按日期排序(相反,因为它以A开头,所以它使用8月作为列表中的第一个)。 My question is how can I now make the field RevDate either a Date field, or at least order correctly the months? 我的问题是,现在如何使字段RevDate成为日期字段,或者至少正确订购月份?

 SELECT
   tbl1.AName
  , tbl1.AStore
  , tbl1.AVr
  ,Count ( tbl2.RevTag1) as Tag1
  ,Count ( tbl2.RevTag2) as Tag2
  ,Count ( tbl2.RevTag3) as Tag3
  ,FORMAT ( tbl2.RevDateTime,'MMM-yyyy') As 'RevDate'

FROM 
   tbl2
  INNER JOIN  tbl1
    ON  tbl2.AppID =  tbl1.AppID
    Group by  tbl1.AName, tbl1.AStore, tbl1.AVr, FORMAT ( tbl2.RevDateTime,'MMM-yyyy')
    Order by FORMAT ( tbl2.RevDateTime,'MMM-yyyy') asc

thanks in advance 提前致谢

Try return also the month or year month. 尝试还返回月份或年份月份。

SELECT MONTH(create_date) as name, FORMAT ( create_date,'MMM-yyyy') As 'RevDate'

FROM table order by 1 从表中按1排序

or 要么

SELECT FORMAT(CREATE_DATE, 'yyyyMM') as name, FORMAT ( create_date,'MMM-yyyy') As 'RevDate'
FROM table 
order by 1

obs: You have a group by, so put a new column in group by with the same format in select clause. obs:您有一个group by,因此在group by中放入一个新列,并在select子句中使用相同的格式。

Sorry to answer again..:) still i just wanted get your result with out adding additional columns in the group by clause. 很抱歉再次回答..::)我仍然只想在group by子句中添加其他列来得到您的结果。

I have tried with the below query and its works for me.. You can consider it if needed. 我已经尝试过以下查询及其对我的工作。如果需要,您可以考虑一下。 :) :)

SELECT
   tbl1.AName
  , tbl1.AStore
  , tbl1.AVr
  ,Count ( tbl2.RevTag1) as Tag1
  ,Count ( tbl2.RevTag2) as Tag2
  ,Count ( tbl2.RevTag3) as Tag3
  ,FORMAT ( tbl2.RevDateTime,'MMM-yyyy') As 'RevDate'
FROM 
   tbl2
  INNER JOIN  tbl1
    ON  tbl2.AppID =  tbl1.AppID
    Group by  tbl1.AName, tbl1.AStore, tbl1.AVr, FORMAT ( tbl2.RevDateTime,'MMM-yyyy')
    order by  MONTH('1-'+FORMAT ( tbl2.RevDateTime,'MMM-yyyy')) asc , YEAR('1-'+FORMAT ( tbl2.RevDateTime,'MMM-yyyy')) asc

So after getting this working ish, I was still having issues that if it was run over more than 12 months the months would line up so Jan15 and Jan16 next to each other (at least outside of SSMS). 因此,在获得成功后,我仍然遇到问题,如果运行时间超过12个月,则几个月的时间会对齐,因此Jan15和Jan16会彼此相邻(至少在SSMS之外)。 Then a colleague found this: http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx Which lead me to the following result: 然后一位同事发现了这一点: http : //weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx,这使我得出以下结果:

 SELECT
   tbl1.AName
  , tbl1.AStore
  , tbl1.AVr
  ,Count ( tbl2.RevTag1) as Tag1
  ,Count ( tbl2.RevTag2) as Tag2
  ,Count ( tbl2.RevTag3) as Tag3
  ,dateadd(month, datediff(month, 0, Tbl2.RevDateTime),0) as Revdate

FROM 
   tbl2
  INNER JOIN  tbl1
    ON  tbl2.AppID =  tbl1.AppID
    Group by  tbl1.AName, tbl1.AStore, tbl1.AVr, dateadd(month, datediff(month, 0, Tbl2.RevDateTime),0) 
    Order by dateadd(month, datediff(month, 0, Tbl2.RevDateTime),0) asc

This worked great, both in SSMS and in tables outside of SSMS (SSRS, Excel etc), due to retaining the date format so this is now the best solution to my particular problem. 由于保留了日期格式,因此在SSMS和SSMS以外的表(SSRS,Excel等)中都很好用,因此现在这是解决我的特定问题的最佳解决方案。

Thanks for all you answers and thanks to Jeffs blog 感谢您的所有回答,并感谢Jeffs博客

To aggregate by month, just use datediff(month,0,<date>) . 要按月汇总,只需使用datediff(month,0,<date>)

you can even convert it back to date using dateadd(month,<value>,0) . 您甚至可以使用dateadd(month,<value>,0)将其转换回日期。

 SELECT tbl1.AName
       ,tbl1.AStore
       ,tbl1.AVr
       ,Count ( tbl2.RevTag1) as Tag1
       ,Count ( tbl2.RevTag2) as Tag2
       ,Count ( tbl2.RevTag3) as Tag3
       ,FORMAT ( dateadd(month,datediff(month,0,tbl2.RevDateTime),0),'MMM-yyyy') As 'RevDate'    
 FROM tbl2
    INNER JOIN  tbl1 ON  tbl2.AppID =  tbl1.AppID
 Group by  tbl1.AName, tbl1.AStore, tbl1.AVr, datediff(month,0,tbl2.RevDateTime)
 Order by datediff(month,0,tbl2.RevDateTime) asc

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

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