简体   繁体   中英

splitting values to a column with case statement in sql server

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
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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM