have the following dynamic query that creates a pivoted table for each account_id against the products using a UDF.I need to add spaces after the UDF is executed depending up the account_id being matched to the specific tables.this wrks perfect and gives me the 2nd table in the image, I just want to add some condition where it checks for account id's present in other tables and then adds spaces to output from the UDF eg- the account id in the pivot matches the id present in test_rev then 2 spaces if the account_id in the pivot matches the id present in test_oncontract then 3 spaces so on
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.A_NAME) FROM TEST_11 c FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set @query = 'SELECT account_id, ' + @cols + ' INTO TEST_DETAIL from ( select account_id , dbo.make_table(apple,ball,cat,dog) AS newvalue ,a_name from test_sample ) x pivot ( MAX(newvalue) for a_name in (' + @cols + ') ) p '
execute(@query)
THE IMAGE SHOWS HOW THE MAKE_TABLE udf CONVERTS THE 1ST TABLE INTO 2ND JUST PICKING UP THE INITIALS FROM EACH COL NAME FROM THE 1ST TABLE]1
Aucun commentaire:
Enregistrer un commentaire