Reputation:
I'm trying to convert rows into columns when using this in a subquery query like:
select distinct
bqtID, bqtName, bqtCity, bqtStatus, bqtManagerName,
(select
max(case when serName = 'Capacity' then serStatus end) Capacity,
max(case when serName = 'Parking' then serStatus end) Parking
from
tblService
where
serBqtID = bqtID),
from
View_BanquetList
where
bqtID = 1
I get this error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
While when I used it separately then it works:
select
max(case when serName = 'Capacity' then serStatus end) Capacity,
max(case when serName = 'Parking' then serStatus end) Parking
from
tblService
where
serBqtID = 1
Results:
Capacity Parking
-------- -------
101-200 51-100
Why is it not converting multiple rows into columns in sub-query?
Upvotes: 0
Views: 2714
Reputation: 6278
You are trying to return two columns as one in the select list. That doesn't work. I'm not an expert on SQL Server, but with Oracle there would be at least three options.
I think all should work with SQL Server as well. Option 3 is closest to what you have now.
EDIT: try this:
select distinct
v.bqtID, v.bqtName, v.bqtCity, v.bqtStatus, v.bqtManagerName,
t.Capacity, t.Parking
from
(select
serBqtID,
max(case when serName = 'Capacity' then serStatus end) Capacity,
max(case when serName = 'Parking' then serStatus end) Parking
from
tblService
group by
serBqtID) t
inner join
View_BanquetList v on t.serBqtID = v.bqtID
where
v.bqtID = 1
Upvotes: 1