FirebirdSQL联合重复记录

时间:2017-09-04 08:42:08

标签: sql duplicates union firebird-3.0

使用FirebirdSQL 3.0.2。我试图提取一个包含固定行的报告。意思是,我希望每次都有一定数量的行。如果结果集中不存在某个行,我想显示缺少行的值为零。

为此,我有一个虚拟表RAPOR_EK,其中所有可能的行组合都包含在行中。我在主报告SQL中使用UNION选择该表。

我的问题是重复记录。如果在我的主报告SQL中选择了一条记录,我会从虚拟表中选择一条额外的记录。

下面包含表结构,虚拟表记录,报告SQL和报告SQL输出。您可以在报告SQL输出中的行号1-2,5-6,7-8中看到重复记录。

我感谢任何帮助。

原始数据表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)
);

虚拟桌子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

虚拟表记录

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返回仅存在于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

1 个答案:

答案 0 :(得分:1)

鉴于数据的复杂性,我创建了一个简化的模式来演示解决方案:

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_sourcerapor_ek具有相同的角色,report_datatablo_medas的角色相同。

现在,为了获取您需要的数据,我们从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