gpt4 book ai didi

SQL 按月和年分组

转载 作者:行者123 更新时间:2023-12-01 17:33:04 26 4
gpt4 key购买 nike

我不确定应该在以下 SQL 查询中编写什么来显示“日期”列,如下所示:“月-年”-“9-2011”。

SELECT MONTH(date) + '.' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY MONTH(date), YEAR(date)

所以,我想要做的是将第一列中的数据更改为显示月份和年份,而不是仅显示月份。

最佳答案

SELECT CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4)) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4))

或者正如 @40-Love 提到的,你可以使用前导零进行转换:

GROUP BY 
CAST(YEAR(date) AS VARCHAR(4)) + '-' + right('00' + CAST(MONTH(date) AS VARCHAR(2)), 2)

关于SQL 按月和年分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5406170/

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