gpt4 book ai didi

sql - 按月排序 SQL 查询

转载 作者:行者123 更新时间:2023-12-04 22:36:29 25 4
gpt4 key购买 nike

我有一个 sql 查询:

select dateName(month, DateAccessed) "Month"
, count(1) totalVisits
, count(distinct l.userName) UsersVisit
from and where clause goes here
group by dateName(monthDateAccessed)
order by Month

我得到的输出是

Month   totalVisits   UsersVisit
April 100 25
February 200 35
July 300 45
March 400 55
May 500 65

但我想要的输出顺序是:

February      200       35
March 400 55
April 100 25
May 500 65
July 300 45

我怎样才能得到这个?

最佳答案

使用 month(DateAccessed)datepart(month, DateAccessed) 提取月份编号并在 order by 子句中使用它.但是,您还必须将其添加到 group by 子句中:

SELECT 
DATENAME(month, DateAccessed) "Month",
COUNT(1) totalVisits,
COUNT(DISTINCT l.userName) UsersVisit
FROM and where clause goes here
GROUP BY
MONTH(dateaccessed),
DATENAME(month, DateAccessed)
ORDER BY
MONTH(dateaccessed);

如果您的数据保存超过一年的数据,您应该在 group- 和 order by 子句(和 select 语句)中包含年份,如果您还没有确保在 where 子句中只获取一年的数据.

关于sql - 按月排序 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247921/

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