I found the following link: SQL, How to Concatenate results?
However, my use case is slightly different. I have ~64 fields (allowed values Y
and N
) that are all called "QryGroup" followed by a number.
I've written a query (Query 1) to create another query (Query 2) that will return all the column names where the QryGroup has at least one value = 'Y' within a defined subset.
However, in order to do this, it's creating a large number of unions in order to get all of the values together. Query 1 is working correctly to create Query 2 in such a way that Query 2 gives me the result I'm looking for, but all in separate lines. I'd like to concatenate these into a single line so that I can put them into Query 3, to only show the columns where at least one of the records in the subset have a 'Y' value in them.
To give an example, I don't want a table with the following columns:
Item Name Item Description QryGroup1 QryGroup2 QryGroup3 QryGroup4 QryGroup5 QryGroup6 QryGroup7 QryGroup8
when only QryGroup1, 4, and 6 have a non-'N' value. Instead, I want to have
Item Name Item Description QryGroup1 QryGroup4 QryGroup6
I have the following query that, in this case, would return the values
`QryGroup1`
`QryGroup4`
`QryGroup6`
Query 2:
select 'QryGroup1' FROM OITM
WHERE QryGroup1 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup1 HAVING COUNT(*)<> 0
union all
select 'QryGroup2' FROM OITM
WHERE QryGroup2 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup2 HAVING COUNT(*)<> 0
union all
select 'QryGroup3' FROM OITM
WHERE QryGroup3 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup3 HAVING COUNT(*)<> 0
union all
select 'QryGroup4' FROM OITM
WHERE QryGroup4 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup4 HAVING COUNT(*)<> 0
union all
select 'QryGroup5' FROM OITM
WHERE QryGroup5 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup5 HAVING COUNT(*)<> 0
union all
select 'QryGroup6' FROM OITM
WHERE QryGroup6 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup6 HAVING COUNT(*)<> 0
union all
select 'QryGroup7' FROM OITM
WHERE QryGroup7 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup7 HAVING COUNT(*)<> 0
union all
select 'QryGroup8' FROM OITM
WHERE QryGroup8 <> 'N' AND oitm.itemcode in ([subset])
GROUP BY QryGroup8 HAVING COUNT(*)<> 0
I don't think that the link that I posted above will work in my situation because I'm trying to concatenate Union
ed results, instead of just normal results.
If you need more clarification, please let me know. I've also included the "Query 1" below, if it helps:
SELECT replace(replace(replace(stuff( (select cast('union all select ''' as nvarchar(max))
+ column_name +
cast(''' FROM OITM' as nvarchar(max))
+ char(13) +
cast(' WHERE ' as nvarchar(max))
+ column_name +
cast( ' <> ''N'' AND oitm.itemcode in (subset)' as nvarchar(max))
+char(13) +
cast(' GROUP BY ' as nvarchar(max))
+ column_name +
cast(' HAVING COUNT(*)<> 0 ' as nvarchar(max))
from information_schema.columns
where table_schema = 'dbo'
and table_name = 'OITM'
and column_name like 'QryGroup%%'
for xml path(''))
,1,10,''), '<', '<'), '>', '>'), '
', char(13))