I'm a trying to figure out how to write a query that appends multiple tables and counts the missing dates. Some locations have 5 business days/week (tableX) and some 6 business days (tableY). I'd like to determine business days for the location, extract the current count and figure out how many days some of them are behind with filling in data. I've tried a few examples from SO using UNION ALL, DATEDIFF
and MAX
of the current that but that's not giving me the expected result. The missing rows are there just the illustrate the blanks. The simplest query, the better. Thank you.
tableX
+-----------+----------+---------+-------+--------------+--------------+
| Date | Location | DayName | Count | BusinessDays | Missing Days |
+-----------+----------+---------+-------+--------------+--------------+
| 4/1/2020 | X | Wed | 1 | | |
| 4/2/2020 | X | Thu | 1 | | |
| 4/3/2020 | X | Fri | 1 | | |
| 4/4/2020 | X | Sat | | | |
| 4/5/2020 | X | Sun | | | |
| missing | | (Mon) | | | |
| missing | | (Tue) | | | |
| 4/8/2020 | X | Wed | 1 | | |
| 4/9/2020 | X | Thu | 1 | | |
| 4/10/2020 | X | Fri | 1 | | |
| 4/11/2020 | X | Sat | | | |
| 4/12/2020 | X | Sun | | | |
| 4/13/2020 | X | Mon | 1 | | |
| 4/14/2020 | X | Tue | 1 | | |
| 4/15/2020 | X | Wed | 1 | | |
| | | Total | 9 | 11 | 2 |
+-----------+----------+---------+-------+--------------+--------------+
tableY
+-----------+----------+---------+-------+--------------+--------------+
| Date | Location | DayName | Count | BusinessDays | Missing Days |
+-----------+----------+---------+-------+--------------+--------------+
| 4/1/2020 | Y | Wed | 1 | | |
| 4/2/2020 | Y | Thu | 1 | | |
| 4/3/2020 | Y | Fri | 1 | | |
| 4/4/2020 | Y | Sat | 1 | | |
| 4/5/2020 | Y | Sun | | | |
| missing | | (Mon) | | | |
| missing | | (Tue) | | | |
| missing | | (Wed) | | | |
| 4/9/2020 | Y | Thu | 1 | | |
| 4/10/2020 | Y | Fri | 1 | | |
| 4/11/2020 | Y | Sat | 1 | | |
| 4/12/2020 | Y | Sun | | | |
| 4/13/2020 | Y | Mon | 1 | | |
| 4/14/2020 | Y | Tue | 1 | | |
| 4/15/2020 | Y | Wed | 1 | | |
| | | Total | 10 | 13 | 3 |
+-----------+----------+---------+-------+--------------+--------------+
Desired result
+-------+------+----------+--------------+
| Month | Year | Location | Missing Days |
+-------+------+----------+--------------+
| 4 | 2020 | X | 2 |
| 4 | 2020 | Y | 3 |
+-------+------+----------+--------------+
simple failed example
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = (SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
SET @EndDate = GETDATE()
SELECT [Location],
[Date],
(DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) AS Businessdays
FROM [Daily].[dbo].[tableX] WHERE YEAR(Date)=YEAR(getdate()) AND MONTH(Date)=MONTH(getdate())
UNION ALL
SELECT [Location],
[Date],
(DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) AS Businessdays
FROM [Daily].[dbo].[tableY] WHERE YEAR(Date)=YEAR(getdate()) AND MONTH(Date)=MONTH(getdate())