SQL

lxq19851204 2013-01-04 04:08:08

预定表:
CREATE TABLE `t3` (
`MenuID` int(11) NOT NULL default '0',
`Total` decimal(19,4) default '0.0000',
`TransStatus` int(11) default '0',
`BusinessDate` datetime default NULL
);
insert into t3 value (182,100,2,'2013-01-07');
insert into t3 value(184,100,7,'2013-01-07');
CREATE TABLE `t2` (
`PaymentID` int(11) NOT NULL default '0',
`MenuID` int(11) NOT NULL default '0',
`PaymentAmount` decimal(19,4) NOT NULL default '0.0000',
`BusinessDate` datetime default NULL
);
insert into t2 value (176,182,50,'2013-01-07');
insert into t2 value(177,182,50,'2013-01-08');
insert into t2 value(178,184,100,'2013-01-07');

销售表:
CREATE TABLE `t4` (
`MenuID` int(11) NOT NULL default '0',
`Total` decimal(19,4) default '0.0000',
`TransStatus` int(11) default '0',
`BusinessDate` datetime default NULL
)
insert into t4 value (182,100,2,'2013-01-08');
insert into t4 value(184,100,7,'2013-01-08');

CREATE TABLE `t5` (
`PaymentID` int(11) NOT NULL default '0',
`MenuID` int(11) NOT NULL default '0',
`PaymentAmount` decimal(19,4) NOT NULL default '0.0000',
`BusinessDate` datetime default NULL
);
insert into t2 value (176,182,50,'2013-01-07');
insert into t2 value(177,182,50,'2013-01-08');
insert into t2 value(178,184,100,'2013-01-07');

'*********************************************
Select sum(total),sum(aa),sum(DEP_BF) as DEP_BF
From (SELECT aa.MenuID, aa.BusinessDate,C.total,SUM(aa) as aa,IF(DEP_BF1 IS NULL,0,MAX(c.total)-SUM(aa)) AS DEP_BF
FROM ( SELECT a1.menuid as MenuID,a1.businessdate as BusinessDate,SUM(a1.Paymentamount) AS aa ,
(Select sum(a2.Paymentamount) FROM t2 a2 where a2.businessDate<a1.businessdate
AND A1.MENUID=A2.MENUID GROUP BY A2.MENUID) AS DEP_BF1 FROM t2 a1
WHERE a1.businessdate Between '2013-01-07' And '2013-01-07' GROUP BY a1.menuid,a1.businessdate) aa
LEFT JOIN t3 c ON c.menuid=aa.menuid Where c.TransStatus=2 Group by c.menuid)a

没有转去销售表的时候,数据是正确的;
'2013-01-07':
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 100.0000 | 50.0000 | 0.0000 |
+------------+---------+---------+

'2013-01-08':
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 100.0000 | 50.0000 | 50.0000 |
+------------+---------+---------+

当在'2013-01-08'时,把预定表的数据转到销售表的时候,
'2013-01-07':
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 200.0000 |150.0000 | 0.0000 |
+------------+---------+---------+

'2013-01-08':
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 100.0000 | 50.0000 | 50.0000 |
+------------+---------+---------+

正确的应该是:
'2013-01-07':
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 150.0000 | 150.0000 |0.0000 |
+------------+---------+---------+

'2013-01-08'(150是在01-08号之前收的算DEP_BF):
+------------+---------+---------+
| sum(total) | sum(aa) | DEP_BF |
+------------+---------+---------+
| 200.0000 | 50.0000 | 150.0000|
+------------+---------+---------+
...全文
149 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxq19851204 2013-01-04
  • 打赏
  • 举报
回复
SELECT aa.MenuID, aa.BusinessDate,C.total,SUM(aa) as aa,IF(DEP_BF1 IS NULL,0,MAX(c.total)-SUM(aa)) AS DEP_BF
 FROM ( SELECT a1.menuid as MenuID,a1.businessdate as BusinessDate,SUM(a1.Paymentamount) AS aa,
(Select sum(a2.Paymentamount)  FROM t2 a2 where a2.businessDate<a1.businessdate
AND A1.MENUID=A2.MENUID GROUP BY A2.MENUID) AS DEP_BF1
FROM t2 a1
WHERE a1.businessdate  Between '2013-01-07' And '2013-01-07'
GROUP BY a1.menuid,a1.businessdate) aa
 Inner JOIN t3 c
 ON c.menuid=aa.menuid  Where c.TransStatus=2  Group by c.menuid
'************
上面这个语句需要改

更新t3的transstatus全部为2
查询7号:
+--------+---------------------+----------+----------+--------+
| MenuID | BusinessDate        | total    | aa       | DEP_BF |
+--------+---------------------+----------+----------+--------+
|    182 | 2013-01-07 00:00:00 | 100.0000 |  50.0000 |      0 |
|    184 | 2013-01-07 00:00:00 | 100.0000 | 100.0000 |      0 |
+--------+---------------------+----------+----------+--------+
查询8号的数据要为:
+--------+---------------------+----------+----------+--------+
| MenuID | BusinessDate        | total    | aa       | DEP_BF |
+--------+---------------------+----------+----------+--------+
|    182 | 2013-01-08 00:00:00 | 100.0000 |  50.0000 |50.0000 |
|    184 | 2013-01-08 00:00:00 | 100.0000 |   0.0000 |100.0000|
+--------+---------------------+----------+----------+--------+
ACMAIN_CHM 2013-01-04
  • 打赏
  • 举报
回复
没看懂想实现什么。 建议不要用销售之类的业务术语,直接描述你的表原来是什么样的数据,期望的结果是什么样。
lxq19851204 2013-01-04
  • 打赏
  • 举报
回复
t5与t2的数据一样
rucypli 2013-01-04
  • 打赏
  • 举报
回复
t5表没数据? 最后的sql执行报错
lxq19851204 2013-01-04
  • 打赏
  • 举报
回复
就是2张单为预定状态的时候,DEP_BF为0 当在01-08转去销售表的时候,02-07收的钱就变为DEP_BF=150.
rucypli 2013-01-04
  • 打赏
  • 举报
回复
没看明白 什么转去

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧