问题描述
我在mysql
中有一张下表。
DROP TABLE IF EXISTS `mdc_data`;
CREATE TABLE `mdc_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,`tariff_id` int(11) DEFAULT NULL,`msn` varchar(100) DEFAULT NULL,`cust_id` varchar(100) DEFAULT NULL,`kwh_t_max` varchar(100) DEFAULT NULL,`start_data_date_time` datetime DEFAULT NULL,`end_data_date_time` datetime DEFAULT NULL,`incoming_source` varchar(100) DEFAULT NULL,`t_type` varchar(100) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
/*Data for the table `mdc_data` */
insert into `mdc_data`(`id`,`tariff_id`,`msn`,`cust_id`,`kwh_t_max`,`start_data_date_time`,`end_data_date_time`,`incoming_source`,`t_type`) values (1,12,'00209706','37010114710','281','2020-09-10 00:40:00','2020-09-10 01:10:00','WAPDA','Slab Based'),(2,'00209707','37010114712','103',(3,'297','2020-09-10 02:00:00','2020-09-10 02:30:00',(4,'119',(5,'312','2020-09-10 03:20:00','2020-09-10 03:50:00',(6,'135',(7,'285','2020-09-10 01:20:00','2020-09-10 01:50:00','Generator',(8,'107',(9,'304','2020-09-10 02:40:00','2020-09-10 03:10:00',(10,'127',(11,'320','2020-09-10 04:00:00','2020-09-10 04:30:00',(12,'143','2020-09-10 04:40:00','2020-09-10 05:10:00',(13,'268','2020-09-09 23:20:00','2020-09-10 00:00:00',(14,'95','Slab Based');
通过使用此查询SELECT * FROM
mdc_data d WHERE d.
msn ='00209706' ORDER BY d.
start_data_date_time ASC
我得到了
id|tariff_id|msn | cust_id |kwh_t_max|start_data_date_time|end_data_date_time |incoming_source|t_type |
=================================================================================================================
13| 12 |00209706|37010114710| 268 |2020-09-09 23:20:00 |2020-09-10 00:00:00|Generator |Slab Based|
1| 12 |00209706|37010114710| 281 |2020-09-10 00:40:00 |2020-09-10 01:10:00|WAPDA |Slab Based|
7| 12 |00209706|37010114710| 285 |2020-09-10 01:20:00 |2020-09-10 01:50:00|Generator |Slab Based|
3| 12 |00209706|37010114710| 297 |2020-09-10 02:00:00 |2020-09-10 02:30:00|WAPDA |Slab Based|
9| 12 |00209706|37010114710| 304 |2020-09-10 02:40:00 |2020-09-10 03:10:00|Generator |Slab Based|
5| 12 |00209706|37010114710| 312 |2020-09-10 03:20:00 |2020-09-10 03:50:00|WAPDA |Slab Based|
11| 12 |00209706|37010114710| 320 |2020-09-10 04:00:00 |2020-09-10 04:30:00|Generator |Slab Based|
....
我想做什么?
2020-09-10 00:00:00
处的第一个值是起始值。
我想按每米计出消耗量。例如,仪表号是00209706
。为了获得WAPDA
的消费量,需要以下算法。
281 - 268 = 13 | 297 - 285 = 12 | 312 - 304 = 8 |
// Adding the difference value
13+12+8 = 33 => is the consumption against `WAPDA`
类似于发电机
285 - 281 = 4 | 304 - 297 = 7 | 320 - 312 = 8 |
// Adding the difference value
4+7+8 = 19=> is the consumption against `Generator`
预期产量
msn | units | incoming_source | date |
====================================================
00209706| 33 | WAPDA | 2020-09-10 |
00209706| 19 | Generator | 2020-09-10 |
....
解决方法
我认为您想要lag()
,然后进行汇总:
select msn,sum(kwh_t_max - lag_kwh_t_max) units,incoming_source,date(end_data_date_time) date
from (
select
t.*,lag(kwh_t_max) over(partition by msn order by end_data_date_time) lag_kwh_t_max
from mytable t
) t
group by msn,date(end_data_date_time)
我发现将消耗量减去属于相同来源的行会更有意义-但这就是您要的。
msn | units | incoming_source | date :------- | ----: | :-------------- | :--------- 00209706 | 19 | Generator | 2020-09-10 00209706 | 33 | WAPDA | 2020-09-10 00209707 | 20 | Generator | 2020-09-10 00209707 | 28 | WAPDA | 2020-09-10