将新列嵌套到 Select 查询中

Nest new column into a Select query

我无法编辑以下 table,称为 connector_meter_value:

+----------------+---------------------+-------------+------+
| transaction_pk | value_timestamp     | value       | unit |
+----------------+---------------------+-------------+------+
|              1 | 2020-01-24 11:50:19 | 1690.100    | kWh  |
+----------------+---------------------+-------------+------+
|              1 | 2020-01-24 12:00:01 | 1691260.000 | Wh   |
+----------------+---------------------+-------------+------+
|              1 | 2020-01-24 12:00:01 | 7382.430    | W    |
+----------------+---------------------+-------------+------+
|              1 | 2020-01-24 12:30:00 | 1694940.000 | Wh   |
+----------------+---------------------+-------------+------+
|              1 | 2020-01-24 12:30:00 | 7385.140    | W    |
+----------------+---------------------+-------------+------+
|              2 | 2020-01-30 20:18:29 | 1701.680    | kWh  |
+----------------+---------------------+-------------+------+
|              2 | 2020-01-30 20:30:01 | 1703070.000 | Wh   |
+----------------+---------------------+-------------+------+
|              2 | 2020-01-30 20:30:01 | 7424.300    | W    |
+----------------+---------------------+-------------+------+
|              2 | 2020-01-30 21:00:00 | 1706660.000 | Wh   |
+----------------+---------------------+-------------+------+

我想创建以下视图,它给出每个 transaction_pk (cum_kWh) 值之间的差异,但首先创建一个新列,根据单位编辑值(千瓦时)。我还需要删除行,例如 unit = 'W'

+----------------+---------------------+-------------+------+----------+---------+
| transaction_pk | value_timestamp     | value       | unit | kWh      | cum_kWh |
+----------------+---------------------+-------------+------+----------+---------+
|              1 | 2020-01-24 11:50:19 | 1690.100    | kWh  | 1690.100 | 0       |
+----------------+---------------------+-------------+------+----------+---------+
|              1 | 2020-01-24 12:00:01 | 1691260.000 | Wh   | 1691.26  | 1.16    |
+----------------+---------------------+-------------+------+----------+---------+
|              1 | 2020-01-24 12:30:00 | 1694940.000 | Wh   | 1694.94  | 3.68    |
+----------------+---------------------+-------------+------+----------+---------+
|              2 | 2020-01-30 20:18:29 | 1701.680    | kWh  | 1701.680 | 0       |
+----------------+---------------------+-------------+------+----------+---------+
|              2 | 2020-01-30 20:30:01 | 1703070.000 | Wh   | 1703.07  | 2.39    |
+----------------+---------------------+-------------+------+----------+---------+
|              2 | 2020-01-30 21:00:00 | 1706660.000 | Wh   | 1706.66  | 3.59    |
+----------------+---------------------+-------------+------+----------+---------+

kWh 列采用 Wh 值并除以 1,000 以创建可用于 cum_kWh 列计算的 kWh 值。

当我可以编辑 connector_meter_value table 时,我使用了以下代码(我实际上复制了它并编辑了复制的 table(我称之为 meter_value_kWh))

CREATE VIEW kWhcharging AS
select t.*,
   COALESCE(kWh - (select kWh
              from meter_value_kWh t2
              where t2.value_timestamp < t.value_timestamp AND t2.transaction_pk = t.transaction_pk
              order by t2.value_timestamp DESC
              limit 1
             ),0
   ) as cum_kWh
from meter_value_kWh t
WHERE value <>0 and t.transaction_pk <>'NULL' and unit<>"W"

效果很好,但是因为复制的 table 不会在原始 table 自动刷新时自动刷新,所以我尝试创建原始 table 的视图。

我现在正尝试将一个 'kWh' 列插入到查询中,该列随后用于计算 cum_kWh 列,但我无法弄清楚该行的位置。下面加星标的部分是我添加的

select t.*,
   COALESCE(**select IF(unit = 'Wh', value/1000, value) as kWh
            From stevedb.connector_meter_value k1** - 
                   (select k1.kWh
                   FROM stevedb.connector_meter_value t2
                   where t2.value_timestamp < t.value_timestamp AND t2.transaction_pk = 
t.transaction_pk
                   order by t2.value_timestamp DESC
                   limit 1
                ),0
   ) as cum_kWh
from connector_meter_value t
WHERE t.value <>0 and t.transaction_pk <>'NULL' and t.unit<>"W"

不确定向视图添加新列的方式有什么问题,因为您已经向视图添加了新列 cum_kWh。当您想要将值 IF(unit = 'Wh', value/1000, value) 作为新列时,您只需以相同的方式将其添加到您的视图中:

CREATE VIEW kWhcharging AS
SELECT
    t.transaction_pk,  -- |
    t.value_timestamp, -- | The columns from the original table
    t.value,           -- |
    t.unit,            -- |
    IF(unit = 'Wh', value/1000, value) as kWh, -- The new normalized column 'kWh'
    COALESCE(IF(unit = 'Wh', value/1000, value) - (select
                IF(unit = 'Wh', value/1000, value)
            from connector_meter_value t2
            where
                t2.value_timestamp < t.value_timestamp AND
                t2.transaction_pk = t.transaction_pk AND
                t2.unit != 'W'
            order by
                t2.value_timestamp desc
            limit 1
           )
       , 0) as cum_kWh -- The cumulative calculation
FROM
    connector_meter_value t
WHERE
    unit != 'W'

您还可以创建两个视图。带有附加规范化列 kWh 的一个视图和使用第一个视图计算 cum_kWh 值的第二个视图,但可以从第一个视图访问新的 kWh 列。

这将生成以下结果 table:

+----------------+---------------------+---------+------+--------------------+--------------------+
| transaction_pk | value_timestamp     | value   | unit | kWh                | cum_kWh            |
+----------------+---------------------+---------+------+--------------------+--------------------+
|              1 | 2020-01-24 11:50:19 |  1690.1 | kWh  | 1690.0999755859375 |                  0 |
|              1 | 2020-01-24 12:00:01 | 1691260 | Wh   |            1691.26 |  1.160024414062491 |
|              1 | 2020-01-24 12:30:00 | 1694940 | Wh   |            1694.94 | 3.6800000000000637 |
|              2 | 2020-01-30 20:18:29 | 1701.68 | kWh  | 1701.6800537109375 |                  0 |
|              2 | 2020-01-30 20:30:01 | 1703070 | Wh   |            1703.07 | 1.3899462890624363 |
|              2 | 2020-01-30 21:00:00 | 1706660 | Wh   |            1706.66 | 3.5900000000001455 |
+----------------+---------------------+---------+------+--------------------+--------------------+

因为已经为您提供了 。但是对于您的问题,您可以使用两个用户定义的变量。

  • 首先将两个变量都设置为零。
  • 现在,如果 transaction_pk 与当前记录和旧记录相同,则找出 value 列之间的差异,如果不将其设置为零。
  • 将两个变量都设置为当前行数据,以便在下一行中使用。
  • 它适用于 order by 子句,必须确保它正常工作。

注意:此解决方案仅适用于 mysql 低于 8 的版本。如果 mysql 8 使用 window 函数 LAG 相同。

SELECT
    transaction_pk,
    value_timestamp,
    `value`,
    unit,
    ROUND(IF( transaction_pk = @old_tran, IF(unit = 'kWh', `value`, `value` / 1000) - @old_value, 0), 3) AS cum_kwh,
   @old_value:= ROUND(IF(unit = 'kWh' && @old_tran:= transaction_pk, `value`, `value` / 1000), 3)  AS Kwh
FROM connector_meter_value,
(SELECT @old_tran:= 0 AS tran, @old_value:= 0 AS o_val) AS b
WHERE unit <> 'W'
ORDER BY transaction_pk, value_timestamp

查看工作 Fiddle 相同。