使用T-SQL,我想要一個新的列,它會顯示每個月的第一天,當前年份的getdate()。 之後,我需要計算這個特定日期的行。我應該使用CTE還是臨時表?當前getdate的每個月的第一天遞歸
-1
A
回答
1
幾個月的某一年的即席表:
declare @year date = dateadd(year,datediff(year,0,getdate()),0)
;with Months as (
select
MonthStart=dateadd(month,n,@year)
from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11)) t(n)
)
select MonthStart
from Months
rextester演示:http://rextester.com/POKPM51023
回報:
+------------+
| MonthStart |
+------------+
| 2017-01-01 |
| 2017-02-01 |
| 2017-03-01 |
| 2017-04-01 |
| 2017-05-01 |
| 2017-06-01 |
| 2017-07-01 |
| 2017-08-01 |
| 2017-09-01 |
| 2017-10-01 |
| 2017-11-01 |
| 2017-12-01 |
+------------+
第一部分:dateadd(year,datediff(year,0,getdate()),0)
增加了,因爲年數1900-01-01
至日期1900-01-01
。所以它會返回一年中的第一個日期。您還可以將year
替換爲其他級別的截斷:年,季,月,日,小時,分鐘,秒等等。
第二部分使用common table expression和table value constructor (values (...),(...))
來源數字0-11,這是作爲月份添加到年初。
0
不知道爲什麼你需要遞歸...但對於一個月的第一天,你可以嘗試查詢象下面這樣:
Select Dateadd(day,1,eomonth(Dateadd(month, -1,getdate())))
0
declare @year date = dateadd(year,datediff(year,0,getdate()),0)
;WITH months(MonthNumber) AS
(
SELECT 0
UNION ALL
SELECT MonthNumber+1
FROM months
WHERE MonthNumber < 11
)
select dateadd(month,MonthNumber,@year)
from months
2
如果2012+,你可以使用DateFromParts()
至獲取日期的列表
Select D = DateFromParts(Year(GetDate()),N,1)
From (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) N(N)
返回
D
2017-01-01
2017-02-01
2017-03-01
2017-04-01
2017-05-01
2017-06-01
2017-07-01
2017-08-01
2017-09-01
2017-10-01
2017-11-01
2017-12-01
編輯跨數
要獲得交易(按月假設)。它成爲一個左的小事加入到創建日期
-- This is Just a Sample Table Variable for Demonstration.
-- Remove this and Use your actual Transaction Table
--------------------------------------------------------------
Declare @Transactions table (TransDate date,MoreFields int)
Insert Into @Transactions values
('2017-02-18',6)
,('2017-02-19',9)
,('2017-03-05',5)
Select TransMonth = A.MthBeg
,TransCount = count(B.TransDate)
From (
Select MthBeg = DateFromParts(Year(GetDate()),N,1)
,MthEnd = EOMonth(DateFromParts(Year(GetDate()),N,1))
From (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) N(N)
) A
Left Join @Transactions B on TransDate between MthBeg and MthEnd
Group By A.MthBeg
返回
TransMonth TransCount
2017-01-01 0
2017-02-01 2
2017-03-01 1
2017-04-01 0
2017-05-01 0
2017-06-01 0
2017-07-01 0
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-12-01 0
相關問題
- 1. 每個月的第一天
- 2. SQL得到每個月的第一天當月
- 3. 當前月份第一天的日期和提前5天的日期。 MySQL的
- 4. SQL返回第一天,每月的表
- 5. 如何在每月的第一天添加1個月至datetimepicker?
- 6. PHP - MYSQL:每月最後12個月的第一天到最後一天
- 7. 如何獲得當月的第一天?
- 8. 獲取當月的第一天-1個月
- 9. 當天+ N天下個月或上個月的一天
- 10. Pickadate - 每個月的第一天開始月份,而不是一週的第一天
- 11. SQL SELECT每月的第一天和最後一天。
- 12. 如何查找db2當前月份的第28個月到第27個月的天數
- 13. 同一個月的第一天
- 14. 如何在R中的每個月獲得10個第一天
- 15. SQL檢索數據從一年前的一個月的第一天到今天
- 16. Python的日曆,數週的空天前的第一個月
- 17. 得到的天數在每月的第一個星期在PHP
- 18. 鑑於NSDate,找到第四個前個月的最後一天
- 19. 當月的第一天和最後一天
- 20. 查詢當月的第一天和最後一天
- 21. 本月的第一天
- 22. 獲取月的第一天
- 23. 如何獲得當月的時間戳,一個月的第一天,零時
- 24. 每個月的第1天只插入一次數據
- 25. EJB 3.0 TimerService - 在每個月的第一天運行計時器
- 26. 在每個月的第一天開始工作流程
- 27. 計劃函數在每個月的第一天運行
- 28. Quartz.net - 在每m個月的第n天重複一次?
- 29. 在jQuery-ui Datepicker中突出顯示每個月的第一天
- 30. 上個月第一天的Excel公式
的感謝!我真的很感謝這個簡單的解決方案 –
謝謝!你能提供一個你的想法的小解釋嗎? – Proffesore
@Poffesore FROM部分生成12個記錄(1-12),字段名稱爲N.然後我們簡單地使用N作爲DateFromParts()中的月份。 ... DateFromPart(年,月,日)... –