对于不匹配的SQL Server,如何返回0的总和

时间:2018-12-21 18:55:50

标签: sql sql-server tsql

我有一个查询,可以将销售汇总到案例存储桶中,效果很好,除了 我需要它为没有销售的存储桶返回0

这是针对MS SQL Server 2016的,我已经搜索并发现了一些建议使用Coalesce的类似问题,但我不知道如何将其应用于我的查询。

表数据如下所示:
Surveyid,projid,surveyperiod,quadsold,averagebaseprice2
65374、12079、2018-12-01、4、301805
70641,10366,2018-12-01,9,584390
等等

select t.range, sum(s.quartersold) as qtrsold, surveydate
from (
  select concat(datepart(quarter,surveyperiod), datepart(year,surveyperiod)) as surveydate, quartersold, case
   when averagebaseprice2 between 0 and 199999.99 then 'Less than $200,000'
   when averagebaseprice2 between 200000 and 224999.99 then '$200,000 - $224,000'
   when averagebaseprice2 between 225000 and 249999.99 then '$225,000 - $249,999'
   when averagebaseprice2 between 250000 and 274999.99 then '$250,000 - $274,999'
   when averagebaseprice2 between 275000 and 299999.99 then '$275,000 - $299,999'
   when averagebaseprice2 between 300000 and 324999.99 then '$300,000 - $324,999'
   when averagebaseprice2 between 325000 and 349999.99 then '$325,000 - $349,999'
   when averagebaseprice2 between 350000 and 374999.99 then '$350,000 - $374,999'
   when averagebaseprice2 between 375000 and 399999.99 then '$375,000 - $399,999'
   when averagebaseprice2 between 400000 and 424999.99 then '$400,000 - $424,999'
   when averagebaseprice2 between 425000 and 449999.99 then '$425,000 - $449,999'
   when averagebaseprice2 between 450000 and 474999.99 then '$450,000 - $474,999'
   when averagebaseprice2 between 475000 and 499999.99 then '$475,000 - $499,999'
   when averagebaseprice2 between 500000 and 599999.99 then '$500,000 - $599,999'
   when averagebaseprice2 between 600000 and 699999.99 then '$600,000 - $699,999'
   when averagebaseprice2 between 700000 and 799999.99 then '$700,000 - $799,999'
   when averagebaseprice2 between 800000 and 899999.99 then '$800,000 - $899,999'
   when averagebaseprice2 between 900000 and 999999999.99 then '$900,000 and greater'
   end as range
  from surveys s
  where datepart(year,s.surveyperiod) = 2018 
) t
group by t.range, surveydate

(对不起,我不知道如何更好地格式化) 与上面的查询,我得到:
        范围qtrsold测量日期
    $ 250,000-$ 274,999 8 12018
    $ 275,000-$ 299,999 8 12018
    $ 300,000-$ 324,999 59 12018
    $ 325,000-$ 349,999 22 12018
    $ 350,000-$ 374,999 139 12018
    $ 375,000-$ 399,999 82 12018
    $ 400,000-$ 424,999 103 12018
    $ 425,000-$ 449,999 92 12018
    $ 450,000-$ 474,999 142 12018
    $ 475,000-$ 499,999 144 12018
    $ 500,000-$ 599,999 314 12018
    600,000美元-699,999美元64 2018年1月
    700,000-799,999美元80 12018
    $ 800,000-$ 899,999 18 12018
    900,000美元及以上6 12018

我想得到:
    范围qtrsold测量日期
        少于$ 200,000 0 12018
        200,000美元-224,000美元0 12018
        $ 225,000-$ 249,999 0 12018

    $ 250,000-$ 274,999 8 12018
    $ 275,000-$ 299,999 8 12018
    $ 300,000-$ 324,999 59 12018
    $ 325,000-$ 349,999 22 12018
    $ 350,000-$ 374,999 139 12018
    $ 375,000-$ 399,999 82 12018
    $ 400,000-$ 424,999 103 12018
    $ 425,000-$ 449,999 92 12018
    $ 450,000-$ 474,999 142 12018
    $ 475,000-$ 499,999 144 12018
    $ 500,000-$ 599,999 314 12018
    600,000美元-699,999美元64 2018年1月
    700,000-799,999美元80 12018
    $ 800,000-$ 899,999 18 12018
    900,000美元及以上6 12018

非常感谢您的协助。

1 个答案:

答案 0 :(得分:2)

您需要分别生成范围,然后使用left join引入数据。目前尚不清楚列的来源,因此以下是一个猜测:

select (datename(quarter, s.surveyperiod) + datename(year, s.surveyperiod)) as surveydate,
       v.range,
       coalesce(sum(?.quartersold), 0) as qtrsold
from (values ('Less than $200,000', 0 200000),
             ('$200,000 - $224,000', 0, 225,000),
             ('$225,000 - $249,999', 225000, 250000),
             . . .
     ) v(range, lo, hi) left join
     surveys s 
     on s. averagebaseprice2 >= v.lo and s.averagebaseprice2 < v.hi left join
     projects p
     on s.projid = p.projid
where s.surveyperiod >= '2018-01-01' and s.surveyperiod < '2019-01-01'
group by (datename(quarter, s.surveyperiod) + datename(year, s.surveyperiod)) as surveydate,
         v.range  ;