[英]SQL Date Query include month even if no data
I have a booking database with various dates for each booking. 我有一个预订数据库,其中包含每个预订的不同日期。 I want to get a count of all bookings in each month eg 我想获得每个月所有预订的数量,例如
JAN | 12
FEB | 15
MAR | 53
APR | 25
If I have zero bookings in a month, how can I still get the month listed? 如果我一个月内的预订量为零,那么我仍如何获得列出的月份? eg: 例如:
JAN | 12
FEB | 15
MAR | 53
APR | 0
MAY | 52
Schema: 架构:
ID | BookingDate | REF
I don't have or want to have a months table to join against. 我没有或不想参加一个月的会议。
Below is my query so far: 以下是我到目前为止的查询:
SELECT TOP 1000 [Id]
,[MemberId]
,[LocationId]
,[Date]
,[BookingStateId]
,[RPEFeeling]
,[RPEPush]
,[Notes]
,[TimeSlotId]
,[MembershipId]
,[TrialSession]
,[CreatedDate]
,[ModifiedDate]
,[CreatedBy]
,[ModifiedBy]
FROM [DB].[dbo].[SessionBooking]
DECLARE @months TABLE (MonthNumber INT, YearNumber INT, Name VARCHAR(50))
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (1, 2014, 'Jan')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (2, 2014, 'Feb')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (3, 2014, 'Mar')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (4, 2014, 'Apr')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (5, 2014, 'May')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (6, 2014, 'Jun')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (7, 2014, 'Jul')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (8, 2014, 'Aug')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (9, 2014, 'Sept')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (10, 2014, 'Oct')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (11, 2014, 'Nov')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (12, 2014, 'Dec')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (1, 2015, 'Jan')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (2, 2015, 'Feb')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (3, 2015, 'Mar')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (4, 2015, 'Apr')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (5, 2015, 'May')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (6, 2015, 'Jun')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (7, 2015, 'Jul')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (8, 2015, 'Aug')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (9, 2015, 'Sept')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (10, 2015, 'Oct')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (11, 2015, 'Nov')
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (12, 2015, 'Dec')
SELECT MonthNumber, YearNumber, SUM(Id)
FROM SessionBooking b
RIGHT JOIN @months mo ON DATEPART(m, b.Date) = mo.MonthNumber
AND DATEPART(yy, b.Date) = mo.YearNumber
GROUP BY mo.MonthNumber, mo.YearNumber
This is only returning the following results (where there are multiple bookings over last two years on each month. 这仅返回以下结果(每个月过去两年中有多次预订)。
MonthNumber YearNumber (No column name)
1 2014 NULL
2 2014 NULL
3 2014 NULL
4 2014 NULL
5 2014 NULL
6 2014 NULL
7 2014 NULL
8 2014 NULL
9 2014 NULL
10 2014 NULL
11 2014 NULL
12 2014 NULL
1 2015 NULL
2 2015 215
3 2015 134
4 2015 NULL
5 2015 NULL
6 2015 NULL
7 2015 NULL
8 2015 NULL
9 2015 NULL
10 2015 NULL
11 2015 NULL
12 2015 NULL
You need to have a table variable with all the months and do a JOIN. 您需要在所有月份中都有一个表变量,然后进行JOIN。
DECLARE @months TABLE (MonthNumber INT, YearNumber INT, Name VARCHAR(50))
INSERT INTO @months (MonthNumber, YearNumber, Name) VALUES (1, 2015, 'January')
...
SELECT MonthNumber, YearNumber, ISNULL(SUM(...), 0)
FROM Booking b
RIGHT JOIN @months mo ON DATEPART(m, b.BookingDate)=mo.MonthNumber AND DATEPART(yy, b.BookingDate)=mo.YearNumber
GROUP BY mo.MonthNumber, mo.YearNumber
EDIT: If you want to consider multiple years, add year as a column in @month and change the CROSS JOIN condition to consider month and year. 编辑:如果要考虑多年,请将年添加为@month中的列,并更改CROSS JOIN条件以考虑年和月。
The easy way is using a months table because you can have empty months. 最简单的方法是使用月表,因为您可以有空的月。
create table months (
month_id integer,
date_ini datetime,
date_end datetime
)
And this are some examples of how create that table automatic 这是一些如何自动创建表格的示例
Using select Generate days from date range 使用选择从日期范围生成天数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.