如何才能获得从最新到最旧的日期

时间:2018-04-16 16:21:51

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

第一次使用这样的东西寻求帮助,所以我不确定我是否正确地这样做但是这里...

我正在努力让我的日期从最近的到最早的。 当我按[Day] Desc或Asc尝试Order时,它无法正确显示。

这是现在显示的链接,因为它现在是

https://imgur.com/a/9P9hz

任何人都可以帮忙吗?

DECLARE @StartDate DATETIME = '2018/03/13'
DECLARE @NumDays INT = 11


DECLARE @cols AS NVARCHAR(MAX),
             @query  AS NVARCHAR(MAX),
             @query1  AS NVARCHAR(MAX),
             @query2  AS NVARCHAR(MAX),
             @query3 AS NVARCHAR(MAX),
             @query4 AS NVARCHAR(MAX),
             @query5 AS NVARCHAR(MAX) 



;with cte as (          
    SELECT 
    CAST(DAY(SentTime) AS VARCHAR(MAX)) [Day]
                 --,CAST((DATENAME(DAY, SentTime)) AS INT) as SentDay 
                 ,isnull(count(id),0) 'Total'
                 ,Sum(Case when isnull(IsDeleted,'') <> '' Then 1 else 0 end) Deleted
                 ,Sum(Case when isnull(CallLogTime,'') = '' and isnull(IsDeleted,'')=''  Then 1 else 0 end) 'Not Logged'
                ,Sum(Case when isnull(SentTime,'') = '' and isnull(IsDeleted,'')=''  Then 1 else 0 end) 'Received'
                 ,Sum(Case when isnull(CallLogTime,'') <> '' and isnull(IsDeleted,'')=''  Then 1 else 0 end) Logged

    FROM tblMailItems
    WHERE DATEDIFF(dd, SentTime, GETDATE()) <= @NumDays
    group by DAY(SentTime) --,CAST((DATENAME(DAY, SentTime)) AS INT) 

),
cte2 as (
    select * from cte
)

select * 
into #X
from cte
union
select 'Row Total', sum(Total), sum(Deleted), sum([not logged]), sum(Received), sum(Logged) from cte2

--drop table #X

select @cols = STUFF((SELECT ',' + QUOTENAME([Day]) 
                    from #X
                    group by DAY
                    order by DAY asc

                                   FOR XML PATH('')
            ), 1, 1, '')

                    --select * from #x 

set @query1 = 'select 1 Sequence, ''Total Logged'' [Description], ' + @cols + '
from 
(
  select [day], total
  from #X
) src
pivot
(
  sum(total)
  for Day in (' + @Cols + ')
) p'



set @query2 = ' UNION select 2 Sequence, ''Deleted'' [Description], ' + @cols + '

from 
(
  select [day], Deleted
  from #X
) src
pivot
(
  sum(Deleted)
  for Day in (' + @Cols + ')
) p'


Select  @query = @query1 + @query2



set @query3 = ' UNION select 3 Sequence, ''Not Logged'' [Description], ' + @cols + ' 
from 
(
  select [day], [Not Logged]
  from #X
) src
pivot
(
  sum([Not Logged])
  for Day in (' + @Cols + ')
) p'

select  @query = @query1 + @query2 + @query3


set @query4 = ' union select 4 sequence, ''Received'' [description], ' + @cols + ' 
from 
(
  select [day], [Received]
  from #x
) src
pivot
(
  sum([Received])
  for day in (' + @cols + ')
) p'

select  @query = @query1 + @query2 + @query3 + @query4


set @query5 = ' UNION select 5 Sequence, ''Logged'' [Description], ' + @cols + ' 
from 
(
  select [day], [Logged]
  from #X
) src
pivot
(
  sum([Logged])
  for Day in (' + @Cols + ')
) p'

select  @query = @query1 + @query2 + @query3 + @query5

execute  (@query)
drop table #x

2 个答案:

答案 0 :(得分:0)

您的ORDER BY列[Day]被强制转换为VARCHAR(MAX)。如果日期和时间是DATETIME格式,则可以最有效地订购日期和时间。

答案 1 :(得分:0)

如评论和答案中所述,在date(time)列上订购效率更高。我认为你的问题是,排序发生在别名DAY上,而不是列DAY。尝试通过别名指定列:

SELECT ',' + QUOTENAME([Day]) 
from #X as x 
group by X.DAY 
order by x.DAY asc