My first table is Called Date_Table this has all the days of the year dating back to January 2020 and goes all the way to 31 Decemeber 2030.The data looks like this:
| Date | Day |
|---|---|
| 01/01/2020 00:00 | Wed |
| 02/01/2020 00:00 | Thur |
My second table Sales_Table consists of sales stages, each stage has a date assigned when it reaches the stage. Here is what my data looks like:
| Sale_ID | Enquiry | App Rec | App Passed | Sale Completed |
|---|---|---|---|---|
| 1 | 01/01/2020 00:00 | 02/01/2020 00:00 | 03/01/2020 00:00 | 05/01/2020 00:00 |
| 2 | 02/01/2020 00:00 | 03/01/2020 00:00 | 03/01/2020 00:00 | 05/01/2020 00:00 |
My query is this so far:
SELECT COUNT(CASE WHEN [Enquiry] IS NOT NULL THEN 1
END) AS [Total Enquiries],
COUNT(CASE WHEN [App Rec] IS NOT NULL THEN 1
END) AS [Apps Received],
COUNT(CASE WHEN [App Passed] IS NOT NULL THEN 1
END) AS [Apps Passed],
COUNT(CASE WHEN [Sale Completed] IS NOT NULL THEN 1
END) AS [Completed Sales]
FROM [Date_Table]
LEFT JOIN [Sales_Table] ON [Date] Between [Enquiry] AND [App Rec] GROUP BY [Date]
This seems to work fine for the first 2 dates but if i add another AND statement in the join I get a parsing of SQL query failed error.
The end report would be a table like this:
| Date | Enquiry | App Rec | App Passed | Sale Completed |
|---|---|---|---|---|
| 01/01/2020 00:00 | 3 | 6 | 4 | 2 |
| 02/01/2020 00:00 | 5 | 1 | 11 | 4 |
I tried to add all the dates fields to the left join using BETWEEN and AND
Like this:
LEFT JOIN [Sales_Table] ON [Date] Between [Enquiry] AND [App Rec] AND [App Passed] AND [Sale Completed]
This just gave me a parsing error
See example
Output is
with test data
Fiddle