2013-03-27 70 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