日期:2014-05-18  浏览次数:20580 次

求帮改个SQL语句
SQL code

ALTER PROCEDURE [dbo].[UP_AdvCount]
    @UserId bigint 
AS
BEGIN
    SELECT  
    COUNT(distinct ADID) as total,
    SUM(CASE WHEN ADState_Id = 999 THEN 1 ELSE 0 END) AS showCount,
    SUM(CASE WHEN ADState_Id = 0 THEN 1 ELSE 0 END) AS noReviewCount,
    SUM(CASE WHEN ADState_Id = 3 THEN 1 ELSE 0 END) AS pauseCount
    From SK_ADVInfoList_VI WHERE DELETESTATE=0 AND AdUser_Id = @UserId
END


是统计信息
表结构就不给了,太多,看起来麻烦
total统计 起来数据是对了,但是,其它根据状态 来统计 的不行,
状态和 ADID是多对一的关系 ,但 我要的是
显示的信息是这样的
名称 平台 状态
----------------------------------
应用1 平台1 未审核
  平台2 审核通过
-----------------------------------
应用2 平台1 审核通过
  平台2 审核通过
------------------------------------
应用3 平台1 未审核
  平台2 未审核


这样,我应该统计的结果 是

total(应用) =3
showCount(通过) = 2
noReviewCount(未审核)=2

但出来 的结果 确是
total(应用) =3
showCount(通过) = 3
noReviewCount(未审核)=3

求高手 帮我改下,怎么好合适

------解决方案--------------------
SQL code
ALTER PROCEDURE [dbo].[UP_AdvCount]
    @UserId bigint 
AS
BEGIN
    SELECT  
    COUNT(distinct ADID) as total,
    COUNT(distinct  CASE WHEN ADState_Id = 999 THEN 平台 ELSE NULL END) AS showCount,
    COUNT(distinct CASE WHEN ADState_Id = 0 THEN 平台 ELSE NULL END) AS noReviewCount,
    COUNT(distinct CASE WHEN ADState_Id = 3 THEN 平台 ELSE NULL END) AS pauseCount
    From SK_ADVInfoList_VI WHERE DELETESTATE=0 AND AdUser_Id = @UserId
END