我想创建一个月度声明(MySQL)

时间:2013-02-14 11:45:32

标签: mysql join null group-by

我想创建月度声明

Tables
Product (id, name, price)
Invoice (id, date)
Invoice_item (id, invoice_id, product_id, quantity)

示例数据:

product
id | name  | price
1  | apple | 10
2  | beer  | 20
3  | shoe  | 40

invoice
id | date
1  | 2013-01-21
2  | 2013-01-29
3  | 2013-02-02

invoice_item
id | invoice_id | product_id | quantity
1  | 1          | 1          | 2
2  | 1          | 2          | 1
3  | 2          | 3          | 1
4  | 3          | 2          | 2

我想要这个结果:

product.id | product.name | year(invoice.date) | month(invoice.date) | count       | sum
1          | apple        | 2013               | 01                  | 2           | 20
1          | apple        | 2013               | 02                  | 0 (or null) | 0 (or null)
2          | beer         | 2013               | 01                  | 1           | 20
2          | beer         | 2013               | 02                  | 2           | 40
3          | shoe         | 2013               | 01                  | 1           | 40
3          | shoe         | 2013               | 02                  | 0 (or null) | 0 (or null)

以下是我完成的代码:

SELECT * FROM ( 
SELECT product prod, 
(SELECT date FROM invoice 
GROUP BY YEAR(date), MONTH(date)) grpdate) p 
LEFT JOIN invoice_item ii 
   ON ii.product_id = p.id 
LEFT JOIN invoice i 
   ON ii.invoice_id=i.id 
GROUP BY p.id, YEAR(date), MONTH(date), 
IF(YEAR(date) = YEAR(grpdate), YEAR(date), null), 
IF(MONTH(date) = MONTH(grpdate), MONTH(date), null) 

2 个答案:

答案 0 :(得分:2)

您应该可以使用以下内容:

select 
  p.id,
  p.name,
  d.Year,
  d.Month,
  coalesce(i.TotalQuantity, 0) as TotalQuantity,
  p.price * coalesce(i.TotalQuantity, 0) as TotalPrice
from product p
inner join 
(
  select distinct p.id, year(date) year, month(date) month
  from invoice
  cross join product p
) d
  on p.id = d.id
left join
(
  select ii.invoice_id,
    ii.product_id,
    year(i.date) Year,
    month(i.date) Month,
    sum(quantity) TotalQuantity
  from invoice_item ii
  inner join invoice i
    on ii.invoice_id = i.id
  group by ii.invoice_id, ii.product_id, year(i.date), month(i.date)
) i
  on p.id = i.product_id
  and d.year = i.Year
  and d.month = i.month;

SQL Fiddle with Demo。此查询的结果是:

| ID |  NAME | YEAR | MONTH | TOTALQUANTITY | TOTALPRICE |
----------------------------------------------------------
|  1 | apple | 2013 |     1 |             2 |         20 |
|  1 | apple | 2013 |     2 |             0 |          0 |
|  2 |  beer | 2013 |     1 |             1 |         20 |
|  2 |  beer | 2013 |     2 |             2 |         40 |
|  3 |  shoe | 2013 |     1 |             1 |         40 |
|  3 |  shoe | 2013 |     2 |             0 |          0 |

答案 1 :(得分:0)

尝试以下:

SELECT product.id, product.name, YEAR(invoice.date), MONTH(invoice.date), COUNT(*), SUM(invoice_item.price)
FROM invoice_item , invoice, product
WHERE invoice_item.invoice_id = invoice.id
AND invoice_item.product_id = product.id
GROUP BY YEAR(invoice.date), MONTH(invoice.date),product.id