回答

收藏

将多个查询转换为一个

技术问答 技术问答 227 人阅读 | 0 人回复 | 2023-09-12

我想显示以下内容:- n* m2 Z- x! n
month no.     1month          Januarytotal income   $xx一站式查询1 P. ~" E: ^: ^* }
目前,我正在使用以下代码来显示我想要的输出。但是,我想知道是否可以在查询中编码以显示所需的输出?
5 F- t& m4 `/ _目前使用的代码:( x0 D% t5 d5 |8 y* M4 o
DECLARE @january int = 0SELECT Month(Transaction_Date) AS Month_Number,DATENAME(MONTH,DATEADD(MONTH,@january,0)) AS 'Month',CONCAT('$',SUM(Credit_Amount)) AS Total_IncomeFROM Income WHERE Transaction_Date BETWEEN '2020-01-01' AND '2020-01-31'GROUP BY MONTH(Transaction_Date)DECLARE @february int = 1SELECT Month(Transaction_Date) AS Month_Number,DATENAME(MONTH,DATEADD(MONTH,@february,0)) AS 'Month',CONCAT('$',SUM(Credit_Amount)) AS Total_IncomeFROM Income WHERE Transaction_Date BETWEEN '2020-02-01' AND '2020-02-29'GROUP BY MONTH(Transaction_Date)DECLARE @march int = 2SELECT Month(Transaction_Date) AS Month_Number,DATENAME(MONTH,DATEADD(MONTH,@march,0)) AS 'Month',CONCAT('$',SUM(Credit_Amount)) AS Total_IncomeFROM Income WHERE Transaction_Date BETWEEN '2020-03-01' AND '2020-03-31'GROUP BY MONTH(Transaction_Date)DECLARE @april int = 3SELECT Month(Transaction_Date) AS Month_Number,DATENAME(MONTH,DATEADD(MONTH,@april,0)) AS 'Month',CONCAT('$',SUM(Credit_Amount)) AS Total_IncomeFROM Income WHERE Transaction_Date BETWEEN '2020-04-01' AND '2020-04-30'GROUP BY MONTH(Transaction_Date)                ) _7 g9 N0 h, _. |
    解决方案:                                                               
( P- \" b: d, D* T1 U* o* P                                                                您可以按EOMONTH月的最后一天分组! A& p" a# S; g, h
SELECT    Month(EOMONTH(Transaction_Date)) AS Month_Number,   DATENAME(MONTH,EOMONTH(Transaction_Date)) AS [Month],   CONCAT('$',SUM(Credit_Amount)) AS Total_IncomeFROM Income WHERE Transaction_Date >= '2020-01-01' AND Transaction_Date < '2020-05-01'GROUP BY     EOMONTH(Transaction_Date);
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则