我想你可以尝试使用 Case 语句更改 Select 语句中的 ServiceType
SELECT ItemId,....., ServiceType =
CASE ServiceTypes
WHEN 'VMF' THEN 'True'
WHEN 'SRR' THEN 'False'
ELSE ''
END, InLimit, IpConcantenated
from ....
where ...
如果你想重复该列,你可以反刍两次
SELECT ItemId,.....,
ServiceTypeVMF =
CASE ServiceType
WHEN 'VMF' THEN 'True'
WHEN 'SRR' THEN 'False'
ELSE ''
END,
ServiceTypeSRR =
CASE ServiceType
WHEN 'VMF' THEN 'False'
WHEN 'SRR' THEN 'True'
ELSE ''
END,
InLimit,
IpConcantenated
from ....
where ...
好吧,前面的case语句会获取你的多行,但我认为如果你想将行转换为列,假设你提到的只有2行'VMF'和'SNR',这应该是你的sql语句
with cte as
(select ItemID, Upccode, ..............,
row_number() over(partition by ItemID, Upccode order by ServiceType) As rn
,ServiceType
,MinLimit
,IsContracted
from Items)
select ItemID, UpcCode,
max(case when rn = 1 then ServiceType end) as ServiceType1,
max(case when rn = 2 then ServiceType end) as ServiceType2,
,MinLimit
,IsContracted
from cte
group by ItemId, UpcCode,.........,MinLimit,IsContracted;