FirebirdSQL 联合重复记录
FirebirdSQL union duplicate records
使用火鸟SQL 3.0.2。我正在尝试提取其中包含固定行的报告。意思是,我想每次都有一定数量的行。如果结果集中不存在某些行,我想显示具有零值的缺失行。
为此,我有一个虚拟 table RAPOR_EK
,其中包含所有可能的行组合作为行。我 select table 在我的主要报告 SQL 中使用 UNION
。
我的问题是关于重复记录。在我的主报告 SQL 中有一条记录 selected 的地方,我还有一条来自虚拟 table.
的额外记录 selected
Table 结构、虚拟 table 记录、报告 SQL 和报告 SQL 输出包含在下面。您可以在报告 SQL 输出中的行号 1-2、5-6、7-8 等中看到重复记录。
感谢任何帮助。
原始数据table DDL
CREATE TABLE TABLO_MEDAS (
ID BIGINT NOT NULL,
T_GERIBILDIRIMNO INTEGER,
T_ILANTARIHI TIMESTAMP,
T_KODNO INTEGER,
T_KADEME SMALLINT,
T_IL VARCHAR(15),
T_ILCE VARCHAR(20),
T_SEBEKEUNSURU VARCHAR(220),
T_KESINTINEDENIILISKIACIKLAMA VARCHAR(250),
TKS_KAYNAGAGORE VARCHAR(20),
TKS_SUREYEGORE VARCHAR(10),
TKS_SEBEBEGORE VARCHAR(20),
T_BILDIRIMEGORE VARCHAR(15),
T_BASLAMATARIHI TIMESTAMP,
T_SONAERMETARIHI TIMESTAMP,
T_KESINTISURESI DOUBLE PRECISION,
TEKS_IMARALANICIOG DOUBLE PRECISION,
TEKS_IMARALANICIAG DOUBLE PRECISION,
TEKS_IMARALANDISIOG DOUBLE PRECISION,
TEKS_IMARALANDISIAG DOUBLE PRECISION,
TES_IMARALANICIOG DOUBLE PRECISION,
TES_IMARALANICIAG DOUBLE PRECISION,
TES_IMARALANDISIOG DOUBLE PRECISION,
TES_IMARALANDISIAG DOUBLE PRECISION,
T_ISLETME VARCHAR(50),
T_SURE VARCHAR(25),
CONSTRAINT PK_TABLO_MEDAS PRIMARY KEY (ID)
);
虚拟 table DDL
CREATE TABLE RAPOR_EK (
KAYNAK VARCHAR(30),
SEBEP VARCHAR(30)
) ;
举报SQL
select
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
M.TKS_KAYNAGAGORE AS "KAYNAK",
M.TKS_SEBEBEGORE AS "SEBEP",
(sum(M.TEKS_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_OG",
(sum(M.TEKS_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_AG",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IAIK_TOPLAM",
(sum(M.TEKS_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_OG",
(sum(M.TEKS_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_AG",
((sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IADK_TOPLAM",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG) + (sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "GNLTOPLAM"
from TABLO_MEDAS M
where
M.T_ILCE = 'CİHANBEYLİ'
and T_BILDIRIMEGORE = 'Bildirimsiz'
and (T_BASLAMATARIHI >= '2015-01-01' and T_BASLAMATARIHI <= '2015-01-31 23:59:59')
group by
M.TKS_KAYNAGAGORE,
M.TKS_SEBEBEGORE
UNION
SELECT
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
KAYNAK,
SEBEP,
0,
0,
0,
0,
0,
0,
0
FROM
RAPOR_EK
虚拟 table 条记录
KAYNAK SEBEP
Dağıtım-AG Dışsal
Dağıtım-AG Şebeke işletmecisi
Dağıtım-AG Mücbir Sebep
Dağıtım-AG Güvenlik
Dağıtım-OG Dışsal
Dağıtım-OG Şebeke işletmecisi
Dağıtım-OG Mücbir Sebep
Dağıtım-OG Güvenlik
İletim Sistemi Şebeke işletmecisi
İletim Sistemi Mücbir Sebep
İletim Sistemi Dışsal
İletim Sistemi Güvenlik
上报SQL输出
TARIH RAPOR_TURU KAYNAK SEBEP IAIK_OG IAIK_AG IAIK_TOPLAM IADK_OG IADK_AG IADK_TOPLAM GNLTOPLAM
2015-01-01 10 Dağıtım-AG Dışsal 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Dışsal 0 0.00046546808634433004 0.000462962962962963 0 0.003875217981011432 0.0028260562385191467 0.0011495668596296753
2015-01-01 10 Dağıtım-AG Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Şebeke işletmecisi 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Şebeke işletmecisi 0 0.0402048059579915 0.03998842592592593 0 0.09726797132338694 0.07093401158683058 0.04897975941207866
2015-01-01 10 Dağıtım-OG Dışsal 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Dışsal 0 0.052365159713737126 0.052083333333333336 0.042275574112734866 0 0.011445527766002543 0.04027589604631112
2015-01-01 10 Dağıtım-OG Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Güvenlik 0.16129032258064516 0.0005818351079304125 0.0014467592592592592 0 0 0 0.00102639898181221
2015-01-01 10 Dağıtım-OG Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Şebeke işletmecisi 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Şebeke işletmecisi 0.6881720430107527 0.8316751032757317 0.8309027777777778 2.8491649269311066 1.3673706645998838 1.768545994065282 1.1033378494888533
2015-01-01 10 İletim Sistemi Dışsal 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Şebeke işletmecisi 0 0 0 0 0 0 0
编辑:
使用以下 SQL returns 行仅存在于 TABLO_MEDAS
.
SELECT
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
RAPOR_EK.KAYNAK,
RAPOR_EK.SEBEP,
(sum(M.TEKS_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_OG",
(sum(M.TEKS_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_AG",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IAIK_TOPLAM",
(sum(M.TEKS_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_OG",
(sum(M.TEKS_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_AG",
((sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IADK_TOPLAM",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG) + (sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "GNLTOPLAM"
FROM
RAPOR_EK
LEFT JOIN TABLO_MEDAS AS M ON M.TKS_KAYNAGAGORE = RAPOR_EK.KAYNAK AND M.TKS_SEBEBEGORE = RAPOR_EK.SEBEP
where
M.T_ILCE = 'CİHANBEYLİ'
and T_BILDIRIMEGORE = 'Bildirimsiz'
and (T_BASLAMATARIHI >= '2015-01-01' and T_BASLAMATARIHI <= '2015-01-31 23:59:59')
group by
KAYNAK,
SEBEP
工作SQL如下:
SELECT
RAPOR_EK.KAYNAK,
RAPOR_EK.SEBEP,
COALESCE(DT.IAIK_OG, 0) AS "IAIK_OG",
COALESCE(DT.IAIK_AG, 0) AS "IAIK_AG",
COALESCE(DT.IAIK_TOPLAM, 0) AS "IAIK_TOPLAM",
COALESCE(DT.IADK_OG, 0) AS "IADK_OG",
COALESCE(DT.IADK_AG, 0) AS "IADK_AG",
COALESCE(DT.IADK_TOPLAM, 0) AS "IADK_TOPLAM",
COALESCE(DT.GNLTOPLAM, 0) AS "GNLTOPLAM"
FROM RAPOR_EK
LEFT JOIN (
SELECT
M.TKS_KAYNAGAGORE AS "KAYNAK",
M.TKS_SEBEBEGORE AS "SEBEP",
(sum(M.TES_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IAIK_OG",
(sum(M.TES_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IAIK_AG",
((sum(M.TES_IMARALANICIOG) + sum(M.TES_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "IAIK_TOPLAM",
(sum(M.TES_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IADK_OG",
(sum(M.TES_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IADK_AG",
((sum(M.TES_IMARALANDISIOG) + sum(M.TES_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "IADK_TOPLAM",
((sum(M.TES_IMARALANICIOG) + sum(M.TES_IMARALANICIAG) + (sum(M.TES_IMARALANDISIOG) + sum(M.TES_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "GNLTOPLAM"
FROM TABLO_MEDAS M
WHERE
M.T_ILCE = 'CİHANBEYLİ'
AND M.T_BILDIRIMEGORE = 'Bildirimsiz'
AND M.TKS_SUREYEGORE = 'Uzun'
AND (M.T_BASLAMATARIHI >= '2015-01-01' AND T_BASLAMATARIHI <= '2015-01-31 23:59:59')
GROUP BY
M.TKS_KAYNAGAGORE,
M.TKS_SEBEBEGORE
) DT USING (KAYNAK, SEBEP)
ORDER BY KAYNAK, SEBEP
鉴于您的数据的复杂性,我创建了一个简化的模式来演示解决方案:
create table report_source (
REPORT_ID_1 VARCHAR(25) NOT NULL,
REPORT_ID_2 VARCHAR(25) NOT NULL,
CONSTRAINT PK_REPORT_SOURCE PRIMARY KEY (REPORT_ID_1, REPORT_ID_2)
);
create table report_data (
REPORT_ID_1 VARCHAR(25) NOT NULL,
REPORT_ID_2 VARCHAR(25) NOT NULL,
VALUE_1 INT,
VALUE_2 INT
);
insert into report_source (report_id_1, report_id_2) values ('a', 'a');
insert into report_source (report_id_1, report_id_2) values ('a', 'b');
insert into report_source (report_id_1, report_id_2) values ('b', 'a');
insert into report_source (report_id_1, report_id_2) values ('b', 'b');
insert into report_data (report_id_1, report_id_2, value_1, value_2) values ('a', 'a', 1, 2);
insert into report_data (report_id_1, report_id_2, value_1, value_2) values ('b', 'b', 3, 4);
其中 report_source
与您的 rapor_ek
具有相同的角色,report_data
与 tablo_medas
具有相同的角色。
现在要获取您需要的数据,我们 select 来自 report_source
的所有行,并左连接到 report_data
,用默认值替换 null
值:
select src.report_id_1, src.report_id_2,
coalesce(dt.value_1, 0) as value_1,
coalesce(dt.value_2, 0) as value_2
from report_source src
left join report_data dt using (report_id_1, report_id_2)
产生:
a a 1 2
a b 0 0
b a 0 0
b b 3 4
但是您的代码并没有那么简单,因为您的 where 子句中还有将左联接转换为内联接的条件。要使用默认值获取报告数据,该查询需要取代 report_data
,换句话说:
select src.report_id_1, src.report_id_2,
coalesce(dt.value_1, 0) as value_1,
coalesce(dt.value_2, 0) as value_2
from report_source src
left join (
select *
from report_data
where <conditions>
<group by, etc>
) dt using (report_id_1, report_id_2)
您可能还需要将联接 using (report_id_1, report_id_2)
替换为 on dt.report_id_1 = src.report_id_1 and dt.report_id_1 = src.report_id_2
使用火鸟SQL 3.0.2。我正在尝试提取其中包含固定行的报告。意思是,我想每次都有一定数量的行。如果结果集中不存在某些行,我想显示具有零值的缺失行。
为此,我有一个虚拟 table RAPOR_EK
,其中包含所有可能的行组合作为行。我 select table 在我的主要报告 SQL 中使用 UNION
。
我的问题是关于重复记录。在我的主报告 SQL 中有一条记录 selected 的地方,我还有一条来自虚拟 table.
的额外记录 selectedTable 结构、虚拟 table 记录、报告 SQL 和报告 SQL 输出包含在下面。您可以在报告 SQL 输出中的行号 1-2、5-6、7-8 等中看到重复记录。
感谢任何帮助。
原始数据table DDL
CREATE TABLE TABLO_MEDAS (
ID BIGINT NOT NULL,
T_GERIBILDIRIMNO INTEGER,
T_ILANTARIHI TIMESTAMP,
T_KODNO INTEGER,
T_KADEME SMALLINT,
T_IL VARCHAR(15),
T_ILCE VARCHAR(20),
T_SEBEKEUNSURU VARCHAR(220),
T_KESINTINEDENIILISKIACIKLAMA VARCHAR(250),
TKS_KAYNAGAGORE VARCHAR(20),
TKS_SUREYEGORE VARCHAR(10),
TKS_SEBEBEGORE VARCHAR(20),
T_BILDIRIMEGORE VARCHAR(15),
T_BASLAMATARIHI TIMESTAMP,
T_SONAERMETARIHI TIMESTAMP,
T_KESINTISURESI DOUBLE PRECISION,
TEKS_IMARALANICIOG DOUBLE PRECISION,
TEKS_IMARALANICIAG DOUBLE PRECISION,
TEKS_IMARALANDISIOG DOUBLE PRECISION,
TEKS_IMARALANDISIAG DOUBLE PRECISION,
TES_IMARALANICIOG DOUBLE PRECISION,
TES_IMARALANICIAG DOUBLE PRECISION,
TES_IMARALANDISIOG DOUBLE PRECISION,
TES_IMARALANDISIAG DOUBLE PRECISION,
T_ISLETME VARCHAR(50),
T_SURE VARCHAR(25),
CONSTRAINT PK_TABLO_MEDAS PRIMARY KEY (ID)
);
虚拟 table DDL
CREATE TABLE RAPOR_EK (
KAYNAK VARCHAR(30),
SEBEP VARCHAR(30)
) ;
举报SQL
select
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
M.TKS_KAYNAGAGORE AS "KAYNAK",
M.TKS_SEBEBEGORE AS "SEBEP",
(sum(M.TEKS_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_OG",
(sum(M.TEKS_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_AG",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IAIK_TOPLAM",
(sum(M.TEKS_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_OG",
(sum(M.TEKS_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_AG",
((sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IADK_TOPLAM",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG) + (sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "GNLTOPLAM"
from TABLO_MEDAS M
where
M.T_ILCE = 'CİHANBEYLİ'
and T_BILDIRIMEGORE = 'Bildirimsiz'
and (T_BASLAMATARIHI >= '2015-01-01' and T_BASLAMATARIHI <= '2015-01-31 23:59:59')
group by
M.TKS_KAYNAGAGORE,
M.TKS_SEBEBEGORE
UNION
SELECT
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
KAYNAK,
SEBEP,
0,
0,
0,
0,
0,
0,
0
FROM
RAPOR_EK
虚拟 table 条记录
KAYNAK SEBEP
Dağıtım-AG Dışsal
Dağıtım-AG Şebeke işletmecisi
Dağıtım-AG Mücbir Sebep
Dağıtım-AG Güvenlik
Dağıtım-OG Dışsal
Dağıtım-OG Şebeke işletmecisi
Dağıtım-OG Mücbir Sebep
Dağıtım-OG Güvenlik
İletim Sistemi Şebeke işletmecisi
İletim Sistemi Mücbir Sebep
İletim Sistemi Dışsal
İletim Sistemi Güvenlik
上报SQL输出
TARIH RAPOR_TURU KAYNAK SEBEP IAIK_OG IAIK_AG IAIK_TOPLAM IADK_OG IADK_AG IADK_TOPLAM GNLTOPLAM
2015-01-01 10 Dağıtım-AG Dışsal 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Dışsal 0 0.00046546808634433004 0.000462962962962963 0 0.003875217981011432 0.0028260562385191467 0.0011495668596296753
2015-01-01 10 Dağıtım-AG Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Şebeke işletmecisi 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-AG Şebeke işletmecisi 0 0.0402048059579915 0.03998842592592593 0 0.09726797132338694 0.07093401158683058 0.04897975941207866
2015-01-01 10 Dağıtım-OG Dışsal 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Dışsal 0 0.052365159713737126 0.052083333333333336 0.042275574112734866 0 0.011445527766002543 0.04027589604631112
2015-01-01 10 Dağıtım-OG Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Güvenlik 0.16129032258064516 0.0005818351079304125 0.0014467592592592592 0 0 0 0.00102639898181221
2015-01-01 10 Dağıtım-OG Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Şebeke işletmecisi 0 0 0 0 0 0 0
2015-01-01 10 Dağıtım-OG Şebeke işletmecisi 0.6881720430107527 0.8316751032757317 0.8309027777777778 2.8491649269311066 1.3673706645998838 1.768545994065282 1.1033378494888533
2015-01-01 10 İletim Sistemi Dışsal 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Güvenlik 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Mücbir Sebep 0 0 0 0 0 0 0
2015-01-01 10 İletim Sistemi Şebeke işletmecisi 0 0 0 0 0 0 0
编辑:
使用以下 SQL returns 行仅存在于 TABLO_MEDAS
.
SELECT
'2015-01-01' AS "TARIH",
10 AS "RAPOR_TURU",
RAPOR_EK.KAYNAK,
RAPOR_EK.SEBEP,
(sum(M.TEKS_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_OG",
(sum(M.TEKS_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IAIK_AG",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IAIK_TOPLAM",
(sum(M.TEKS_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_OG",
(sum(M.TEKS_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')) as "IADK_AG",
((sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "IADK_TOPLAM",
((sum(M.TEKS_IMARALANICIOG) + sum(M.TEKS_IMARALANICIAG) + (sum(M.TEKS_IMARALANDISIOG) + sum(M.TEKS_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))) AS "GNLTOPLAM"
FROM
RAPOR_EK
LEFT JOIN TABLO_MEDAS AS M ON M.TKS_KAYNAGAGORE = RAPOR_EK.KAYNAK AND M.TKS_SEBEBEGORE = RAPOR_EK.SEBEP
where
M.T_ILCE = 'CİHANBEYLİ'
and T_BILDIRIMEGORE = 'Bildirimsiz'
and (T_BASLAMATARIHI >= '2015-01-01' and T_BASLAMATARIHI <= '2015-01-31 23:59:59')
group by
KAYNAK,
SEBEP
工作SQL如下:
SELECT
RAPOR_EK.KAYNAK,
RAPOR_EK.SEBEP,
COALESCE(DT.IAIK_OG, 0) AS "IAIK_OG",
COALESCE(DT.IAIK_AG, 0) AS "IAIK_AG",
COALESCE(DT.IAIK_TOPLAM, 0) AS "IAIK_TOPLAM",
COALESCE(DT.IADK_OG, 0) AS "IADK_OG",
COALESCE(DT.IADK_AG, 0) AS "IADK_AG",
COALESCE(DT.IADK_TOPLAM, 0) AS "IADK_TOPLAM",
COALESCE(DT.GNLTOPLAM, 0) AS "GNLTOPLAM"
FROM RAPOR_EK
LEFT JOIN (
SELECT
M.TKS_KAYNAGAGORE AS "KAYNAK",
M.TKS_SEBEBEGORE AS "SEBEP",
(sum(M.TES_IMARALANICIOG)/(select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IAIK_OG",
(sum(M.TES_IMARALANICIAG)/(select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IAIK_AG",
((sum(M.TES_IMARALANICIOG) + sum(M.TES_IMARALANICIAG)) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "IAIK_TOPLAM",
(sum(M.TES_IMARALANDISIOG)/(select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IADK_OG",
(sum(M.TES_IMARALANDISIAG)/(select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ')*60) as "IADK_AG",
((sum(M.TES_IMARALANDISIOG) + sum(M.TES_IMARALANDISIAG)) / ((select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "IADK_TOPLAM",
((sum(M.TES_IMARALANICIOG) + sum(M.TES_IMARALANICIAG) + (sum(M.TES_IMARALANDISIOG) + sum(M.TES_IMARALANDISIAG))) / ((select sum(I.ILC_IMARALANICI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANICI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_OG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ') + (select sum(I.ILC_IMARALANDISI_AG) from ILCELER_TABLOSU I where I.ILCEADI = 'CİHANBEYLİ'))*60) AS "GNLTOPLAM"
FROM TABLO_MEDAS M
WHERE
M.T_ILCE = 'CİHANBEYLİ'
AND M.T_BILDIRIMEGORE = 'Bildirimsiz'
AND M.TKS_SUREYEGORE = 'Uzun'
AND (M.T_BASLAMATARIHI >= '2015-01-01' AND T_BASLAMATARIHI <= '2015-01-31 23:59:59')
GROUP BY
M.TKS_KAYNAGAGORE,
M.TKS_SEBEBEGORE
) DT USING (KAYNAK, SEBEP)
ORDER BY KAYNAK, SEBEP
鉴于您的数据的复杂性,我创建了一个简化的模式来演示解决方案:
create table report_source (
REPORT_ID_1 VARCHAR(25) NOT NULL,
REPORT_ID_2 VARCHAR(25) NOT NULL,
CONSTRAINT PK_REPORT_SOURCE PRIMARY KEY (REPORT_ID_1, REPORT_ID_2)
);
create table report_data (
REPORT_ID_1 VARCHAR(25) NOT NULL,
REPORT_ID_2 VARCHAR(25) NOT NULL,
VALUE_1 INT,
VALUE_2 INT
);
insert into report_source (report_id_1, report_id_2) values ('a', 'a');
insert into report_source (report_id_1, report_id_2) values ('a', 'b');
insert into report_source (report_id_1, report_id_2) values ('b', 'a');
insert into report_source (report_id_1, report_id_2) values ('b', 'b');
insert into report_data (report_id_1, report_id_2, value_1, value_2) values ('a', 'a', 1, 2);
insert into report_data (report_id_1, report_id_2, value_1, value_2) values ('b', 'b', 3, 4);
其中 report_source
与您的 rapor_ek
具有相同的角色,report_data
与 tablo_medas
具有相同的角色。
现在要获取您需要的数据,我们 select 来自 report_source
的所有行,并左连接到 report_data
,用默认值替换 null
值:
select src.report_id_1, src.report_id_2,
coalesce(dt.value_1, 0) as value_1,
coalesce(dt.value_2, 0) as value_2
from report_source src
left join report_data dt using (report_id_1, report_id_2)
产生:
a a 1 2
a b 0 0
b a 0 0
b b 3 4
但是您的代码并没有那么简单,因为您的 where 子句中还有将左联接转换为内联接的条件。要使用默认值获取报告数据,该查询需要取代 report_data
,换句话说:
select src.report_id_1, src.report_id_2,
coalesce(dt.value_1, 0) as value_1,
coalesce(dt.value_2, 0) as value_2
from report_source src
left join (
select *
from report_data
where <conditions>
<group by, etc>
) dt using (report_id_1, report_id_2)
您可能还需要将联接 using (report_id_1, report_id_2)
替换为 on dt.report_id_1 = src.report_id_1 and dt.report_id_1 = src.report_id_2