增加总销售额

时间:2014-10-17 21:47:13

标签: sql sql-server

我在获得所售商品总数方面遇到了问题。下面的脚本向我显示了总销售额,但没有将它们总结。我得到它总结,但它列出了重复的发票,所以我在invoice.no之前输入“distinct”。我无法弄清楚我在哪里搞砸了。请注意,我之前没有SQL培训,我在这里只读到了其他读者的问题。请帮忙。提前谢谢。

SELECT DISTINCT 
    sh.invoice_no, sr.item_id, sr.item_desc, sr.last_sale, sr.SQ, 
    sr.DQ, sr.AQ, sr.VQ,sr.PQ, sr.iQOH, sr.avg_MAC, si.cost, 
    sr.PT, si.item_id AS Item, SUM(sh.qty_shipped) AS QTY_Shipped
FROM iml_view_item_stock_review_nh as sr 
INNER JOIN p21_view_inventory_supplier as si ON sr.item_id = si.item_id 
INNER JOIN p21_sales_history_view as sh ON sr.item_id = sh.item_id
WHERE (sr.item_id LIKE 'IEI%') 
  AND (sr.PT = 'R') 
  AND (sh.invoice_date > CONVERT(DATETIME, '2013-10-17 00:00:00', 102)) 
GROUP BY 
    sr.item_id, sr.item_desc, sr.last_sale, 
    sr.SQ, sr.DQ, sr.AQ, sr.VQ, 
    sr.PQ, sr.iQOH, sr.avg_MAC, si.cost, 
    sr.PT, si.item_id, sh.invoice_no 

以下是数据样本。我省略了其他一些专栏。希望这可以帮助。

|invoice_no |   item_id    |  QTY_Shipped |
|-----------------------------------------|
| 1000616   |  IEI0803000F |       4      |
| 1001289   |  IEI0803000F |       2      |
| 1018242   |  IEI0803000F |       2      |
| 1001224   |     IEI212I  |       2      |
| 1002265   |     IEI212I  |       2      |
| 1003547   |     IEI212I  |       2      |

预期输出类似于:

|   item_id    |  QTY_Shipped |
|--------------|--------------|
| IEI0803000F  |       8      |
|   IEI212I    |       6      |

对不起,如果我没有太多的知识。我的公司在1月份派我去接受培训和认证,很快我就会帮助别人。提前致谢。

1 个答案:

答案 0 :(得分:0)

事实证明我的查询是正确的。我对数据感到震惊。

SELECT    sr.item_id, sr.item_desc, sr.last_sale, sr.SQ, 
          sr.DQ, sr.AQ, sr.VQ,sr.PQ, sr.iQOH, sr.avg_MAC, 
          si.cost, sr.PT, si.item_id AS Item, SUM(sh.qty_shipped) AS QTY_Shipped
FROM      iml_view_item_stock_review_nh as sr INNER JOIN
          p21_view_inventory_supplier as si ON sr.item_id = si.item_id INNER JOIN
          p21_sales_history_view as sh ON sr.item_id = sh.item_id
WHERE     (sr.item_id LIKE 'IEI%') AND (sr.PT = 'R') AND 
          (sh.invoice_date > DATEADD(YEAR,-1,GETDATE())) 


GROUP BY          sr.item_id, sr.item_desc, sr.last_sale, 
                  sr.SQ, sr.DQ, sr.AQ, sr.VQ, 
                  sr.PQ, sr.iQOH, sr.avg_MAC, si.cost, 
                  sr.PT, si.item_id