我有一个表,我需要执行sql查询来转换列上的行。我的桌子是这样的:
----------------------------------------------------------
Id |IdR |DateFrom |DateTo |NameOfHappening |Amount
293|264 |01.06.2019 |30.06.2019 |Disease 1 |2836.80
369|600 |01.07.2019 |07.07.2019 |Disease 1 |661.92
646|631 |01.08.2019 |11.08.2019 |Disease 1 |876.59
647|631 |12.08.2019 |21.08.2019 |Disease 2 |796.90
840|703 |30.09.2019 |30.09.2019 |Disease 1 |90.75
971|718 |31.05.2019 |31.05.2019 |Disease 1 |0.00我编写了显示日期的查询,但其他列有问题。我的SQL查询如下所示:
select
a.IDR,
(select max(op.DATE_FROM) from HAPP_TABLE op where op.IDR=a.IDR) as DATE_FROM_HP1,
(select max(op.DATE_TO) from HAPP_TABLE op where op.IDR=a.IDR) as DATE_TO_HP1,
(select max(op.DATE_FROM) from HAPP_TABLE op where op.IDR=a.IDR group by op.DATE_FROM order by op.DATE_FROM desc rows 2 to 2) as DATE_FROM_HP2,
(select max(op.DATE_TO) from HAPP_TABLE op where op.IDR=a.IDR group by op.DATE_FROM order by op.DATE_FROM desc rows 2 to 2) as DATE_TO_HP2,
(select max(op.DATE_FROM) from HAPP_TABLE op where op.IDR=a.IDR group by op.DATE_FROM order by op.DATE_FROM desc rows 3 to 3) as DATE_FROM_HP3,
(select max(op.DATE_TO) from HAPP_TABLE op where op.IDR=a.IDR group by op.DATE_FROM order by op.DATE_FROM desc rows 3 to 3) as DATE_TO_HP3
from HAPP_TABLE a
group by a.IDR到这个列日期,我需要通过IDR列连接来自列NameOfHappening的其他数据和一行组中的数量。但是这两列的数据我无法获得类似的日期查询日期。我必须选择与日期相对应的列来行。我想得到这样的东西:
IdR DATE_FROM_HP1 DATE_TO_HP1 DATE_FROM_HP2 DATE_TO_HP2
264 |01.06.2019 |30.06.2019 |Disease 1|null |null |null
600 |01.07.2019 |07.07.2019 |Disease 1|null |null |null
631 |12.08.2019 |21.08.2019 |Disease 2|01.08.2019 |11.08.2019 |Disease 1
703 |30.09.2019 |30.09.2019 |Disease 1|null |null |null
718 |31.05.2019 |31.05.2019 |Disease 1|null |null |null请帮助我解决这个问题,因为我尝试了几个解决方案,每个解决方案都以多行选择消息结束。
发布于 2019-10-21 14:28:06
使用关联子查询获取“上一”行,然后返回:
select h.*, hprev.* -- select whatever columns you want
from (select h.*,
(select max(h2.date)
from happ_table h2
where h2.idr = h.idr and
h2.date_from < h.date_from
) as prev_date_from
from happ_table h
) h left join
happ_table hprev
on hprev.idr = h.idr and
hprev.date_from = h.prev_date_from;备注:
在Firebird
发布于 2019-10-21 18:16:18
像select max(op.DATE_FROM) from HAPP_TABLE op where op.IDR=a.IDR这样的查询只有一行,所以添加rows 2 to 2或rows 3 to 3不会产生值。
此外,使用max会产生不一定属于一起的值,因此可能会产生错误的结果
您可以通过删除max的使用、按特定行应用一致的顺序和获取特定行来更改现有查询,例如:
select
a.IDR,
(select op.DATE_FROM from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM rows 1) as DATE_FROM_HP1,
(select op.DATE_TO from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM rows 1) as DATE_TO_HP1,
(select op.DATE_FROM from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM desc rows 2 to 2) as DATE_FROM_HP2,
(select op.DATE_TO from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM desc rows 2 to 2) as DATE_TO_HP2,
(select op.DATE_FROM from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM desc rows 3 to 3) as DATE_FROM_HP3,
(select op.DATE_TO from HAPP_TABLE op where op.IDR=a.IDR order by op.DATE_FROM desc rows 3 to 3) as DATE_TO_HP3
from HAPP_TABLE a
group by a.IDR然而,戈登提出的解决方案可能更好。
https://stackoverflow.com/questions/58487153
复制相似问题