我在sqlite中使用了sql查询,实际上是3个SELECT语句的UNION。
我想简化它。有人能帮助我吗?
Select *, "A" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_Implantation >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240))
UNION ALL
select *, "A" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240) EXCEPT
SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_042016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_042016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_042016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_042016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_042016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation)
FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240)
)
UNION ALL
select *, "S" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_042016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_042016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_042016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_042016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_042016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240) EXCEPT
SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation)
FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240)
)
答案 0 :(得分:0)
在没有实际重写查询的情况下帮助简化有点困难,但我会尝试给出可能有帮助的一般指示。
1)您可以使用
之类的东西统一第一个和第二个子查询WHERE (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or ADM_ID = 240)
AND (Dte_Implantation > (select max ...)
OR (Dte_modif > (select max ...)
AND NOT EXISTS (SELECT 1 FROM SUP_STATION_052016 ...)))
当然使用WHERE NOT EXISTS
或LEFT JOIN ... WHERE somefield IS NULL
取决于您的DBMS优化程序,该选择取决于您
2)除此之外,您还可以通过将LEFT OUTER JOIN SUP_EMETTEUR_042016 ...
与
CASE
WHEN OneTable.MandatoryField IS NULL AND AnotherTable.MandatoryField IS NOT NULL THEN 'S'
WHEN OneTable.MandatoryField IS NOT NULL AND AnotherTable.MandatoryField IS NULL THEN 'A'
WHEN ...
END AS Status
3)此外,您可以尝试通过使用表别名来使查询更具可读性(仅用SUP_EMETTEUR_042016.EMR_LB_SYSTEME
等替换SE4.EMR_LB_SYSTEME
之类的长项。可能会将长条件ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or ADM_ID = 240
替换为更短的ADM_ID IN (6, 23, 137, 240)
也会给出一个镜头。
4)为了提高可读性,您可以尝试使用公用表表达式来消除多用(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
WITH max_di AS (SELECT MAX(Dte_Implantation) AS val FROM SUP_STATION_042016)
...(your select here)
FROM max_di
INNER JOIN SUP_STATION_052016 SS5
ON (..conditions here..
AND SS5.Dte_Implantation > max_di.val)
希望这件事能以某种方式帮助你。祝你重构好运=)