简体   繁体   English

SQL日期查询即使没有数据也包括月份

[英]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 使用选择从日期范围生成天数

This use store procedure in MSQL MSQL中使用存储过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 3 个月份字母的日期和月份的 SQL 查询 - SQL query for date and month with 3 letter of month Big Query(SQL)在日期(问题)中添加一个月-(Data Studio) - Big Query (SQL) Add one month to the date (Issue) - (Data Studio) 如何从当前日期sql查询中获取每个月的数据 - How to get data for each month from current date sql query Oracle sql 开发人员日期/月份数据提取查询和语法 - Oracle sql developer date/month data extract query and syntax SQL查询根据交易日期加入数据以获取本月和上个月的费用 - SQL query to join data based on transaction date to get this month's & previous month's cost sql查询获取从一月到当月的所有数据,即使没有记录 - sql query get all data from Jan to current month even if there are no records SQL查询以获取直到特定日期的当前日期(包括月份和日期)的数据 - SQL query to get data till current date(considering month and date) for the particular year SQL Select查询以选择截止日期之间的数据,其中起始日期月份大于截止日期月份,年份差> 1 - SQL Select query to select data between to dates where from date month is higher than the to date month and year difference is >1 SQL查询根据日期和月份检索用户 - SQL Query to retrieve users based on date and month 年/月/周至今 SQL 查询 - Year/Month/Week to date SQL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM