I am trying to build a list by fiscal quarter of when a customer was assigned to a specific sales person. I need this list to track results for the salesperson. Customers may move between sales people on a quarterly basis. I have the tblFiscalQuarterMappingTable shown below to translate calendar months to fiscal quarters. (Fiscal year is from September through August)
tblFiscalQuarterMappingTable:
CalendarYearFiscalYearID | CalendarMonth | CalendarYear | FiscalQuarter | FiscalYear | QtrStartDate | QtrEndDate |
240 | 8 | 2021 | 4 | 2021 | 8/1/2021 | 8/31/2021 |
241 | 9 | 2021 | 1 | 2022 | 9/1/2021 | 9/30/2021 |
242 | 10 | 2021 | 1 | 2022 | 10/1/2021 | 10/31/2021 |
243 | 11 | 2021 | 1 | 2022 | 11/1/2021 | 11/30/2021 |
244 | 12 | 2021 | 2 | 2022 | 12/1/2021 | 12/31/2021 |
245 | 1 | 2022 | 2 | 2022 | 1/1/2022 | 1/31/2022 |
246 | 2 | 2022 | 2 | 2022 | 2/1/2022 | 2/28/2022 |
247 | 3 | 2022 | 3 | 2022 | 3/1/2022 | 3/31/2022 |
248 | 4 | 2022 | 3 | 2022 | 4/1/2022 | 4/30/2022 |
249 | 5 | 2022 | 3 | 2022 | 5/1/2022 | 5/31/2022 |
250 | 6 | 2022 | 4 | 2022 | 6/1/2022 | 6/30/2022 |
251 | 7 | 2022 | 4 | 2022 | 7/1/2022 | 7/31/2022 |
My issue is the constituent_id used in the sample code below should return rows from quarter 1-2022 forward. For some reason the result starts with fiscal quarter 2-2022 thereby not include quarter 1. Please note the Constit_Solicitors.date_From field is in the format of YYYYMMDD and I am converting it in the code.
Code:
SELECT TOP (100) PERCENT dbo.Records.CONSTITUENT_ID, Records_1.ORG_NAME, dbo.tblFiscalQuarterMapping.FiscalQuarter,
dbo.tblFiscalQuarterMapping.FiscalYear, dbo.CONSTIT_SOLICITORS.DATE_TO, dbo.CONSTIT_SOLICITORS.DATE_FROM, CONVERT(date, dbo.CONSTIT_SOLICITORS.DATE_FROM, 120) AS ConvertedDateFrom
FROM dbo.Records INNER JOIN
dbo.CONSTIT_SOLICITORS ON dbo.Records.ID = dbo.CONSTIT_SOLICITORS.CONSTIT_ID INNER JOIN
dbo.Records AS Records_1 ON dbo.CONSTIT_SOLICITORS.SOLICITOR_ID = Records_1.ID INNER JOIN
dbo.tblFiscalQuarterMapping ON
Convert(date,dbo.CONSTIT_SOLICITORS.DATE_FROM,120)<=QTRStartDate and (Convert(date,dbo.Constit_Solicitors.Date_TO,120)>=QtrEndDate or Date_To is null)
WHERE (dbo.CONSTIT_SOLICITORS.SOLICITOR_TYPE = 10245)
GROUP BY dbo.Records.CONSTITUENT_ID, Records_1.ORG_NAME, dbo.tblFiscalQuarterMapping.FiscalQuarter, dbo.tblFiscalQuarterMapping.FiscalYear, dbo.CONSTIT_SOLICITORS.DATE_TO,
dbo.CONSTIT_SOLICITORS.DATE_FROM
HAVING (dbo.Records.CONSTITUENT_ID = '1131669')
ORDER BY dbo.tblFiscalQuarterMapping.FiscalYear, dbo.tblFiscalQuarterMapping.FiscalQuarter
The code returns:
Constituend_ID | ORGName | FiscalQuarter | FiscalYear | Date_TO | DATE_FROM | ConvertedDateFrom |
1131669 | NULL | 2 | 2022 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 3 | 2022 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 4 | 2022 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 1 | 2023 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 2 | 2023 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 3 | 2023 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 4 | 2023 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 1 | 2024 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 2 | 2024 | NULL | 20211102 | 11/2/2021 |
1131669 | NULL | 3 | 2024 | NULL | 20211102 | 11/2/2021 |
I cannot figure out why Fiscal Quarter 1-2021 is missing.
Thanks!
For troubleshooting, try to:
1) Simplify the joining and see what data is missing there
2) Similar to above, and use a LEFT JOIN instead of INNER JOIN for debugging