2013-03-27 88 views
0

需要幫助分割列中的值。這裏是我到目前爲止
將值拆分爲sql server中的case語句的列

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

BooksVideo正在future06列兩個主要產品。 而不是有future06作爲第三列,我想有 BooksVideo旁邊recordidproductname

將生活有輸出的樣子:

RecordID ProductName Books Video 

回答

0

你幾乎擁有了:

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

你這麼多,現在如果它只獲得銷售到其各自欄目的數量?任何建議? – user2146490 2013-03-27 12:36:00

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

非常感謝你的幫助,現在怎麼會和我哪裏會包括計數功能並添加額外的欄目,稱爲總計添加書籍和視頻? – user2146490 2013-03-27 12:54:11

+0

檢查第二個查詢。 – 2013-03-27 12:59:57

+0

出現錯誤:
集合函數或GROUP BY子句 – user2146490 2013-03-27 13:05:53