两个表中的GROUP BY和SUM不同的日期

时间:2010-03-19 21:42:15

标签: mysql

我不确定这在一个mysql查询中是否可行,所以我可能只是通过php组合结果。

我有2个表:'用户'和'结算'

我正在尝试为这两个表中可用的每个日期对总计活动进行分组。 “用户”不是历史数据,但“结算”包含每笔交易的记录。

在这个例子中,我显示了一个用户的状态,我想对创建日期和存款金额求和,我也想按创建日期求和。我意识到数据之间存在一些脱节,但我想将它们全部放在一起并显示如下所示。这将向我展示所有用户在创建时的概述以及当前状态在总交易旁边的情况。

我尝试过UNION和LEFT JOIN,但我似乎无法继续工作。

联盟示例非常接近,但没有将日期合并为一行。

(
SELECT
created,
SUM(status) as totalActive,
NULL as totalDeposit
FROM users
GROUP BY created
)
UNION
(
SELECT
created,
NULL as totalActive,
SUM(transactionAmount) as totalDeposit
FROM billing
GROUP BY created
)

我也尝试使用日期查找表并加入日期,但SUM值被多次添加。

注意:我根本不关心userIds,而是将它放在这里作为示例。

用户表 (状态“1”表示“活跃”) (每个用户一条记录)

created     |   userId  |   status

2010-03-01  | 10            |   0
2010-03-01  | 11            |   1   
2010-03-01  | 12            |   1   
2010-03-10  | 13            |   0
2010-03-12  | 14            |   1
2010-03-12  | 15            |   1
2010-03-13  | 16            |   0   
2010-03-15  | 17            |   1

结算表 (为结算“交易”的每个实例创建的记录

created     |   userId  |   transactionAmount   

2010-03-01  | 10            |   50
2010-03-01  | 18            |   50
2010-03-01  | 19            |   100
2010-03-10  | 89            |   55
2010-03-15  | 16            |   50
2010-03-15  | 12            |   90
2010-03-22  | 99            |   150

期望的结果:

created     |   sumStatusActive     |   sumStatusInactive       |   sumTransactions

2010-03-01  | 2                                 |   1                                       |   200
2010-03-10  | 0                                 |   1                                       |   55
2010-03-12  | 2                                 |   0                                       |   0
2010-03-13  | 0                                 |   0                                       |   0
2010-03-15  | 1                                 |   0                                       |   140
2010-03-22  | 0                                 |   0                                       |   150

表转储:

CREATE TABLE IF NOT EXISTS `users` (
  `created` date NOT NULL,
  `userId` int(11) NOT NULL,
  `status` smallint(6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `users` (`created`, `userId`, `status`) VALUES
('2010-03-01', 10, 0),
('2010-03-01', 11, 1),
('2010-03-01', 12, 1),
('2010-03-10', 13, 0),
('2010-03-12', 14, 1),
('2010-03-12', 15, 1),
('2010-03-13', 16, 0),
('2010-03-15', 17, 1);


CREATE TABLE IF NOT EXISTS `billing` (
  `created` date NOT NULL,
  `userId` int(11) NOT NULL,
  `transactionAmount` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `billing` (`created`, `userId`, `transactionAmount`) VALUES
('2010-03-01', 10, 50),
('2010-03-01', 18, 50),
('2010-03-01', 19, 100),
('2010-03-10', 89, 55),
('2010-03-15', 16, 50),
('2010-03-15', 12, 90),
('2010-03-22', 99, 150);

3 个答案:

答案 0 :(得分:1)

试试这个:

Select created, sum(status) as totalActive, sum(transactionAmount) as totalDeposit
From
( (
  SELECT
  created,
  status,
  0 as transactionAmount
  FROM users
)
UNION
(
  SELECT
  created,
  0 as status,
  transactionAmount
  FROM billing
) ) as x group by created

答案 1 :(得分:0)

阿。感谢p.g.I.hall我能够修改查询并得到我想要的结果:

Select 
createdDate,
SUM(statusSum),
SUM(transactionAmountSum)

From
( (
  SELECT
  created as createdDate,
  sum(status) as statusSum,
 '0' as transactionAmountSum
  FROM users
  GROUP BY createdDate
)
UNION
(
  SELECT
  created as createdDate,
  '0' as statusSum,
  sum(transactionAmount) as transactionAmountSum
  FROM billing
  GROUP BY createdDate
) ) 

as x
group by createdDate

答案 2 :(得分:0)

警告 - 您的users表没有唯一键。我将在这里做一个疯狂的猜测并说你应该用userId列创建一个主键。

没有主键的表意味着您无法防止错误的重复数据滑入您的表格! Aaaaaah!