sql月度分组_SQL Server按月分组
SQL Server按月分組
我有一個具有此架構的表
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
但是當我應該獲取值時,我只會得到空值。 我想念什么嗎?
7個解決方案
101 votes
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
Dave Downs answered 2020-06-17T06:16:37Z
20 votes
將NVARCHAR的尺寸限制為7,提供給CONVERT以僅顯示“ YYYY-MM”
SELECT CONVERT(NVARCHAR(7),PaymentDate,120) [Month], SUM(Amount) [TotalAmount]
FROM Payments
GROUP BY CONVERT(NVARCHAR(7),PaymentDate,120)
ORDER BY [Month]
Martyn Davis answered 2020-06-17T06:16:58Z
5 votes
我更喜歡結合Month和DateTime函數,如下所示:
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Created),0)
這兩個函數共同將比指定日期部分(在此示例中為Month)小的日期分量歸零。
您可以將datepart位更改為Month、DateTime、DAY等,這非常方便。
這樣,您原始的SQL查詢將類似于以下內容(由于我沒有設置數據,因此無法對其進行測試,但它應該使您處于正確的軌道)。
DECLARE @start [datetime] = '2010-04-01';
SELECT
ItemID,
UserID,
DATEADD(MONTH, DATEDIFF(MONTH, 0, Created),0) [Month],
IsPaid,
SUM(Amount)
FROM LIVE L
INNER JOIN Payments I ON I.LiveID = L.RECORD_KEY
WHERE UserID = 16178
AND PaymentDate > @start
還有一件事:Month列鍵入為DateTime,如果您需要進一步處理該數據或將其映射為.NET對象,這也是一個不錯的優勢。
bounav answered 2020-06-17T06:17:36Z
3 votes
DECLARE @start [datetime] = 2010/4/1;
應該...
DECLARE @start [datetime] = '2010-04-01';
您所擁有的是將2010除以4,然后除以1,然后轉換為日期。 從1900-01-01開始的第57.5天。
初始化后,嘗試SELECT @start,以檢查是否正確。
MatBailie answered 2020-06-17T06:18:05Z
3 votes
如果您需要經常執行此操作,則可能要向表中添加一個計算列PaymentMonth:
ALTER TABLE dbo.Payments ADD PaymentMonth AS MONTH(PaymentDate) PERSISTED
它被持久化并存儲在表中-因此查詢它實際上沒有性能開銷。 這是一個4字節的INT值-因此空間開銷也很小。
一旦有了它,就可以簡化查詢,使其類似于以下內容:
SELECT ItemID, IsPaid,
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 And PaymentMonth = 1 AND UserID = 100) AS 'Jan',
(SELECT SUM(Amount) FROM Payments WHERE Year = 2010 And PaymentMonth = 2 AND UserID = 100) AS 'Feb',
.... and so on .....
FROM LIVE L
INNER JOIN Payments I ON I.LiveID = L.RECORD_KEY
WHERE UserID = 16178
marc_s answered 2020-06-17T06:18:34Z
1 votes
另一種不涉及在結果中添加列的方法是簡單地將日期的CONVERT零置零,因此CONVERT和2016-07-16都將是2016-07-01-從而使它們按月相等。
如果您具有datetime(而不是CONVERT)值,則可以將其直接置零:
SELECT
DATEADD( day, 1 - DATEPART( day, [Date] ), [Date] ),
COUNT(*)
FROM
[Table]
GROUP BY
DATEADD( day, 1 - DATEPART( day, [Date] ), [Date] )
如果您有datetime值,則需要使用CONVERT刪除“時段”部分:
SELECT
DATEADD( day, 1 - DATEPART( day, [Date] ), CONVERT( date, [Date] ) ),
COUNT(*)
FROM
[Table]
GROUP BY
DATEADD( day, 1 - DATEPART( day, [Date] ), CONVERT( date, [Date] ) )
Dai answered 2020-06-17T06:19:03Z
0 votes
現在,您的查詢明確地只查看了年份= 2010的付款,但是,我想您的意思是讓Jan / Feb / Mar實際代表2009。如果是這樣,則需要針對這種情況進行一些調整。 不要繼續查詢每一列的總和值,而只是查詢以月為單位的日期差的條件。 將其余的放在WHERE子句中。
SELECT
SUM( case when DateDiff(m, PaymentDate, @start) = 0
then Amount else 0 end ) AS "Apr",
SUM( case when DateDiff(m, PaymentDate, @start) = 1
then Amount else 0 end ) AS "May",
SUM( case when DateDiff(m, PaymentDate, @start) = 2
then Amount else 0 end ) AS "June",
SUM( case when DateDiff(m, PaymentDate, @start) = 3
then Amount else 0 end ) AS "July",
SUM( case when DateDiff(m, PaymentDate, @start) = 4
then Amount else 0 end ) AS "Aug",
SUM( case when DateDiff(m, PaymentDate, @start) = 5
then Amount else 0 end ) AS "Sep",
SUM( case when DateDiff(m, PaymentDate, @start) = 6
then Amount else 0 end ) AS "Oct",
SUM( case when DateDiff(m, PaymentDate, @start) = 7
then Amount else 0 end ) AS "Nov",
SUM( case when DateDiff(m, PaymentDate, @start) = 8
then Amount else 0 end ) AS "Dec",
SUM( case when DateDiff(m, PaymentDate, @start) = 9
then Amount else 0 end ) AS "Jan",
SUM( case when DateDiff(m, PaymentDate, @start) = 10
then Amount else 0 end ) AS "Feb",
SUM( case when DateDiff(m, PaymentDate, @start) = 11
then Amount else 0 end ) AS "Mar"
FROM
Payments I
JOIN Live L
on I.LiveID = L.Record_Key
WHERE
Year = 2010
AND UserID = 100
DRapp answered 2020-06-17T06:19:24Z
總結
以上是生活随笔為你收集整理的sql月度分组_SQL Server按月分组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为OPPO魅族iQOO齐聚!3月还有这
- 下一篇: linux cmake编译源码,linu