带聚合函数的sql子查询-寻找替代项

mzillmmw  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(444)

我的table(表1)

+----+------------+---------+------------+
| id |    Date    |  Houses | kW_measure |
+----+------------+---------+------------+
| 01 | 01/01/2020 | House_A |    2020    |
| 02 | 01/01/2020 | House_B |    2100    |
| 03 | 01/01/2020 | House_C |    2200    |
| 04 | 01/02/2020 | House_A |    2040    |
| 05 | 01/02/2020 | House_B |    2125    |
| 06 | 01/02/2020 | House_C |    2210    |
| 07 | 01/03/2020 | House_A |    2050    |
| 08 | 01/03/2020 | House_B |    2150    |
| 09 | 01/03/2020 | House_C |    2220    |
+----+------------+---------+------------+

我做了一个旋转动作,很好:http://sqlfiddle.com/#!18/45b9d/1/0号

+------------+----------+----------+----------+
|    Date    |  House_A |  House_B |  House_C |
+------------+----------+----------+----------+
| 01/01/2020 |   2020   |   2100   |   2200   |
| 01/02/2020 |   2040   |   2125   |   2210   |
| 01/03/2020 |   2050   |   2150   |   2220   |
+------------+----------+----------+----------+

现在我需要计算度量值之间的差异,得到如下结果:

+------------+----------+-------+
|    Date    |  House_A | Daily |
+------------+----------+-------+
| 01/01/2020 |   2020   |   0   |
| 01/02/2020 |   2040   |   20  |
| 01/03/2020 |   2050   |   10  |
+------------+----------+-------+

但如果我不使用聚合,结果会产生空单元格-http://sqlfiddle.com/#!18/45b9d/5/0号

SELECT (SELECT DISTINCT a.Date)
, SUM(CASE WHEN a.Houses = 'House_A' THEN a.kW_measure END) as 'House_A'
, a.Kw_Measure - (SELECT MIN(b.kW_Measure) FROM Table1 b WHERE b.Houses = 'House_A'
     AND b.Date < a.Date)

FROM Table1 a

GROUP BY Date, kW_Measure
ORDER BY Date ASC

如果我真的使用它,我会得到这样的信息:
无法对包含聚合或子查询的表达式执行聚合函数。
任何帮助都会很好。

6tdlim6h

6tdlim6h1#

可以使用窗口函数。从现有的条件聚合查询开始,这将计算每套房子当前日期和上一日期之间的消费差异:

select
    date,
    house_a,
    coalesce(house_a - lag(house_a) over(order by date), 0) daily_a,
    house_b,
    coalesce(house_b - lag(house_b) over(order by date), 0) daily_b,
    house_c,
    coalesce(house_c - lag(house_c) over(order by date), 0)daily_c
from (
    select 
        date, 
        sum(case when houses = 'House_A' then kw_measure end) as house_a,
        sum(case when houses = 'House_B' then kw_measure end) as house_b,
        sum(case when houses = 'House_C' then kw_measure end) as house_c
    from table1
    group by date
) t
order by date

子查询在技术上不是必需的,但它避免了重复条件查询 sum() 两次。

ijnw1ujt

ijnw1ujt2#

如果你想让房子像结果显示的那样成为一个独立的柱子,你可以使用:

select house, date, kw_measure,
       (kw_measure -
        lag(kw_measure, 1, kw_measure) over (partition by house order by date) 
       ) as diff
from table1;

你似乎每个房子每个日期只有一行,所以聚合似乎没有必要。

t8e9dugd

t8e9dugd3#

你可以用 lag ,这是演示。

with cte as
(
  select
    distinct date,
    houses,
    kW_measure,
    lag(kW_measure) over (partition by houses order by date) as last_value
  from table1
)

select
  date,
  coalesce(sum(case when houses = 'House_A' then kW_measure - last_value end), 0) as House_A,
  coalesce(sum(case when houses = 'House_B' then kW_measure - last_value end), 0) as House_B,
  coalesce(sum(case when houses = 'House_C' then kW_measure - last_value end), 0) as House_C
from cte
group by
  date
order by
  date

输出:

| date       | house_a | house_b | house_c |
| -----------| ------- | ------- | ------- |
| 2020-01-01 | 0       | 0       | 0       |
| 2020-01-02 | 20      | 25      | 10      |
| 2020-01-03 | 10      | 25      | 10      |

相关问题