Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

dynamic query

$
0
0

The below code gives me output with C_01 to C_10 as shown below, I want to dynamically add c_11,c_12...c_xx (xx any number max value as 99)

If we do "select * from #cond"  for mid col value 1 we have 10 distinct c_id values hence we have c_01 to c_10 columns below(which is max 10 records for mid 1, and for mid 2 we have only 4 rec 10,11,12 and 13) means we have picked up max value of cid for any mid value and created those many columns.   

Now to this I want to pickup the max cid which is c_10 in this case and add few more columns c_xx to the #res table with out adding any more records to #cond table, so that my #res table has c_11, c_12....c_xx number of columns. (i,e more number of columns for future use)

the image output is from #res output table

--drop table #cond 
--drop table #res 
--drop table #condList 
--select * from #cond order by 1,2

Create table #cond (m_id int, c_id Int, c_desc varchar(100))
Insert into #cond 
Select 1,1 ,'1_1_Desc'  Union all
Select 1,2 ,'1_2_Desc'  Union all
Select 1,3 ,'1_3_Desc'  Union all
Select 1,4 ,'1_4_Desc'  Union all
Select 1,5 ,'1_5_Desc'  Union all 
Select 1,6 ,'1_6_Desc'  Union all
Select 1,7 ,'1_7_Desc'  Union all
Select 1,8 ,'1_8_Desc'  Union all
Select 1,9 ,'1_9_Desc'  Union all
Select 1,36,'1_10_Desc' Union all
Select 2,10,'2_10_Desc' Union all
Select 2,11,'2_11_Desc' Union all
Select 2,12,'2_12_Desc' Union all
Select 2,13,'2_13_Desc' Union all
Select 3,14,'3_14_Desc' Union all
Select 3,15,'3_15_Desc' Union all
Select 3,16,'3_16_Desc' Union all
Select 3,17,'3_17_Desc' Union all
Select 3,18,'3_18_Desc' Union all
Select 3,19,'3_19_Desc' Union all
Select 3,20,'3_20_Desc' Union all
Select 4,21,'4_21_Desc' Union all
Select 4,22,'4_22_Desc' Union all
Select 4,23,'4_23_Desc' Union all
Select 4,24,'4_24_Desc' Union all
Select 4,25,'4_25_Desc' Union all
Select 4,26,'4_26_Desc' Union all
Select 5,27,'5_27_Desc' Union all
Select 5,28,'5_28_Desc' Union all
Select 5,29,'5_29_Desc' Union all
Select 5,30,'5_30_Desc' Union all
Select 5,31,'5_31_Desc' Union all
Select 6,32,'6_32_Desc' Union all
Select 6,33,'6_33_Desc' Union all
Select 6,34,'6_34_Desc' Union all
Select 6,35,'6_35_Desc' 

Create table #res (ReportingPeriodID INt, pid Int, vid Int, m_id Int, c_id Int, condtatus Int)
Insert into #res 
Select 1,1,1,1,1 ,1 Union All
Select 1,1,1,1,2 ,1 Union All
Select 1,1,1,1,3 ,1 Union All
Select 1,1,1,1,4 ,0 Union All
Select 1,1,1,1,6 ,0 Union All
Select 1,1,1,1,7 ,1 Union All
Select 1,1,1,1,8 ,0 Union All
Select 1,1,1,1,9 ,1 Union All
Select 1,1,1,2,10,0 Union All
Select 1,1,1,2,11,1 Union All
Select 1,1,1,2,12,1 Union All
Select 1,1,1,3,14,1 Union All
Select 1,1,1,3,15,0 Union All
Select 1,1,1,3,16,0 Union All
Select 1,1,1,3,17,1 Union All
Select 1,1,1,3,18,0 Union All
Select 1,1,1,3,20,1 Union All
Select 1,2,2,3,14,0 Union All
Select 1,2,2,3,15,1 Union All
Select 1,2,2,3,16,1 Union All
Select 1,2,2,3,17,1 Union All
Select 1,2,2,3,19,0 Union All
Select 1,2,2,3,20,0 Union All
Select 1,2,2,4,21,1 Union All
Select 1,2,2,4,22,0 Union All
Select 1,2,2,4,23,1 Union All
Select 1,2,2,4,24,0 Union All
Select 1,2,2,4,26,1 Union All
Select 1,2,2,5,27,1 Union All
Select 1,2,2,5,28,1 Union All
Select 1,2,2,5,29,0 Union All
Select 1,2,2,5,30,0 Union All
Select 1,2,2,5,31,1 Union All
Select 1,2,2,6,32,0 Union All
Select 1,2,2,6,33,1 Union All
Select 1,2,2,6,35,0      

--Select * from #cond 

SELECT m_id, c_id,
   'C_'+ right('00'+ltrim(str(ROW_NUMBER ( )   
OVER ( PARTITION BY m_id order by c_id ) )),2) AS Mseq  
   Into #condList
FROM #cond  

--Select * from condList 
DECLARE @cols AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Mseq) 
                    from #condList 
                    group by Mseq 
                    order by Mseq
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT pid, vid, m_id, ' + @cols + N' from 
             (
                select R.pid, r.vid, C.m_id, C.Mseq, Isnull(r.condtatus,-1) as condtatus
                from #res R inner Join #condList C on c.m_id = r.m_id and c.c_id = r.c_id where c.m_id <= 7 
            ) x
            pivot 
            (
                max(condtatus)
                for Mseq in (' + @cols + N')
            ) p '

--Select @cols 
--Select @query 

exec sp_executesql @query;



Neil




Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>