SQL查询:计算给定2个表中的配置文件或丢失

时间:2012-02-05 16:32:46

标签: mysql sql

我有2张存储收入的表:

id    name     date         price     isDel
1     abc     2012-01-26    613.90       0
2     cde     2012-01-25    530.10       0

和费用:

id     name   price    date         isDel
1      b1     334.90   2012-01-26   0
2      b1     41.90    2012-01-25   0
3      d1     61.90    2012-01-25   0

我想得到如下结果:

date        total income         total expense             profile n loss
2012-01-25  530.10                103.80                    426.30
2012-01-26  613.90                344.90                    26.90

感谢。有人能提供SQL解决方案吗?我确实在stackoverflow上引用了一些成员的问题和答案,但我不能修改为想要我需要。请帮忙。

3 个答案:

答案 0 :(得分:2)

如果您希望每天获得盈利/亏损,请执行以下操作:

SELECT Inc.date, TotalInc, TotalExp, TotalInc - TotalExp AS ProfitNLoss
FROM
(
  SELECT date, SUM(price) as TotalInc
  FROM Income
  WHERE isdel = 0
  GROUP BY date) as Inc
LEFT OUTER JOIN 
(

  SELECT date, SUM(price) as TotalExp
  FROM Expense
  WHERE isdel = 0
  GROUP BY date) as Exp

ON Inc.date = Exp.date

UNION

SELECT Exp.date, TotalInc, TotalExp, TotalInc - TotalExp AS ProfitNLoss
FROM
(
  SELECT date, SUM(price) as TotalInc
  FROM Income
  WHERE isdel = 0
  GROUP BY date) as Inc
RIGHT OUTER JOIN 
(

  SELECT date, SUM(price) as TotalExp
  FROM Expense
  WHERE isdel = 0
  GROUP BY date) as Exp

ON Inc.date = Exp.date

答案 1 :(得分:0)

哎呀,这是“给我代码”的问题,但我会给你一些提示:

所以你需要做的是:

创建所有唯一日期的列表(遗憾的是我没有找到一种方法来生成sql中没有表查询或没有长php数组的日期列表,如果有人知道这样做的方法我会很乐意添加它到anser),所以你必须这样做:

SELECT date FROM income GROUP BY date;
-- If there's a chance that you'll have record in expenses and not in income:
(SELECT date FROM income GROUP BY date)
    UNION (SELECT date FROM expenses GROUP BY date;)
    GROUP BY date;
-- Or create table containing just list of all dates (best option)

然后加入所有独特日期的费用和收入:

SELECT SUM(expenses.price) AS expenses, SUM(incomes.price) AS income, dates.date
    FROM unique_dates -- or from your subquery
    LEFT JOIN incomes ON incomes.date = unique_dates.date
    LEFT JOIN expenses ON expenses.date = unique_dates.date
    GROUP BY unique_dates.date

哦,我不知道profile n loss是什么意思。

答案 2 :(得分:0)

感谢Pheiberg并感谢您的教授回复。 我刚做了一些小改动,所以当表没有记录或返回NULL时它会返回0,所以即使给定-ve值,Profit计算仍然可以进行校准。这里的代码分享给你们。

注意:即时通讯使用MySQL

SELECT Inc.create_dt, IFNULL(TotalInc,0), IFNULL(TotalExp,0), IFNULL(TotalInc,0) - IFNULL(TotalExp,0) AS ProfitNLoss
FROM
(
  SELECT create_dt, SUM(price) as TotalInc
  FROM income
  WHERE IsDel= 0
  GROUP BY create_dt) as Inc
LEFT OUTER JOIN 
(
  SELECT create_dt, SUM(price) as TotalExp
  FROM expenses
  WHERE IsDel= 0
  GROUP BY create_dt) as Exp

ON Inc.create_dt= Exp.create_dt
UNION
SELECT Exp.create_dt, IFNULL(TotalInc,0), IFNULL(TotalExp,0), IFNULL(TotalInc,0) - IFNULL(TotalExp,0) AS ProfitNLoss
FROM
(
  SELECT create_dt, SUM(price) as TotalInc
  FROM income
  WHERE IsDel= 0
  GROUP BY create_dt) as Inc
RIGHT OUTER JOIN 
(
  SELECT create_dt, SUM(price) as TotalExp
  FROM expenses
  WHERE IsDel= 0
  GROUP BY create_dt) as Exp
ON Inc.create_dt= Exp.create_dt