T-SQL:按年,月分类结果

时间:2011-07-10 17:50:09

标签: sql sql-server tsql sql-order-by

我有以下声明:

select
    (datename(MM, SomeDate) + ' ' + datename(day, SomeDate)) as D,
    count(SomeID) as IDs
from dbo.SomeTable
where
    datename(MM, SomeDate) = @Month
    and datename(YYYY, SomeDate) = @Year
group by
    datename(YYYY, SomeDate),
    datename(MM, SomeDate),
    datename(DD, SomeDate);

它可以工作,但它会对结果进行排序:

July 1, 2011 - 2
July 10, 2011 - 4
July 2, 2011 - 10

如何让它在7月2日之后显示7月10日?我知道这很简单,但我找不到问题。谢谢你的帮助!

编辑:伙计们,我知道必须有一个“顺序”,我认为这很明显。但“正常”排序会产生相同的结果(请参阅我的评论)。我认为这与整理有关,但我不是那里的专家。

5 个答案:

答案 0 :(得分:2)

通过将查询正文移动到子查询中来管理修复它,包括group by子句中的SomeDate(确保删除时间部分)。

我个人不会在此查询中使用DateName - 我宁愿使用DatePart并保留所有数字,但要根据您的查询:

设定:

create table SomeTable (
    SomeDate datetime not null,
    SomeID int not null
)
go
insert into SomeTable (SomeDate,SomeID)
select '20110701',1 union all
select '20110702',1 union all
select '20110710',1

查询:

declare @Month varchar(10)
declare @Year int
select @Month='July',@Year=2011

select D,IDs from (
select
    (datename(MM, SomeDate) + ' ' + datename(day, SomeDate)) as D,
    count(SomeID) as IDs
    ,DATEADD(day,DATEDIFF(day,0,SomeDate),0) as SomeDate
from dbo.SomeTable
where
    datename(MM, SomeDate) = @Month
    and datename(YYYY, SomeDate) = @Year
group by
    datename(YYYY, SomeDate),
    datename(MM, SomeDate),
    datename(DD, SomeDate),
    DATEADD(day,DATEDIFF(day,0,SomeDate),0)
) t
order by SomeDate

答案 1 :(得分:1)

如果SomeDate的类型为DATE,那么您只需添加ORDER BY SomeDate即可。 GROUP BY也可以简化:

select
    (datename(MM, SomeDate) + ' ' + datename(day, SomeDate)) as D,
    count(SomeID) as IDs
from dbo.SomeTable
where
    datename(MM, SomeDate) = @Month
    and datename(YYYY, SomeDate) = @Year
group by
    SomeDate 
order by
    SomeDate ;

WHERE也可以简化。


如果是DATETIME类型,您可以使用:

select
    (datename(MM, DATEADD(dd, 0, DATEDIFF(dd, 0, SomeDate)))
      + ' ' + datename(day, DATEADD(dd, 0, DATEDIFF(dd, 0, SomeDate)))) as D,
    count(SomeID) as IDs
from dbo.SomeTable
where
    datename(MM, SomeDate) = @Month
    and datename(YYYY, SomeDate) = @Year
group by
    DATEADD(dd, 0, DATEDIFF(dd, 0, SomeDate)) 
order by
    DATEADD(dd, 0, DATEDIFF(dd, 0, SomeDate)) ;

答案 2 :(得分:1)

您丢失了来自somedate 的信息,无论如何它都不适用于ORDER BY

DATENAME排序不是你想要的,因为七月是八月之后

因此,将更多派生列添加到GROUP BY以便稍后使用DATEPART进行排序,该DATEPART提供数字

select
    (datename(MM, SomeDate) + ' ' + datename(day, SomeDate)) as D,
    count(SomeID) as IDs
from
    dbo.SomeTable
where
    datename(MM, SomeDate) = @Month
    and datename(YYYY, SomeDate) = @Year
group by
    datename(YYYY, SomeDate),
    datename(MM, SomeDate),
    datename(DD, SomeDate),
    datepart(YYYY, SomeDate),
    datepart(MM, SomeDate),
    datepart(DD, SomeDate)
order by
    datepart(YYYY, SomeDate),
    datepart(MM, SomeDate),
    datepart(DD, SomeDate)

答案 3 :(得分:0)

您需要添加ORDER BY子句并按SomeDate排序。

答案 4 :(得分:0)

SELECT COUNT(*)
   /*Event_type
   EventDate*/ AS EventCount
  ,MONTH(EventDate) AS MONTH
  ,YEAR(EventDate) AS YEAR
  ,DATENAME(MONTH ,EventDate) AS MonthName
FROM   EventTable
WHERE  (Event_type = 'TypeOfEvent')
   AND (Userid NOT LIKE '%*%')
GROUP BY
   YEAR(EventDate)
  ,MONTH(EventDate)
  ,DATENAME(MONTH ,EventDate)
ORDER BY
   YEAR
  ,MONTH