mysql 选择两个值显示出来,MySQL显示两个值之和

Below is my query.

SELECT n.`name`,n.`customer_id`,m.`msn`, m.kwh,

m.kwh - LAG(m.kwh) OVER(PARTITION BY n.`customer_id` ORDER BY m.`data_date_time`) AS kwh_diff

FROM mdc_node n

INNER JOIN `mdc_meters_data` m ON n.`customer_id` = m.`cust_id`

WHERE n.`lft` = 5 AND n.`icon` NOT IN ('folder')

AND m.`data_date_time` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()

Which gives me below result

u4gto.png

I want to sum up the kwh_diff and to show only one-row record not multiple like below

name customer_id msn sum_kwh_diff

Zeeshan 37010114711 4A60193390663 4.5

I have tried to do the following

SUM(m.kwh - LAG(m.kwh) OVER(PARTITION BY n.`customer_id` ORDER BY m.`data_date_time`)) AS sum_kwh_diff

and got Error Code: 4074 Window functions can not be used as arguments to group functions.

解决方案

You want to sum the differences between consecutive rows.

Say, for example, that you have these values for the column kwh:

kwh

---

10

12

14

17

25

32

so the differences are:

kwh_diff

--------

0

12-10

14-12

17-14

25-17

32-25

The sum of these differences is equal to 32-10 which is:

the diffference between the last value and the first value

So what you need is window function FIRST_VALUE() to obtain these values:

SELECT DISTINCT n.`name`, n.`customer_id`, m.`msn`,

FIRST_VALUE(m.kwh) OVER (PARTITION BY n.`customer_id` ORDER BY m.`data_date_time` DESC) -

FIRST_VALUE(m.kwh) OVER (PARTITION BY n.`customer_id` ORDER BY m.`data_date_time` ASC) AS kwh_diff

FROM mdc_node n

INNER JOIN `mdc_meters_data` m ON n.`customer_id` = m.`cust_id`

WHERE n.`lft` = 5 AND n.`icon` NOT IN ('folder')

AND m.`data_date_time` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()

and no subquery or aggregation is needed.

I kept in my code PARTITION BY n.customer_id because you use it in your code, although you may need PARTITION BY n.customer_id, m.msn.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值