gpt4 book ai didi

sql-server - MS-SQL 降序排序输出

转载 作者:行者123 更新时间:2023-12-03 22:48:37 33 4
gpt4 key购买 nike

我有这个 MS-SQL 查询,数据库中有数千行记录:

SELECT DISTINCT TOP 7 DATENAME(MM, mydatetime) + ' ' + CAST(DAY(mydatetime) AS VARCHAR(2)) as thedate
, MONTH(mydatetime)
, DAY(mydatetime)
, COUNT(Page) as totalcount
, count(DISTINCT Page) as visitors
FROM someTable
WHERE Page LIKE '%AEC%'
GROUP BY DATENAME(MM, mydatetime) + ' ' + CAST(DAY(mydatetime) AS VARCHAR(2))
, MONTH(mydatetime)
, DAY(mydatetime)
ORDER BY MONTH(mydatetime) DESC
, DAY(mydatetime) DESC

它会输出这样的:

thedate     | totalcount | visitors
-----------------------------------
October 17 | 4 | 1
October 15 | 1 | 1
October 12 | 1 | 1
October 3 | 3 | 3
October 2 | 42 | 22
September 28| 2 | 1
September 21| 14 | 10

我的问题是我无法按降序输出它,所以它看起来像这样:

thedate     | totalcount | visitors
-----------------------------------
September 21| 14 | 10
September 28| 2 | 1
October 2 | 42 | 22
October 3 | 3 | 3
October 12 | 1 | 1
October 15 | 1 | 1
October 17 | 4 | 1

非常感谢任何帮助。

最佳答案

根据您的实际需求,您可以将当前查询用作派生表并按照您想要的方式排序:

SELECT *
FROM ( SELECT DISTINCT TOP 7 DATENAME(mm, mydatetime) + ' '
+ CAST(DAY(mydatetime) AS VARCHAR(2)) AS thedate,
MONTH(mydatetime) AS theMonth,
DAY(mydatetime) AS theDay,
COUNT(page) AS totalcount,
COUNT(DISTINCT page) AS visitors
FROM sometable
WHERE page LIKE '%AEC%'
GROUP BY DATENAME(mm, mydatetime) + ' '
+ CAST(DAY(mydatetime) AS VARCHAR(2)),
MONTH(mydatetime),
DAY(mydatetime)
ORDER BY MONTH(mydatetime) DESC,
DAY(mydatetime) DESC) A
ORDER BY theMonth, theDay

关于sql-server - MS-SQL 降序排序输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961444/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com