如何组合这两个查询?

时间:2018-04-09 11:05:54

标签: sql sql-server

我有一张表格中有产品,以及添加产品或离开产品目录的日期。和qprod表是一个日历表,所有日期都是2018-2025

我想制作一个图表,其中包含每个月的新产品数量和剩余产品数量。当我自己运行其中一个查询时,它会给我一个月份以及添加/离开目录的产品数量。
像这样:

Year |Month |NewProd  
2018 | 1    | 5  
2018 | 2    | 10  
2018 | 3    | 2

但我希望这些数量在一个表中,所以一列包含newprods,另一列包含oldprods。
喜欢这个

Year |Month |NewProd | OldProd   
2018 | 1    | 5      | 1  
2018 | 2    | 10     | 0  
2018 | 3    | 2      | 3  

有人可以帮助我将这两个查询合并到一个查询中吗?

我正在使用Microsoft Sql Server管理工作室

Select year, month, Count(prodid) as NewProd
from (select DATEPART("yyyy", datum) AS year,
             DATEPART("mm", datum)   AS month,
             DatePart("dd", datum)   AS Day,
             DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
       from qProds
      ) dates left join
      Products
      on products.lnewprod = dates.Monthdate 
 Group By year, month
 Order By Year, month

并且

Select year, month, Count(prodid) as Old_prod
from (select DATEPART("yyyy", datum) AS year,
             DATEPART("mm", datum)   AS month ,
             DatePart("dd", datum)   AS Day,
             DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
      from qProds
     ) dates left join
     products
     on products.loldprod = dates.Monthdate 
Group By year, month
Order By Year, month

3 个答案:

答案 0 :(得分:0)

我不确定onChange是什么。但是以下查询应该返回您想要的结果:

qprod

请注意,如果一个月没有" old"或"新"记录,它不会在结果集中。这可以通过日历表修复。我不清楚select year(v.dte), month(v.dte), sum(v.isnew), sum(v.isold) from products p cross apply (values (p.lnewprod, 1, 0), (p.loldprod, 0, 1) ) v(dte, isnew, isold) group by year(v.dte), month(v.dte) order by year(v.dte), month(v.dte); 就是这样一张桌子。

答案 1 :(得分:0)

试试这个;

select A.yr as Year,A.mn as Month, A.NewProd, B.Old_prod from (
Select year as yr, month as mn, Count(prodid) as NewProd
from (select DATEPART("yyyy", datum) AS year,
             DATEPART("mm", datum)   AS month,
             DatePart("dd", datum)   AS Day,
             DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
       from qProds
      ) dates left join
      Products
      on products.lnewprod = dates.Monthdate 
 Group By year, month
 Order By Year, month)A
full join 
select * from 
(Select year as yrr, month as mnt, Count(prodid) as Old_prod
from (select DATEPART("yyyy", datum) AS year,
             DATEPART("mm", datum)   AS month ,
             DatePart("dd", datum)   AS Day,
             DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
      from qProds
     ) dates left join
     products
     on products.loldprod = dates.Monthdate 
Group By year, month
Order By Year, month)B
on (A.yr = B.yrr and A.mn = B.mnt and A.NewProd = B.Old_prod)

答案 2 :(得分:0)

我的建议是将你的两个陈述结合起来然后总结计数以获得你想要的结果:

SELECT year, month, sum(NewProd) as NewProd, sum(OldProd) as OldProd
FROM (
    Select year, month, Count(prodid) as NewProd, 0 as OldProd
    from (select DATEPART("yyyy", datum) AS year,
                 DATEPART("mm", datum)   AS month,
                 DatePart("dd", datum)   AS Day,
                 DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
           from qProds
          ) dates left join
          Products
          on products.lnewprod = dates.Monthdate 
     Group By year, month
    UNION
    Select year, month, 0 as NewProd, Count(prodid) as OldProd
    from (select DATEPART("yyyy", datum) AS year,
                 DATEPART("mm", datum)   AS month ,
                 DatePart("dd", datum)   AS Day,
                 DATEFROMPARTS(YEAR(datum), MONTH(datum), Day(datum)) as MonthDate 
          from qProds
         ) dates left join
         products
         on products.loldprod = dates.Monthdate 
    Group By year, month) as combined_counts
GROUP BY year, month
ORDER BY year, month

未经测试。