选择按产品名称分组的年度销售数据,确保仅在1年内获得产品

时间:2016-08-05 23:27:17

标签: mysql sql

所以我试图获得年复一年的数据。虽然我可以做这样的事情

SELECT tbl_products.product_name, SUM(tbl_invoice_details.quantity- IFNULL(tbl_invoice_details.quantity_returned,0)), 
MONTH(tbl_invoice.invoice_date) as `month`, sort_order
FROM
   tbl_invoice_details
   INNER JOIN tbl_invoice ON tbl_invoice_details.invoice_ID = tbl_invoice.invoice_ID
   INNER JOIN tbl_products ON tbl_products.product_ID = tbl_invoice_details.product_ID
WHERE tbl_invoice.invoice_date BETWEEN  '2016-01-01' AND '2016-07-01'
GROUP BY  `month`, product_name, sort_order
order by `month`, sort_order

并运行两次(每年一次)。但是,如果有一年销售的产品而另一年没有销售,那么在创建图表时会出现问题。我可以在后端的代码中修复它,但似乎会浪费很多周期。我也可以在两年内对产品进行select distinct,并在后端使用该结果对每个产品运行查询,但这将是大量的数据库调用。有谁知道在mySql中更好的方法吗?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以更改where子句:

SELECT p.product_name, SUM(id.quantity -  IFNULL(id.quantity_returned,0)), 
       YEAR(i.invoice_date) as ayyyy, MONTH(i.invoice_date) as `month`, sort_order
FROM tbl_invoice_details id INNER JOIN
     tbl_invoice i
     ON id.invoice_ID = i.invoice_ID INNER JOIN
     tbl_products p
     ON p.product_ID = id.product_ID
WHERE month(i.invoice_date) IN (1, 2, 3, 4, 5, 6) AND
      year(i.invoice_date) IN (2015, 2016)
GROUP BY  YEAR(i.invoice_date), `month`, product_name, sort_order
ORDER BY yyyy, `month`, sort_order;

编辑:

您可能希望为这些值添加两列:

SELECT p.product_name,
       SUM(CASE WHEN YEAR(i.voice_date) = 2015 THEN id.quantity -  COALESCE(id.quantity_returned, 0) END)  as val_2015,
       SUM(CASE WHEN YEAR(i.voice_date) = 2016 THEN id.quantity -  COALESCE(id.quantity_returned, 0) END)  as val_2016,
       MONTH(i.invoice_date) as `month`, sort_order
FROM tbl_invoice_details id INNER JOIN
     tbl_invoice i
     ON id.invoice_ID = i.invoice_ID INNER JOIN
     tbl_products p
     ON p.product_ID = id.product_ID
WHERE month(i.invoice_date) IN (1, 2, 3, 4, 5, 6) AND
      year(i.invoice_date) IN (2015, 2016)
GROUP BY  YEAR(i.invoice_date), `month`, product_name, sort_order
ORDER BY yyyy, `month`, sort_order;

答案 1 :(得分:0)

如果您没有日期表并且无法创建日期表,那么一堆工会可能会有所帮助。例如

/*
DROP TABLE TBL_PRODUCTS;
CREATE TABLE TBL_PRODUCTS(PRODUCT_ID INT,PRODUCT_NAME VARCHAR(10));
INSERT INTO TBL_PRODUCTS VALUES
(1,'ABC'), (2,'DEF');
CREATE TABLE TBL_INVOICE(INVOICE_ID INT,INVOICE_DATE DATE);
INSERT INTO TBL_INVOICE VALUES
(1,'2015-06-01'), (2,'2015-07-01'),(3,'2015-08-01'),
(4,'2016-01-01'), (5,'2016-02-01'),(6,'2016-03-01');
DROP TABLE TBL_INVOICE_DETAILS;
CREATE TABLE TBL_INVOICE_DETAILS (INVOICE_ID INT,PRODUCT_ID INT,QUANTITY INT,QUANTITY_RETURNED INT);
INSERT INTO TBL_INVOICE_DETAILS VALUES
(1,1,10,NULL), (2,1,10,NULL),(3,2,10,NULL),
(4,1,10,NULL), (5,1,10,NULL),(6,2,10,NULL);
*/
USE SANDBOX;
SELECT S.product_name, SUM(S.quantity -  IFNULL(S.quantity_returned,0)) NET, 
       S.yyyy, S.`month`
FROM
(
SELECT p.product_name, id.quantity ,id.quantity_returned, 
       YEAR(i.invoice_date) as yyyy, MONTH(i.invoice_date) as `month`
FROM       tbl_invoice_details id 
INNER JOIN tbl_invoice i  ON id.invoice_ID = i.invoice_ID 
INNER JOIN tbl_products p ON p.product_ID = id.product_ID
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,1 FROM TBL_PRODUCTS P 
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,2 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,3 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,4 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,5 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,6 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,7 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,8 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,9 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,10 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,11 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2015,12 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,1 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,2 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,3 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,4 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,5 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,6 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,7 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,8 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,9 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,10 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,11 FROM TBL_PRODUCTS P
UNION   SELECT  P.PRODUCT_NAME,0,0,2016,12 FROM TBL_PRODUCTS P

) S
WHERE   S.YYYY IN (2015,2016)
GROUP   BY S.PRODUCT_NAME,S.YYYY,S.`MONTH`

,即使没有像这样的发票,也会为每个月的每件商品返回结果集

+--------------+------+------+-------+
| product_name | NET  | yyyy | month |
+--------------+------+------+-------+
| ABC          |    0 | 2015 |     1 |
| ABC          |    0 | 2015 |     2 |
| ABC          |    0 | 2015 |     3 |
| ABC          |    0 | 2015 |     4 |
| ABC          |    0 | 2015 |     5 |
| ABC          |   10 | 2015 |     6 |
| ABC          |   10 | 2015 |     7 |
| ABC          |    0 | 2015 |     8 |
| ABC          |    0 | 2015 |     9 |
| ABC          |    0 | 2015 |    10 |
| ABC          |    0 | 2015 |    11 |
| ABC          |    0 | 2015 |    12 |
| ABC          |   10 | 2016 |     1 |
| ABC          |   10 | 2016 |     2 |
| ABC          |    0 | 2016 |     3 |
| ABC          |    0 | 2016 |     4 |
| ABC          |    0 | 2016 |     5 |
| ABC          |    0 | 2016 |     6 |
| ABC          |    0 | 2016 |     7 |
| ABC          |    0 | 2016 |     8 |
| ABC          |    0 | 2016 |     9 |
| ABC          |    0 | 2016 |    10 |
| ABC          |    0 | 2016 |    11 |
| ABC          |    0 | 2016 |    12 |
| DEF          |    0 | 2015 |     1 |
| DEF          |    0 | 2015 |     2 |
| DEF          |    0 | 2015 |     3 |
| DEF          |    0 | 2015 |     4 |
| DEF          |    0 | 2015 |     5 |
| DEF          |    0 | 2015 |     6 |
| DEF          |    0 | 2015 |     7 |
| DEF          |   10 | 2015 |     8 |
| DEF          |    0 | 2015 |     9 |
| DEF          |    0 | 2015 |    10 |
| DEF          |    0 | 2015 |    11 |
| DEF          |    0 | 2015 |    12 |
| DEF          |    0 | 2016 |     1 |
| DEF          |    0 | 2016 |     2 |
| DEF          |   10 | 2016 |     3 |
| DEF          |    0 | 2016 |     4 |
| DEF          |    0 | 2016 |     5 |
| DEF          |    0 | 2016 |     6 |
| DEF          |    0 | 2016 |     7 |
| DEF          |    0 | 2016 |     8 |
| DEF          |    0 | 2016 |     9 |
| DEF          |    0 | 2016 |    10 |
| DEF          |    0 | 2016 |    11 |
| DEF          |    0 | 2016 |    12 |
+--------------+------+------+-------+
48 rows in set (0.02 sec)