Count()保持清零

时间:2016-06-27 20:47:45

标签: sql sql-server count aggregate-functions

我正在尝试做一个count语句确实显示每个类别有多少特定版本。问题是,只要版本超过一个,结果就会清零。

例如,如果一个类别中有三个,其中两个具有一个版本而另一个具有另一个版本,则具有两个实例的版本的计数将在结果中显示“0”而不是“2”。当该类别只有每个版本中的一个时,它的工作完美。只有当一个版本的实例有多个时才会出现。

有谁知道如何解决?我假设它在我的案例陈述中,但我不确定。

我的代码如下。

select Category, Count(Case When Version = 'Offer' then 1 end) as Offers, Count(Case When Version = 'No Offer' then 1 end) as [No Offers] from (
Select *, ROW_NUMBER() over (Partition By Category order by version) as GroupOrder, DENSE_RANK() over (order by category) as GroupNo From Input 
) A
where groupno in 
(
select groupno from (
Select *, ROW_NUMBER() over (Partition By Category order by version) as GroupOrder, DENSE_RANK() over (order by category) as GroupNo From Input 
) A where GroupOrder > 1) and ID not in 
(
select ID from
(
Select *, ROW_NUMBER() over (Partition By Category, Version order by version) as GroupOrder, DENSE_RANK() over (order by category, Version) as GroupNo From Input 
) A
where groupno in 
(
select groupno from 
(
Select *, ROW_NUMBER() over (Partition By Category, Version order by version) as GroupOrder, DENSE_RANK() over (order by category, Version) as GroupNo From Input 
) A where GroupOrder > 1) 
)
group by Category
order by  Category 

1 个答案:

答案 0 :(得分:0)

使用CTE's解决此类问题我取得了很大成功。这可能会为您提供所需的答案。

;WITH CTE
  AS (SELECT Category,
             CASE WHEN [Version] = 'Offer' THEN 1
                  ELSE 0
               END AS Offers,
             CASE WHEN [Version] = 'No Offer' THEN 1
                 ELSE 0
               END AS [No Offers]
        FROM
          (SELECT *, ROW_NUMBER() OVER (PARTITION BY Category ORDER BY version) AS GroupOrder
                   , DENSE_RANK() OVER (ORDER BY category) AS GroupNo
            FROM Input) A
        WHERE GroupNo IN
          (SELECT GroupNo
             FROM (SELECT *
                        , ROW_NUMBER() OVER (PARTITION BY Category ORDER BY version) AS GroupOrder
                        , DENSE_RANK() OVER (ORDER BY category) AS GroupNo
                    FROM   Input) A
             WHERE   GroupOrder > 1)
               AND ID NOT IN
                 (SELECT ID
                   FROM
                     (SELECT *
                           , ROW_NUMBER() OVER (PARTITION BY Category, Version ORDER BY version) AS GroupOrder
                           , DENSE_RANK() OVER (ORDER BY category, Version) AS GroupNo
                       FROM   Input) A
                   WHERE GroupNo IN
                     (SELECT GroupNo
                       FROM 
                         (SELECT *
                               , ROW_NUMBER() OVER (PARTITION BY Category, Version ORDER BY version) AS GroupOrder
                               , DENSE_RANK() OVER (ORDER BY category, Version) AS GroupNo
                           FROM Input) A
                       WHERE   GroupOrder > 1)))
  SELECT Category,
         SUM(Offers) AS Offers,
         SUM([No Offers]) AS Offers
    FROM [CTE]
    GROUP BY Category
    ORDER BY Category