سؤال

Need help in splitting values in a column. Here's what I got so far

select
recordid, productname,
case when future06 like '%418%' then (Books
case when future06 like '%421%' then (Video) else null
end
from schema.dbo.table1

Books and Video are two main products under future06 column. Instead of having future06 as the third column, I would like to have both Books and Video alongside recordid and productname.

Would life to have the output look like:

RecordID   ProductName  Books   Video
هل كانت مفيدة؟

المحلول

You almost had it:

select
    RecordID
    , ProductName
    , Books = case when future06 like '%418%' then future06 else null end
    , Video = case when future06 like '%421%' then future06 else null end
from
    schema.dbo.table1

نصائح أخرى

select
recordid, productname,
case when future06 like '%418%' then future06 else null end as Books,
case when future06 like '%421%' then future06 else null end as Video
from schema.dbo.table1


select
recordid, productname,
COUNT(case when future06 like '%418%' then 1 else 0 end) as NoOfBooks,
COUNT(case when future06 like '%421%' then 1 else 0 end) as NoOfVideo
from schema.dbo.table1 
group by recordid, productname

select
recordid, productname,
COUNT(case when future06 like '%418%' then 1 else 0 end) as NoOfBooks,
COUNT(case when future06 like '%421%' then 1 else 0 end) as NoOfVideo, 
COUNT(*) as Total 
from schema.dbo.table1 
group by recordid, productname
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top