select newColumn = stuff(
(
select '' +[ATTR_VAL]
from [NDC_ATTR]
where item_id = 185836
and field_id in (144, 225)
order by 1 desc
for xml path (''), type).value('.','varchar(max)')
,1,0,'')
或更多項:
select
t.item_id
, newColumn = stuff(
(
select '' +[ATTR_VAL]
from [NDC_ATTR] as i
where i.item_id = t.item_id
and i.field_id in (144, 225)
order by 1 desc
for xml path (''), type).value('.','varchar(max)')
,1,0,'')
from [NDC_ATTR] as t
where t.item_id in (...)
group by t.item_id
任選:添加一個分隔符:(注意0改變到定界符3210的長度爲stuff
的第三參數)
select
t.item_id
, newColumn = stuff(
(
select ';' +[ATTR_VAL]
from [NDC_ATTR] as i
where i.item_id = t.item_id
and i.field_id in (144, 225)
order by 1 desc
for xml path (''), type).value('.','varchar(max)')
,1,1,'')
from [NDC_ATTR] as t
where t.item_id in (...)
group by t.item_id
你的表沒有這些列,所以我只是沒有遵循你的查詢和你想要做的事情之間的關係。 –
你試圖連接表或...你的'from'在哪裏? – SqlZim
看看:http://stackoverflow.com/a/545672/7187145 – Masoud