当前位置: 首页 > 面试题库 >

SQL Server按月分组

谭景福
2023-03-14
问题内容

我有一个具有此架构的表

ItemID    UserID    Year    IsPaid    PaymentDate  Amount
1         1         2009    0         2009-11-01  300
2         1         2009    0         2009-12-01  342
3         1         2010    0         2010-01-01  243
4         1         2010    0         2010-02-01  2543
5         1         2010    0         2010-03-01  475

我正在尝试使查询工作显示每个月的总数。到目前为止,我已经尝试过DateDiff和嵌套选择,但都没有给我想要的东西。我认为这是最接近的:

DECLARE @start [datetime] = 2010/4/1;
SELECT ItemID, IsPaid,
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 And DateDiff(m, PaymentDate, @start) = 0 AND UserID = 100) AS "Apr",
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =1 AND UserID = 100) AS "May",
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =2 AND UserID = 100) AS "Jun", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =3 AND UserID = 100) AS "Jul", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =4  AND UserID = 100) AS "Aug", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =5  AND UserID = 100) AS "Sep", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =6  AND UserID = 100) AS "Oct", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =7 AND UserID = 100) AS "Nov", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =8 AND UserID = 100) AS "Dec", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =9 AND UserID = 100) AS "Jan", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =10 AND UserID = 100) AS "Feb", 
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 AND DateDiff(m, PaymentDate, @start) =11 AND UserID = 100) AS "Mar" 
FROM LIVE L INNER JOIN Payments I ON I.LiveID = L.RECORD_KEY 
WHERE UserID = 16178

但是当我应该获取值时,我只会得到空值。我想念什么吗?


问题答案:
SELECT CONVERT(NVARCHAR(10), PaymentDate, 120) [Month], SUM(Amount) [TotalAmount]
FROM Payments
GROUP BY CONVERT(NVARCHAR(10), PaymentDate, 120)
ORDER BY [Month]

您也可以尝试:

SELECT DATEPART(Year, PaymentDate) Year, DATEPART(Month, PaymentDate) Month, SUM(Amount) [TotalAmount]
FROM Payments
GROUP BY DATEPART(Year, PaymentDate), DATEPART(Month, PaymentDate)
ORDER BY Year, Month


 类似资料:
  • 问题内容: 我有一个SQLite数据库,其中包含交易,每个交易都有一个 价格 和一个 transDate 。 我想检索按月分组的交易总和。检索到的记录应类似于以下内容: 问题答案: 当您按MONTH分组时总是很好,还应该检查YEAR 对于SQLITE

  • 问题内容: 我有一个非常基本的模型: 我可以使用以下命令创建按日期分组的所有条目的列表: 这自然会按天对项目进行分组。但是我真正想做的是按月分组。无论如何,我可以使用anateate()做到这一点吗? 非常感谢, 问题答案: 如果您使用的是PostgreSQL,则可能需要执行以下操作: 我不确定跨其他数据库的可移植性。

  • 问题内容: 我正在尝试返回一些在滚动12个月内分散的结果,例如: 如何在日期范围内分散结果,并在第一列中填充月份名称? 在MSSQL中,它将类似于: 但这是针对Oracle数据库的,因此DATEPART和DateTime不可用。 我的问题表大致为: 问题答案: 使用: 您还可以使用:

  • 问题内容: 我想要 但是这个查询不能使用 如何使用SQLAlchemy做到这一点? 问题答案: 假设您的数据库引擎实际上支持诸如之类的功能,则可以尝试 否则你可以像

  • 问题内容: 给定一个在每行上都带有时间戳的表,您将如何格式化查询以适合此特定的json对象格式。 我正在尝试将json对象组织成年/月。 json以查询为基础: 这是我到目前为止的查询- 该查询正在分解,因为它(可预测地)将不同年份组合在一起。 问题答案: 是你想要的。

  • 问题内容: 我有一个像这样的简单模型: 我想输出按月细分: 一个月内有多少笔交易() 合并值() 我不确定最好的攻击方法是什么。我已经看到了一些看上去很吓人的额外选择查询,但我的简单想法是告诉我,最好是迭代数字,从任意开始的年/月开始,一直计数到我达到当月,然后简单地抛出查询该月的过滤条件。更多数据库工作-减轻开发人员压力! 什么对你最有意义?有什么好方法可以拉回快速数据表吗?还是我的肮脏方法可能