分组返回多行

时间:2018-11-09 15:36:30

标签: sql sql-server

我下面的查询返回多行,而不是合计计数。我在做什么错了。

select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
       Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy, 
       count(*) userCount
from CompletesCur OS 
   left outer join  Sales s on S.Cust = ID
WHERE s.ReturnYear = 2017
  and s.MediaType = 'Online'
  and os.ReturnYearFiled = 2017
  and s.ReturnYear = s.TransactionTaxYear2
  and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate

结果:

Paid_Free   Status          Busy   Usercount
Paid      Returning Yr3+     2H      2
Paid      Returning Yr3+     2H      1
Paid      Returning Yr3+     1H      1
Paid      Returning Yr3+     1H      1
Paid      Returning Yr2+     2H      2
Paid      Returning Yr2+     2H      2

请求的结果:

Paid_Free    Status            Busy    UserCount
Paid        Returning Yr3+      2H       3
Paid        Returning Yr3+      1H       2
Paid        Returning Yr2+      2H       4  

5 个答案:

答案 0 :(得分:1)

您可以尝试以下操作

'HttpResponse' does not contain a definition for 'ClearHeaders'
'HttpResponse' does not contain a definition for 'ClearContent'
'HttpResponse' does not contain a definition for 'Expires'
'HttpResponse' does not contain a definition for 'AddHeader'
'HttpResponse' does not contain a definition for 'BinaryWrite'
'HttpContext' does not contain a definition for 'Current'

答案 1 :(得分:1)

根据您在输出中的期望分组

with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy, 
count(*) userCount
     from CompletesCur OS 
   left outer join  Sales s on S.Cust = ID
                  WHERE s.ReturnYear = '2017'
                  and MediaType = 'Online'
                  and os.ReturnYearFiled = 2017
                  and s.ReturnYear = s.TransactionTaxYear2
                  and s.ProductGroup in ('Fed','State','Phone Support', 'Import')

                  GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
  group by Paid_Free,Busy,Status

答案 2 :(得分:1)

您需要group by select中的非聚合表达式(通常那些)。重复复杂的表达式会很麻烦,这就是为什么我喜欢在子查询中定义新字段或使用`apply:

select v.Paid_Free, Status, v.Busy, 
       count(*) as userCount
from CompletesCur OS left join
     Sales s
     on S.Cust = ID cross apply
     (values ( case when s.Customer is not null then 'Paid' else 'Free' END,
               case when CompleteDate < '2018-04-01' then '1H' else '2H' end
             )
     ) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and  -- is this really a string?
      MediaType = 'Online' and   -- what table is this from?
      os.ReturnYearFiled = 2017 and
      s.ReturnYear = s.TransactionTaxYear2 and
      s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;

其他说明:

  • 您应限定所有所有列名,包括StatusMediaType
  • ReturnYear是数字还是字符串?如果是数字,则不要将引号用作比较值。
  • 我将日期常数更改为标准的YYYY-MM-DD格式。

答案 3 :(得分:0)

您应该

group by 
  case when s.Customer is not null then 'Paid' else 'Free' END,
  Status,
  case when CompleteDate < '4/1/2018' then '1H' else '2H' end

否则,您仍将创建由不同的客户和日期组成的组。

答案 4 :(得分:0)

  

我在做什么错了?

虽然其他答案解释了解决问题的方法,但是您特别询问了做错了什么,我希望这个答案可以帮助您理解。.

对此更改进行选择,您会看到:

select 
   case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
   Status,
   case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy, 
   --my additions
   s.customer,
   CompleteDate,
   count(*) userCount

没有更多重复的行! (我没有压缩重复项,而是添加了实际上已分组的列,因此您仍然可以获得所有意外行,但是很容易看到它们出现的原因。)

客户/状态/完成日期的组合是唯一的,但是您可以在对数据进行分组后对其进行转换。转换此数据的结果是它变得不太精确-2018年4月1日之前有数千个日期,但您有效地将其转换为布尔值(is-before或is-after)。日期为3/29 / 2018、3 / 28/2018等的所有行,并且以前是唯一的,现在都变成相同的值,因此 apparent 重复

以这种方式隐藏信息/丢失精度,创建重复项