一个结果中的四个查询 - oracle

时间:2015-03-22 12:00:54

标签: sql oracle

我有四个问题。

1.result:Count()| Nazev

  1. 结果:Count()| Nazev

  2. 结果:Ode_dne_včetně| Do_dne_včetně| Nazev_organizace | Pocet

  3. 结果:Nazev | Create_uzivatel | create_cas

  4. 我希望一次点击后只有一个结果。这样:

    Count()| Nazev | Count()| Nazev | Ode_dne_včetně| Do_dne_včetně| Nazev_organizace | Pocet | Nazev | Create_uzivatel | create_cas

    有可能吗?

    - 第一

        select count(*),subjekt.nazev from osoba,subjekt where   
        osoba.ID_PATRI_DO=subjekt.ID group by subjekt.nazev order by     
        subjekt.nazev;   
    

    - 第二

        select count(*),subjekt.nazev from ZADAVACI_POSTUP,subjekt where
        ZADAVACI_POSTUP.id_zadavatel=subjekt.ID group by subjekt.nazev order by 
        subjekt.nazev;
    

    - 第三

        select    max(trunc(sysdate)-6) ode_dne_včetně, max(trunc(sysdate))  
        do_dne_včetně,nazev_organizace,count(*) pocet
        from(
        select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,   
        u.username,u.nazev, a.datumzapisauditu   
        ,to_char(a.datumzapisauditu,'DD.MM.YYYY')  datum , a.id
        from d$caudit a
        join cuzivatel u on u.id= a.id_uzivatel
    
        join osoba os on u.id_osoba_bridge = os.id
        join t$subjekt subj on subj.id = os.id_patri_do
        left join d$caudittyp t on t.id=a.id_audittyp
        where datumzapisauditu between trunc(sysdate)-7  AND  trunc(sysdate)
        order by  a.datumzapisauditu desc
        )
        group by nazev_organizace order by nazev_organizace ;
    

    - 第四

        select  sb.nazev, lg.create_uzivatel, lg.create_cas from Aplikacni_log                   lg 
        join zadavaci_postup zp on zp.id = lg.id_zp
        join subjekt sb on sb.id = zp.id_zadavatel
        where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY')
        order by sb.nazev asc
    

3 个答案:

答案 0 :(得分:2)

您可以使用子查询重构来实现此目的。

Edit1: - subjekt.nazev是四个查询之间的关系 您可以添加WHERE first_qry.nazev=second_qry.nazev以及与剩余查询类似的关系。

with first_qry as (select count(*),subjekt.nazev from osoba,subjekt where   
osoba.ID_PATRI_DO=subjekt.ID group by subjekt.nazev order by     
subjekt.nazev),

second_qry as (select count(*),subjekt.nazev from ZADAVACI_POSTUP,subjekt where
ZADAVACI_POSTUP.id_zadavatel=subjekt.ID group by subjekt.nazev order by 
subjekt.nazev),

third_qry as ( select max(trunc(sysdate)-6) 
ode_dne_včetně, max(trunc(sysdate))  
do_dne_včetně,nazev_organizace,count(*) pocet
from(
select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,   
u.username,u.nazev, a.datumzapisauditu   
,to_char(a.datumzapisauditu,'DD.MM.YYYY')  datum , a.id
from d$caudit a
join cuzivatel u on u.id= a.id_uzivatel

join osoba os on u.id_osoba_bridge = os.id
join t$subjekt subj on subj.id = os.id_patri_do
left join d$caudittyp t on t.id=a.id_audittyp
where datumzapisauditu between trunc(sysdate)-7  
AND  trunc(sysdate)
order by  a.datumzapisauditu desc
)
group by nazev_organizace order by nazev_organizace),

fourth_qry as (select  sb.nazev, lg.create_uzivatel, 
lg.create_cas from   Aplikacni_log                   lg 
join zadavaci_postup zp on zp.id = lg.id_zp
join subjekt sb on sb.id = zp.id_zadavatel
where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY')
order by sb.nazev asc)

select distinct a.*,b.*,c.*,d.* 
from first_qry a ,second_qry b,third_qry c,fourth_qry d

答案 1 :(得分:0)

昨天你问了一个类似的问题我用answer回答了它。

您可以使用与Union或Union all相同的方法,只为没有结果的每列选择null

select count(*) AS subjekt_count,subjekt.nazev ,null,null,null,null,null,null
--null columns represent the results from the other queries
from osoba,subjekt 
where  osoba.ID_PATRI_DO=subjekt.ID
group by subjekt.nazev  
UNION ALL
select null, null,count(*) AS subjekt_nazev_count,subjekt.nazev,null,null,null,null 
from ZADAVACI_POSTUP,subjekt where
ZADAVACI_POSTUP.id_zadavatel=subjekt.ID 
group by subjekt.nazev 
---and so on

还有其他方法使用WITH语句,但您需要在语句之间使用公共密钥,我不清楚您的查询是对同一个表或四个重叠查询的四个独占查询。你期望结果中有重复吗?

答案 2 :(得分:0)

您的所有查询都包含列nazev,它是分组和订单键,因此我假设这是加入结果的字段。 如果是这样,那么您可以使用follwoing SQL。如果这不是您想要的,那么请编辑问题,附加一些数据,表定义,最好是作为SQL Fiddle并准确地解释您的请求。

SQLFiddle

with q1 as (select count(*) cnt, subjekt.nazev
  from osoba,subjekt where osoba.ID_PATRI_DO=subjekt.ID 
  group by subjekt.nazev ),
q2 as (select count(*) cnt, subjekt.nazev 
  from ZADAVACI_POSTUP, subjekt 
  where ZADAVACI_POSTUP.id_zadavatel=subjekt.ID 
  group by subjekt.nazev ),
q3 as (select max(trunc(sysdate)-6) ode_dne_vcetne, 
    max(trunc(sysdate)) do_dne_vcetne, nazev_organizace nazev, count(*) pocet
  from (
    select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,   
        u.username, u.nazev, a.datumzapisauditu,
        to_char(a.datumzapisauditu,'DD.MM.YYYY') datum, a.id
      from d$caudit a
        join cuzivatel u on u.id= a.id_uzivatel
        join osoba os on u.id_osoba_bridge = os.id
        join t$subjekt subj on subj.id = os.id_patri_do
        left join d$caudittyp t on t.id=a.id_audittyp
      where datumzapisauditu between trunc(sysdate)-7  and trunc(sysdate) )
  group by nazev_organizace),
q4 as (select sb.nazev, lg.create_uzivatel, lg.create_cas 
  from aplikacni_log lg join zadavaci_postup zp on zp.id = lg.id_zp
    join subjekt sb on sb.id = zp.id_zadavatel
  where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY') )
select nazev, q1.cnt cnt1, q2.cnt cnt2, q3.ode_dne_vcetne, q3.do_dne_vcetne, 
    q3.pocet, q4.create_uzivatel, q4.create_cas
  from q1 left join q2 using (nazev) left join q3 using (nazev) left join q4 using (nazev)
  order by nazev, create_cas

样本数据的输出:

NAZEV      CNT1 CNT2 ODE_DNE_VCETNE DO_DNE_VCETNE POCET CREATE_UZIVATEL CREATE_CAS
---------- ---- ---- -------------- ------------- ----- --------------- ----------
SUBJEKT1      1    1                                                  1 2015-03-20 
SUBJEKT2      2    1                                                  1 2015-03-20