0

我正在尝试从 2 个表中获取每月的总金额这是我的查询

SELECT date_format(items.date, '%M %Y') as month, 
COALESCE(SUM(items.amount), 0) as Rreceived, 
COALESCE(SUM(issued_items.amount), 0) as Issued, 
items.currency 
FROM items 
LEFT JOIN issued_items 
ON date_format(items.date, '%Y-%m')=date_format(issued_items.date, '%Y-%m') 
AND items.currency=issued_items.currency 
GROUP BY date_format(items.date, '%Y-%m')

我收到的金额有误。你能帮我更正查询吗?

SQL 文件

4

3 回答 3

2

尝试这个:

SELECT date_format(items.date, '%M %Y') as month, 
COALESCE(SUM(items.amount), 0) as Rreceived, 

  (SELECT SUM(amount)
  FROM issued_items
  WHERE date_format(items.date, '%Y-%m') = date_format(date, '%Y-%m') 
    AND items.currency = currency 
  ) AS Issued

FROM items
GROUP BY date_format(items.date, '%Y-%m')
于 2013-08-02T05:58:12.873 回答
1

添加与新的http://sqlfiddle.com/#!2/9856e9/1情况相关的新答案:

为避免行丢失,我在两个表上都选择了所有可能的月份/货币组合,因此我可以在两个表上进行左连接。请注意,如果联合中不存在组合(2013 年 4 月/美元),则不会返回任何行!

SELECT union_items.month, 
ifnull(items.amount,0) as Received, 
ifnull(issued_items.amount,0) as Issued, 
union_items.currency 
FROM ( 
      select 
      date_format(items.date, '%M %Y') as month, items.currency 
      FROM items  
      GROUP BY date_format(items.date, '%Y-%m')
      UNION 
      select 
      date_format(issued_items.date, '%M %Y') as month, issued_items.currency 
      FROM issued_items  
      GROUP BY date_format(issued_items.date, '%Y-%m')
) union_items
LEFT JOIN
(
SELECT date_format(items.date, '%M %Y') as month, 
COALESCE(SUM(items.amount), 0) as amount, 
items.currency 
FROM items  
GROUP BY date_format(items.date, '%Y-%m')
) items 
ON items.month=union_items.month 
AND items.currency=union_items.currency 
LEFT JOIN (
SELECT date_format(issued_items.date, '%M %Y') as month, 

  COALESCE(SUM(issued_items.amount), 0) as amount, 
issued_items.currency 
FROM issued_items
  GROUP BY date_format(issued_items.date, '%Y-%m')

)
issued_items 
ON union_items.month=issued_items.month 
AND union_items.currency=issued_items.currency 
于 2013-08-03T08:19:15.690 回答
1

@Stephen:您的解决方案在issue_items上为items上的每一行发出子查询,不是吗?

我认为这可能是一个更便宜的解决方案:

SELECT items.month, 
items.amount as Rreceived, 
issued_items.amount as Issued, 
items.currency 
FROM ( 

SELECT date_format(items.date, '%M %Y') as month, 
COALESCE(SUM(items.amount), 0) as amount, 
items.currency 
FROM items  
GROUP BY date_format(items.date, '%Y-%m')
) items 
LEFT JOIN (
SELECT date_format(issued_items.date, '%M %Y') as month, 

  COALESCE(SUM(issued_items.amount), 0) as amount, 
issued_items.currency 
FROM issued_items
  GROUP BY date_format(issued_items.date, '%Y-%m')

)
issued_items 
ON items.month=issued_items.month 
AND items.currency=issued_items.currency 

CHANGE IFNULL 可以解决您的问题(如 Oracle 中的 NVL)

SELECT items.month, 
ifnull(items.amount,0) as Rreceived, 
ifnull(issued_items.amount,0) as Issued, 
items.currency 
FROM ( 

SELECT date_format(items.date, '%M %Y') as month, 
COALESCE(SUM(items.amount), 0) as amount, 
items.currency 
FROM items  
GROUP BY date_format(items.date, '%Y-%m')
) items 
LEFT JOIN (
SELECT date_format(issued_items.date, '%M %Y') as month, 

  COALESCE(SUM(issued_items.amount), 0) as amount, 
issued_items.currency 
FROM issued_items
  GROUP BY date_format(issued_items.date, '%Y-%m')

)
issued_items 
ON items.month=issued_items.month 
AND items.currency=issued_items.currency 
于 2013-08-02T06:33:01.567 回答