这是我的架构:
CREATE TABLE `tbltransactions` (
`transactionid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`transactiondate` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customerid` bigint(20) unsigned NOT NULL,
`transactiondetail` varchar(255) NOT NULL,
`transactionamount` decimal(10,2) NOT NULL,
UNIQUE KEY `transactionid` (`transactionid`),
KEY `customerid` (`customerid`),
CONSTRAINT `tbltransactions_ibfk_1` FOREIGN KEY (`customerid`) REFERENCES `tblcustomers` (`customerid`)
) ENGINE=InnoDB AUTO_INCREMENT=240 DEFAULT CHARSET=latin1;
transactionamount包含购买的正值和付款的负值。
我希望我可以从tbltransactions列出所有记录,其中客户的余额最迟为零。有什么帮助吗?
编辑:请考虑这个数据集:
purchase 10
payment -10 // balance is zero
purchase 5
payment -5 // balance again zero
purchase 7 // show this transaction and onwards
purchase 2
payment -5 // show this also since balance is not zero
编辑:实际数据样本:
INSERT INTO `tbltransactions` VALUES (1,'2014-06-22 22:51:00',39,'Balance when computerized',8851.00),(2,'2014-06-22 22:55:05',35,'Balance when computerized',5395.00),(3,'2014-06-22 22:56:17',53,'Balance when computerized',60.00),(4,'2014-06-22 22:57:15',54,'Balance when computerized',2671.00),(5,'2014-06-22 22:57:41',55,'Balance when computerized',1586.00),(6,'2014-06-22 22:58:34',61,'Balance when computerized',50.00),(7,'2014-06-22 22:59:22',56,'Balance when computerized',344.00),(8,'2014-06-22 22:59:42',71,'Balance when computerized',650.00),(9,'2014-06-22 23:01:10',63,'Balance when computerized',1573.00),(10,'2014-06-22 23:01:51',32,'Balance when computerized',7515.00),(11,'2014-06-22 23:02:22',72,'Balance when computerized',466.00),(12,'2014-06-22 23:03:10',64,'Balance when computerized',4774.00),(13,'2014-06-22 23:03:32',42,'Balance when computerized',2992.00),(14,'2014-06-22 23:05:24',41,'Balance when computerized',2218.00),(15,'2014-06-22 23:05:39',40,'Balance when computerized',7149.00),(16,'2014-06-22 23:06:25',80,'Balance when computerized',2607.00),(17,'2014-06-22 23:09:18',67,'Balance when computerized',357.00),(18,'2014-06-22 23:20:39',10,'Balance when computerized',677.00),(19,'2014-06-22 23:13:17',57,'Balance when computerized',135.00),(20,'2014-06-22 23:13:47',58,'Balance when computerized',5872.00),(21,'2014-06-24 11:36:10',73,'Balance when computerized',355.00),(22,'2014-06-22 23:14:30',74,'Balance when computerized',173.00),(23,'2014-06-22 23:16:45',59,'Balance when computerized',995.00),(24,'2014-06-22 23:17:44',19,'Balance when computerized',1704.00),(25,'2014-06-22 23:19:00',23,'Balance when computerized',690.00),(26,'2014-06-22 23:21:17',34,'Balance when computerized',10331.00),(27,'2014-06-22 23:21:43',38,'Balance when computerized',495.00),(28,'2014-06-22 23:22:01',65,'Balance when computerized',6676.00),(29,'2014-06-22 23:23:31',8,'Balance when computerized',4148.00),(30,'2014-06-22 23:23:53',24,'Balance when computerized',3124.00),(31,'2014-06-22 23:27:02',68,'Balance when computerized',3364.00),(35,'2014-06-22 23:35:22',46,'Balance when computerized',19105.00),(36,'2014-06-22 23:36:26',36,'Balance when computerized',2471.00),(37,'2014-06-22 23:36:42',60,'Balance when computerized',910.00),(38,'2014-06-22 23:37:11',75,'Balance when computerized',5203.00),(39,'2014-06-22 23:37:29',77,'Balance when computerized',2342.00),(40,'2014-06-22 23:37:42',13,'Balance when computerized',4555.00),(41,'2014-06-22 23:38:24',62,'Balance when computerized',271.00),(42,'2014-06-22 23:42:43',26,'Balance when computerized',5040.00),(43,'2014-06-22 23:43:13',33,'Balance when computerized',6792.00),(44,'2014-06-22 23:43:57',9,'Balance when computerized',1101.00),(45,'2014-06-22 23:44:27',21,'Balance when computerized',1010.00),(46,'2014-06-22 23:45:16',69,'Balance when computerized',89.00),(47,'2014-06-22 23:45:52',81,'Balance when computerized',220.00),(48,'2014-06-22 23:46:37',82,'Balance when computerized',205.00),(49,'2014-06-22 23:47:26',83,'Balance when computerized',731.00),(50,'2014-06-22 23:48:00',84,'Balance when computerized',155.00),(51,'2014-06-22 23:48:54',5,'Balance when computerized',475.00),(52,'2014-06-22 23:50:13',85,'Balance when computerized',1375.00),(53,'2014-06-22 23:51:04',86,'Balance when computerized',28.00),(54,'2014-06-22 23:51:39',87,'Balance when computerized',26.00),(55,'2014-06-22 23:52:23',88,'Balance when computerized',30.00),(56,'2014-06-22 23:52:53',89,'Balance when computerized',45.00),(57,'2014-06-22 23:53:23',90,'Balance when computerized',140.00),(58,'2014-06-22 23:54:13',91,'Balance when computerized',40.00),(59,'2014-06-22 23:55:38',93,'Balance when computerized',3350.00),(60,'2014-06-22 23:57:13',3,'Balance when computerized',60.00),(61,'2014-06-22 23:59:05',94,'Balance when computerized',3372.00),(62,'2014-06-23 00:00:12',20,'Balance when computerized',562.00),(63,'2014-06-23 00:00:48',18,'Balance when computerized',3227.00),(64,'2014-06-23 00:01:26',7,'Balance when computerized',1023.00),(65,'2014-06-23 00:01:46',29,'Balance when computerized',20.00),(66,'2014-06-23 00:02:57',15,'Balance when computerized',160.00),(67,'2014-06-23 00:04:14',11,'Balance when computerized',345.00),(68,'2014-06-23 00:04:50',31,'Balance when computerized',45.00),(69,'2014-06-23 00:08:45',50,'Balance when computerized',50.00),(70,'2014-06-23 00:09:05',6,'Balance when computerized',2880.00),(71,'2014-06-23 00:11:29',96,'Balance when computerized',1300.00),(72,'2014-06-23 00:12:40',4,'Balance when computerized',601.00),(74,'2014-06-24 10:21:26',97,'Balance when computerized',1250.00),(76,'2014-06-24 10:35:31',32,'1.5 ltr etc.',510.00),(77,'2014-06-24 15:04:13',97,'parchi',535.00),(78,'2014-06-24 15:05:51',32,'parchi',400.00),(79,'2014-06-24 15:08:08',32,'parchi',1924.00),(80,'2014-06-24 15:14:38',35,'suger berd',840.00),(81,'2014-06-24 15:16:49',39,'bottel',85.00),(82,'2014-06-24 15:21:51',20,'salt tusho',250.00),(83,'2014-06-24 15:23:49',26,'eggs',45.00),(84,'2014-06-24 15:24:54',38,'waldah',200.00),(85,'2014-06-24 15:26:12',78,'Balance when computerized',1557.00),(86,'2014-06-24 15:27:12',78,'haldi',70.00),(87,'2014-06-24 15:28:37',68,'eggs butter',87.00),(88,'2014-06-24 15:30:19',98,'Balance when computerized',550.00),(89,'2014-06-24 15:32:13',44,'2 coke',50.00),(90,'2014-06-24 15:33:05',81,'self',-220.00),(91,'2014-06-24 15:33:52',46,'razor',30.00),(92,'2014-06-24 15:34:37',75,'dues',40.00),(93,'2014-06-24 15:35:35',9,'oil ghee',625.00),(94,'2014-06-24 15:36:57',99,'bread',93.00),(95,'2014-06-24 15:38:14',100,'bottle razor',55.00),(96,'2014-06-24 15:38:54',7,'dues',40.00),(97,'2014-06-24 15:39:41',75,'ltr',60.00),(98,'2014-06-24 15:40:08',69,'1.5 ltr',60.00),(99,'2014-06-24 15:40:27',42,'2 1.5 ltr',120.00),(100,'2014-06-24 15:42:02',26,'bread bottle',110.00),(101,'2014-06-24 15:45:39',78,'saman',140.00),(102,'2014-06-26 15:19:20',101,'Oil dues',105.00),(103,'2014-06-26 15:19:59',26,'bread etc',55.00),(104,'2014-06-26 15:20:15',97,'parchi',290.00),(105,'2014-06-26 15:20:33',35,'parchi',355.00),(106,'2014-06-26 15:20:46',81,'bread',100.00),(107,'2014-06-26 15:21:26',102,'razor',40.00),(108,'2014-06-26 15:22:51',38,'dues',30.00),(109,'2014-06-26 15:23:35',20,'register, bottle',275.00),(110,'2014-06-26 15:23:55',46,'bottle dues etc',540.00),(112,'2014-06-26 15:26:08',46,'wife',-5000.00),(113,'2014-06-26 15:26:52',39,'bottle',65.00),(114,'2014-06-26 15:27:05',66,'1.5 ltr',85.00),(115,'2014-06-26 15:27:22',34,'cheeni etc',780.00),(116,'2014-06-26 15:27:46',97,'parchi',260.00),(117,'2014-06-26 15:28:04',81,'surf',370.00),(118,'2014-06-26 15:28:38',103,'rooh afza',150.00),(119,'2014-06-26 15:28:57',35,'parchi oil etc',623.00),(120,'2014-06-26 15:29:19',52,'easy paisa',1060.00),(121,'2014-06-26 15:29:51',35,'cake 1.5 ltr',185.00),(122,'2014-06-26 15:30:06',97,'parchi',243.00),(123,'2014-06-26 15:32:04',18,'dues',13.00),(124,'2014-06-26 15:32:28',26,'bread',50.00),(125,'2014-06-26 15:33:47',78,'bread',150.00),(126,'2014-06-26 15:34:52',9,'cheeni',280.00),(127,'2014-06-26 15:36:17',20,'oil',205.00),(128,'2014-06-26 15:39:31',96,'more load',500.00),(129,'2014-06-26 15:40:38',75,'water etc',125.00),(130,'2014-06-26 15:40:57',35,'dues',30.00),(131,'2014-06-26 15:41:10',18,'half role',90.00),(132,'2014-06-26 15:41:32',88,'geometery dues',20.00),(133,'2014-06-26 15:41:56',4,'dues',10.00),(134,'2014-06-26 15:42:18',41,'dues',60.00),(135,'2014-06-26 15:42:36',20,'ciggeret half role',190.00),(136,'2014-06-26 15:43:02',87,'always',30.00),(137,'2014-06-26 15:43:42',104,'dues',73.00),(138,'2014-06-26 15:44:07',13,'dues',946.00),(139,'2014-06-26 15:44:20',18,'surf',130.00),(140,'2014-06-26 15:44:29',35,'parchi',240.00),(141,'2014-06-26 15:44:46',85,'dues',30.00),(142,'2014-06-26 15:45:05',75,'milk',140.00),(143,'2014-06-26 15:45:24',74,'cream',40.00),(144,'2014-06-26 15:45:39',88,'milk',40.00),(145,'2014-06-26 15:46:00',38,'perfume',90.00),(146,'2014-06-26 15:46:20',32,'chilka etc',70.00),(147,'2014-06-26 15:47:05',90,'payment',-140.00),(148,'2014-06-26 15:47:26',18,'ghee dues',30.00),(149,'2014-06-26 15:47:45',98,'color',15.00),(150,'2014-06-26 15:48:00',85,'taala',50.00),(151,'2014-06-26 15:48:25',103,'ball',15.00),(153,'2014-06-26 15:51:21',64,'catchup',130.00),(154,'2014-06-26 15:51:42',65,'dues',10.00),(155,'2014-06-26 15:52:10',20,'dues',10.00),(156,'2014-06-26 15:52:35',18,'mirch',115.00),(157,'2014-06-26 15:52:56',18,'dues',10.00),(158,'2014-06-26 15:53:13',46,'half role etc',150.00),(159,'2014-06-26 15:53:37',33,'ghee',330.00),(160,'2014-06-26 15:54:06',36,'dues',10.00),(161,'2014-06-26 15:54:37',18,'dues',10.00),(162,'2014-06-26 15:54:50',18,'dues',30.00),(163,'2014-06-26 15:55:20',99,'dues',10.00),(164,'2014-06-26 15:58:14',92,'maidah',30.00),(165,'2014-06-26 16:16:23',26,'dues',856.00),(166,'2014-06-26 16:18:28',20,'load plus others',562.00),(167,'2014-06-26 16:51:35',75,'chanay',50.00),(168,'2014-06-26 16:54:22',103,'dettol',17.00),(169,'2014-06-26 16:55:00',42,'load',100.00),(171,'2014-06-26 17:15:23',85,'dues',125.00),(172,'2014-06-26 17:17:40',46,'tape',25.00),(173,'2014-06-26 17:33:50',66,'chana',40.00),(174,'2014-06-26 17:35:11',75,'shampoo',5.00),(175,'2014-06-26 17:36:37',106,'wiper',50.00),(176,'2014-06-26 17:37:23',43,'bottle',15.00),(177,'2014-06-26 17:37:51',87,'dues',60.00),(178,'2014-06-26 17:38:05',100,'bottle brush',125.00),(179,'2014-06-26 17:38:29',36,'shampoo',180.00),(180,'2014-06-26 17:39:49',32,'dues',20.00),(181,'2014-06-26 17:40:01',55,'dues',7.00),(182,'2014-06-26 17:41:01',41,'dues',15.00),(183,'2014-06-26 18:55:39',66,'bar haf',50.00),(184,'2014-06-26 19:40:30',103,'payment',-150.00),(185,'2014-06-26 20:24:00',61,'chohay maar',30.00),(186,'2014-06-26 21:47:45',97,'payment',-2578.00),(187,'2014-06-26 23:51:17',35,'boteletc',70.00),(188,'2014-06-27 00:00:18',66,'half',17.00),(189,'2014-06-27 00:02:05',99,'self',-107.00),(190,'2014-06-27 00:03:00',68,'tazab',30.00),(192,'2014-06-27 00:07:15',75,'due',25.00),(193,'2014-06-27 00:12:15',108,'dal',35.00),(194,'2014-06-27 00:14:54',57,'due',20.00),(195,'2014-06-27 00:15:30',65,'sig',45.00),(196,'2014-06-27 00:16:21',69,'shapener',15.00),(197,'2014-06-27 00:17:36',39,'botel',150.00),(198,'2014-06-27 00:19:27',37,'ice juice',140.00),(199,'2014-06-27 00:20:31',8,'sweet',250.00),(200,'2014-06-27 00:22:27',106,'botel',20.00),(201,'2014-06-27 00:23:24',22,'due',15.00),(202,'2014-06-27 00:24:08',81,'due',15.00),(203,'2014-06-27 00:26:31',19,'juice',50.00),(204,'2014-06-27 00:29:03',91,'kochyetc',30.00),(205,'2014-06-27 10:16:40',20,'payment',-2054.00),(206,'2014-06-27 10:39:38',78,'bread eggs',135.00),(208,'2014-06-27 10:41:27',74,'payment',-120.00),(209,'2014-06-27 10:45:24',109,'Balance when computerized',12287.00),(210,'2014-06-27 11:04:40',57,'payment',-155.00),(211,'2014-06-27 11:04:55',68,'blue band',55.00),(212,'2014-06-27 11:14:37',32,'sarmad soday',959.00),(213,'2014-06-27 11:28:59',78,'biscuit',40.00),(214,'2014-06-27 11:54:03',71,'bun',30.00),(215,'2014-06-27 15:26:06',92,'cocomo',20.00),(216,'2014-06-27 15:26:20',100,'paste',110.00),(217,'2014-06-27 15:26:48',71,'20 out of 30',-20.00),(218,'2014-06-27 15:27:22',32,'chetos',30.00),(219,'2014-06-27 15:27:44',75,'ghee cheeni',233.00),(220,'2014-06-27 15:29:32',18,'dues',45.00),(221,'2014-06-27 15:30:25',3,'ball',50.00),(222,'2014-06-27 15:31:15',100,'2 bottles',50.00),(223,'2014-06-27 15:32:10',3,'payment',-110.00),(224,'2014-06-27 15:32:38',4,'chips',40.00),(225,'2014-06-27 15:34:11',75,'ghee cheeni daal chawal',433.00),(226,'2014-06-27 15:34:52',41,'spray',400.00),(227,'2014-06-27 21:24:38',40,'katch bred',351.00),(228,'2014-06-27 22:02:04',8,'botel',60.00),(229,'2014-06-27 23:58:40',78,'half',90.00),(230,'2014-06-27 23:59:56',68,'rice',190.00),(231,'2014-06-28 00:00:40',97,'parchi',400.00),(232,'2014-06-28 00:01:15',97,'milk',70.00),(233,'2014-06-28 00:01:53',16,'ice',250.00),(234,'2014-06-28 00:02:53',35,'sig cake',20.00),(235,'2014-06-28 00:03:41',46,'botel cake',95.00),(236,'2014-06-28 00:05:17',75,'parchi rice bottels',750.00),(237,'2014-06-28 00:06:47',78,'sigret wife etc',230.00),(238,'2014-06-28 00:07:23',37,'nimko',10.00),(239,'2014-06-28 00:07:59',41,'rice',160.00);
答案 0 :(得分:1)
SELECT a.*
FROM tbltransactions AS a
JOIN (
SELECT customerid, MAX(transactiondate) last_zero_bal FROM (
SELECT customerid, transactiondate,
@balance := IF (customerid = @prev_cust,
@balance + transactionamount,
transactionamount) AS balance,
@prev_cust := customerid
FROM (SELECT *
FROM tbltransactions
ORDER BY customerid, transactiondate) AS t
CROSS JOIN (SELECT @balance := 0, @prev_cust := NULL) AS v
) AS running_balances
WHERE balance = 0
GROUP BY customerid
) AS b ON a.customerid = b.customerid AND a.transactiondate > b.last_zero_bal
别名running_balances
的子查询计算每个客户的运行余额。然后子查询b
找到每个客户零余额的最新日期。最后,这与原始交易表相结合,以显示此后的所有交易。
答案 1 :(得分:0)
你去吧
select b.*
from (
select a.customerid, max(a.transactionid) last_zero_transactionid
from (
select a.customerid, a.transactionid,
if(@prev_customer_id=customerid, @running_total:=@running_total+@last_transaction, @running_total:=0) running_total,
@prev_customer_id:=customerid prev_customer_id,
@last_transaction:=transactionamount last_transaction
from tbltransactions a
join (select @prev_customer_id:=0, @running_total:=0, @last_transaction:=0) b
order by customerid, transactionid) a
where a.running_total = 0
group by a.customerid) a
join tbltransactions b on a.customerid = b.customerid and a.last_zero_transactionid <= b.transactionid;
答案 2 :(得分:0)
SELECT t1.* FROM
tbltransactions t1
JOIN (
SELECT MAX(t1.transactionid) max_zero_id, t1.customerid
FROM tbltransactions t1
JOIN tbltransactions t2 ON t1.customerid = t2.customerid
AND t1.transactiondate >= t2.transactiondate
GROUP BY t1.transactiondate, t1.customerid
HAVING SUM(t2.transactionamount) = 0
) t2 ON t1.transactionid > t2.max_zero_id AND t2.customerid = t1.customerid
ORDER BY t1.customerid, t1.transactionid
<强>输出强>
+---------------+---------------------+------------+-------------------+-------------------+
| transactionid | transactiondate | customerid | transactiondetail | transactionamount |
+---------------+---------------------+------------+-------------------+-------------------+
| 106 | 2014-06-26 15:20:46 | 81 | bread | 100.00 |
| 117 | 2014-06-26 15:28:04 | 81 | surf | 370.00 |
| 202 | 2014-06-27 00:24:08 | 81 | due | 15.00 |
| 231 | 2014-06-28 00:00:40 | 97 | parchi | 400.00 |
| 232 | 2014-06-28 00:01:15 | 97 | milk | 70.00 |
+---------------+---------------------+------------+-------------------+-------------------+
查询计划
+----+-------------+------------+------+--------------------------+------------+---------+--------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+--------------------------+------------+---------+--------------------+------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 6 | Using temporary; Using filesort |
| 1 | PRIMARY | t1 | ref | transactionid,customerid | customerid | 8 | t2.customerid | 1 | Using where |
| 2 | DERIVED | t1 | ALL | customerid | NULL | NULL | NULL | 229 | Using temporary; Using filesort |
| 2 | DERIVED | t2 | ref | customerid | customerid | 8 | test.t1.customerid | 1 | Using where |
+----+-------------+------------+------+--------------------------+------------+---------+--------------------+------+---------------------------------+