A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Hi @EagleFire26 ,
I don’t know what your data and your query look like, so I can’t advise you. Please provide a minimal example that we can test. This includes the code you are trying now and the CREATE and INSERT statements, as well as the expected output of your example.
Try:
SET @SQLQUERY =
'
select * from
{
SELECT DISTINCT
QUOTENAME(CONCAT(YEAR(SHP.RECEIPT_DATE),''-'',FORMAT(MONTH(SHP.RECEIPT_DATE),''00''))) RECEIPT_MONTH
,QUOTENAME(CONCAT(YEAR(SHP.STATEMENT_START_DATE),''-'',FORMAT(MONTH(SHP.STATEMENT_START_DATE),''00''))) INCURRED_MONTH
,SHP.RBO_INST_ENCOUNTERS INST_ENCOUNTERS
FROM #TEMPSHP SHP
LEFT OUTER JOIN #TEMPHHR HHR ON SHP.CLAIM_HCC_ID = CLAIM_CLAIM_HCC_ID
WHERE RECEIPT_DATE BETWEEN ''' + cast(@STARTDATE as varchar) + ''' AND ''' +cast( @ENDDATEas varchar) + ''') AS BaseData
PIVOT
(SUM(INST_ENCOUNTERS)
FOR INCURRED_MONTH
IN (' + @PIVOTCOLUMN +') AS PIVOTTABLE
'
PRINT @SQLQUERY
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.