Avatar of Andy Donnelly
Andy Donnelly
Flag for United States of America asked on

I'm having difficulty with dealing with converting YYYYMMDD in a SQL Server join.

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: 

CalendarYearFiscalYearIDCalendarMonthCalendarYearFiscalQuarterFiscalYearQtrStartDateQtrEndDate
24082021420218/1/20218/31/2021
24192021120229/1/20219/30/2021
2421020211202210/1/202110/31/2021
2431120211202211/1/202111/30/2021
2441220212202212/1/202112/31/2021
24512022220221/1/20221/31/2022
24622022220222/1/20222/28/2022
24732022320223/1/20223/31/2022
24842022320224/1/20224/30/2022
24952022320225/1/20225/31/2022
25062022420226/1/20226/30/2022
25172022420227/1/20227/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

Open in new window

The code returns:

 

Constituend_IDORGNameFiscalQuarterFiscalYearDate_TODATE_FROMConvertedDateFrom
1131669NULL22022NULL2021110211/2/2021
1131669NULL32022NULL2021110211/2/2021
1131669NULL42022NULL2021110211/2/2021
1131669NULL12023NULL2021110211/2/2021
1131669NULL22023NULL2021110211/2/2021
1131669NULL32023NULL2021110211/2/2021
1131669NULL42023NULL2021110211/2/2021
1131669NULL12024NULL2021110211/2/2021
1131669NULL22024NULL2021110211/2/2021
1131669NULL32024NULL2021110211/2/2021

 I cannot figure out why Fiscal Quarter 1-2021 is missing.


Thanks!

SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
arnold

8/22/2022 - Mon
Ryan Chong

 I cannot figure out why Fiscal Quarter 1-2021 is missing. 

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
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PortletPaul

You are basing your query on dbo.Records and inner joining to the calendar
 
But if to ensure every quarter is displayed, base your query on the calendar table and LEFT join to dbo.records

i.e. if you base the query on the data, and there is no data for quarter 1, then it won't be reported
but, if you base the query on quarters, then each quarter is displayed provided you don't use only inner joins

Andy Donnelly

ASKER
I found the issue but would also like some additional advice.  

When I was comparing a date in the last month of the quarter in most cases if was later than the first day of the month, (date_from of  11/3/2021 wasn't in Quarter 1 since it's start date was later than 11/1/2021).  Using datediff to get the first day of the month solved that issue but is not probably a very elegant solution.  

St5an, could you provide some guidance on how to deal with the date offset so I don't need the fiscal quarter table?  

Also, any ideas on a better approach to getting all fiscal quarters that the customer was related to the sales person would be very helpful.

Thanks to everyone for the quick replies.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
arnold

you have the fiscal year starts at September
I would try and work a mathematical formula based on the month of the date.
9-11 1
12-2 2
3-5 3
6-8 4

adding a month * a coefficient to the date and using the datepart to get the quarter.
using an offset to adjust your circumstance.
in your example, you can add four months to the date, and then use datepart to extract the quarter and the year to which it applies.

you could use row_number () over to partition the data based on the above.

Not sure how inneficient or efficient this could be depending on how many data rows you have.

Can you post an example of the data