问题
Trying to write a query which will behave like a foreach
Query :
select label ,NTILE(10) over(order by label ASC) Quartile INTO #labelTempTab from dbo.ReportFieldsLookup
The data will be like :
label Quartile
----- --------
la1 1
la2 1
la3 1
sa1 2
sa2 2
sq3 2
ha1 3
ha2 3
ha3 3
ka1 4
ka2 4
kas3 4
Continuation of Query :
DECLARE @sql nvarchar(max)
SELECT * INTO #SetValuesTable FROM svo_tbl
SET @sql = 'SELECT MNUM, Label , LabelValue ,[Property Type] FROM #SetValuesTable '
+' CROSS APPLY ( VALUES '
+ stuff(( SELECT ',('''+ replace(C.label,'''','"') + ''',' + quotename(C.label) + ')' FROM #labelTempTab c WHERE c.Quartile = 1 group by label FOR xml path('')), 1, 1, '')
+' ) AS UPTab (Label , LabelValue);'
EXEC(@sql)
The above query will unpivot only for Quartile = 1
How can i make it work for 1 to n and union all the results.
data in #SetValuesTable will look like :
MNUM la1 la2 la3 sa1 sa2 sq3 ha1 ha2 ha3 ka1 ka2 Property Type
12 1 0 2 1 0 8 3 4 0 1 2 s
13 4 0 5 1 6 8 5 2 1 1 3 p
The result(Expected output) should look like
MNUM Label LabelValue Property Type
12 la1 1 s
12 la2 0 s
12 la3 2 s
12 sa1 1 s
12 sa2 0 s
12 sa3 8 s
........
13 ka1 1 p
13 ka2 1 p
12 ka3 3 p
continuation of the query :
SET @sql = @sql + ' INNER JOIN dbo.ReportFieldsLookup tt ON tt.label = Label'
SET @sql = @sql + 'INNER JOIN dbo.SplitStrings_Ordered('''''09-404811,10-433495,10-433575,10-423789'''', ',') AS s ON #SetValuesTable.MNum = s.MNum ORDER BY s.[Index];
The above two statements are taking long time. Especially the Last inner join which is for sorting is taking long time. I think using cross apply might greatly reduce the execution time.
回答1:
Use Cross Apply
to unpivot the result. Dynamic query query should be constructed in this format.
SELECT mnum,
label,
label_value,
[Property Type]
FROM #SetValuesTable
CROSS apply(VALUES ('la1',la1),('la2',la2),('la3',la3),
('sa1',sa1),('sa2',sa2),('sa3',sa3),
('ha1',ha1),('ha2',ha2),('ha3',ha3),
('ka1',ka1),('ka2',ka2)) cs (label, label_value)
Dynamic query
should be something like
DECLARE @label VARCHAR(max)='',
@sql NVARCHAR(max)
SELECT @label += '(' + '''' + label + ''',' + label + '),'
FROM (SELECT DISTINCT Isnull(label, '') label
FROM #labelTempTab)a
SELECT @label = LEFT(@label, Len(@label) - 1)
SET @sql= 'SELECT mnum,
label,
label_value,
[Property Type]
FROM #SetValuesTable
CROSS apply(VALUES ' + @label
+ ') cs (label, label_value) '
EXEC Sp_executesql @sql
Note : Since you are generating values list from #labelTempTab
table make sure you have all the labels present in #SetValuesTable
table
来源:https://stackoverflow.com/questions/28160637/loop-through-a-table-using-cross-apply-and-union-all-the-results