SQL将每月查询报告拆分为每日

时间:2017-05-17 14:31:40

标签: sql sql-server date split report

我有一个查询,在给定日期,它找到了商店中所有材料的数量。 例如,对于2017年1月5日和1月6日的日期,它将为我提供商店中的每种材料

 material   | Initial QTY | Initial Volume | Changes_QTY  | changes_volume | finalQTY | final_volume

 material1  |    10       |   100          | 2            |  20            |         8|   80

我想要同样的想法(初始和最终),但他们之间的时间段每天分开,而不是一起。

我的查询是

SELECT mtr.code
   , mtr.DESCRIPTION
   , mtr.volume
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.trndate < {@VFrom}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) Initial_qty
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.trndate <= {@VFrom}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) * mtr.volume initial_volume
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.TRNDATE >= {@VFrom}
                AND i.trndate <= {@VTo}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) changes_qty
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.TRNDATE >= {@VFrom}
                AND i.trndate <= {@VTo}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) * mtr.volume volume_changes
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.trndate <= {@VTo}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) final_qty
   , Isnull((SELECT Sum(i.PRIMARYQTY * i.INPUTQUANTMODE) - Sum(i.PRIMARYQTY * i.OUTPUTQUANTMODE)
             FROM   itemtrans i
             WHERE  comid = mtr.comid
                AND i.iteid = mtr.id
                AND i.trndate <= {@VTo}
                AND i.stoid = 32
                AND i.comid = 21
             GROUP  BY i.iteid), 0) * mtr.volume final_volume
FROM   material mtr
       INNER JOIN STOREFINDATA STF ON STF.MASTERID = MTR.ID
WHERE  mtr.comid = 21
   AND mtr.igtid = 100
   AND stf.stoid = 32 

我希望我能让自己清楚地帮助我。 感谢

0 个答案:

没有答案