我在子查询查询中使用它时尝试将行转换为列:
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
我收到此错误:
当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。
当我单独使用它时,它可以工作:
select
max(case when serName = 'Capacity' then serStatus end) Capacity,
max(case when serName = 'Parking' then serStatus end) Parking
from
tblService
where
serBqtID = 1
结果:
Capacity Parking
-------- -------
101-200 51-100
为什么在子查询中没有将多行转换为列?
答案 0 :(得分:1)
您正尝试在选择列表中将两列作为一列返回。这不起作用。我不是SQL Server专家,但使用Oracle至少会有三个选项。
我认为所有人都应该使用SQL Server。选项3最接近现在的情况。
编辑:试试这个:
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