结合Select AS语句;工会没有工作

时间:2014-06-08 23:19:25

标签: sql-server

我正在尝试查询但没有成功。目标是提取所有制造商名称及其相应的URL类型。 URL只有一列(可以包含小册子,图像和规格),也就是sTxtType,这就是我使用like命令的原因。我希望以这样的格式返回数据......

nIdPub名称手册图片规格

下面的查询是提取正确的数据,但工会正在将所有数据放在这样的宣传册下的一列......

nIdPub名称手册

请参阅下面的查询。感谢。

Use dbIdwWhseLC

Select tbItemTxt.nIdPub
,   sName
,   COUNT(sTxtType) As BROCHURE
From tbItemTxt 

Inner join tbMfrReporting 
On tbItemTxt.nIdPub = tbMfrReporting.nIdPub 
Where tbItemTxt.sTxtType like '%URBRO%'
Group by sName, tbItemTxt.nIdPub 

Union

Select tbItemTxt.nIdPub
,   sName
,   COUNT(sTxtType) As PICTURE
From tbItemTxt 

Inner join tbMfrReporting 
On tbItemTxt.nIdPub = tbMfrReporting.nIdPub 
Where tbItemTxt.sTxtType like '%URPIC%'
Group by sName, tbItemTxt.nIdPub

Union

Select tbItemTxt.nIdPub
,   sName
,   COUNT(sTxtType) As SPEC
From tbItemTxt 

Inner join tbMfrReporting 
On tbItemTxt.nIdPub = tbMfrReporting.nIdPub 
Where tbItemTxt.sTxtType like '%URSPE%'
Group by sName, tbItemTxt.nIdPub

1 个答案:

答案 0 :(得分:0)

试试这个......

Select tbItemTxt.nIdPub
,   sName
,   COUNT(CASE WHEN tbItemTxt.sTxtType like '%URBRO%' THEN 1 ELSE NULL END) As BROCHURE
,   COUNT(CASE WHEN tbItemTxt.sTxtType like '%URPIC%' THEN 1 ELSE NULL END) As PICTURE
,   COUNT(CASE WHEN tbItemTxt.sTxtType like '%URSPE%' THEN 1 ELSE NULL END) As SPEC
From tbItemTxt 
Inner join tbMfrReporting  On tbItemTxt.nIdPub = tbMfrReporting.nIdPub 
Group by sName, tbItemTxt.nIdPub