2015-12-17 17 views
5

Bir tablonun aynı alanında birden fazla filtre içeren sql sorgularını yazmak için yardıma ihtiyacınız var.SQL sorgusu, belirtilen çocukları içermeyen üst (iş) almak için (durum)

Aşağıda gösterildiği gibi 2 tablom var.

Job tablo:

ID JobId Name  StartTime    FinishTime 
01 001  A  2105:12:10 14:00:00 2105:12:10 14:00:10 
02 002  A  2105:12:10 14:00:00 2105:12:10 14:00:00 
03 003  A  2105:12:10 14:00:00 2105:12:10 14:00:00 
04 004  A  2105:12:10 14:00:00 2105:12:10 14:00:00 

ve

Status tablosu: Ben halleri bulunan işler için sorgulamak zorunda bu 2 tablolardan

ID Status    Timestamp    JobId 
01 Started    2105:12:10 14:00:00  001 
02 Step_1_Started  2105:12:10 14:00:00  001 
03 Step_1_Finished  2105:12:10 14:00:05  001 
04 Step_2_Started  2105:12:10 14:00:05  001 
05 Step_2_Finished  2105:12:10 14:00:10  001 
06 Finished    2105:12:10 14:00:10  001 
........................................................ 
07 Started    2105:12:10 14:00:00  002 
08 Step_1_Started  2105:12:10 14:00:00  002 
09 Step_1_Failed   2105:12:10 14:00:02  002 
........................................................ 
10 Started    2105:12:10 14:00:00  003 
11 Step_1_Started  2105:12:10 14:00:00  003 
12 Step_1_Failed   2105:12:10 14:00:02  003 
13 Step_1_Canceled  2105:12:10 14:00:04  003 
........................................................ 
14 Started    2105:12:10 14:00:00  004 
15 Step_1_Started  2105:12:10 14:00:00  004 

, İPTAL BAŞARISIZ BİTMİŞ ve AKTİF nerede

  • BİTMİŞ: 'Tamamlandı' durumuna sahip bir iş.
  • İPTAL EDİLDİ: '%' İptal 'durumundaki ancak' 'Bitir' 'durumundaki bir iş.
  • BAŞARISIZ: '% Başarısız' durumuna sahip olan ancak '' İptal '' veya '' Bitir '' durumundaki bir iş.
  • Etkin: '% Started' durumuna sahip olan ancak '('% 'Başarısız' veya '%' İptal 'veya' Tamamlandı ') durumundaki bir iş.

diğer sorguları için yardıma mı ihtiyacınız

SELECT 
    j.jobid 
FROM 
    Job j 
JOIN 
    status js ON j.jobid = js.jobid 
WHERE 
    j.startTime >= '2015:12:10' 
    AND j.startTtime < '2015:12:20' 
    AND js.status = 'Finished'; 

çalışıyor Finished için aşağıdaki SQL sorgusu var.

Beklenen çıkışı: önceden

FINISHED: 001 
CANCELED: 003 
FAILED: 002 
Active: 004 

teşekkürler.

+1

Oracle or mysql? Bunlar iki farklı ürün. Şimdiye kadar ne yaptın? – Shadow

+0

iç içe geçmiş kullan, herhangi bir denemeye yardım edebilir mi? –

+0

Veritabanı özel sorgu istemiyorum, böylece Oracle ve mysql ekledim. – jitk

cevap

1

Oracle için versiyonudur:

with jobList (jobid, steps) as (
select jobid, listagg(Status, ' ') WITHIN GROUP (ORDER BY id) from job_status 
group by jobid) 
select 'FINISHED:' as Status , listagg(jobid, ' ') WITHIN GROUP (ORDER BY jobid) from jobList 
where instr(steps, 'Finished') > 0 
union all 
select 'CANCELED:' as Status , listagg(jobid, ' ') WITHIN GROUP (ORDER BY jobid) from jobList 
where instr(steps, 'Finished') = 0 and instr(steps, 'Canceled') > 0 
union all 
select 'FAILED:' as Status , listagg(jobid, ' ') WITHIN GROUP (ORDER BY jobid) from jobList 
where instr(steps, 'Failed') > 0 and instr(steps, 'Canceled') = 0 and instr(steps, 'Finished') = 0 
union all 
select 'Active:' as Status , listagg(jobid, ' ') WITHIN GROUP (ORDER BY jobid) from jobList 
where instr(steps, 'Started') > 0 and instr(steps, 'Failed') = 0 and instr(steps, 'Canceled') = 0 and instr(steps, 'Finished') = 0 

Temelde ben steps denir her jobid bire dize için tüm durumları koydu. Bundan sonra, belirli bir durum var mı, yoksa dizgede arama yapıyorum. Bu kriterler için birden fazla jobid olabileceği için sonucu string olarak değiştirmek için 'u tekrar kullanıyorum. 2 bitmiş işiniz olacaksa (1 ve 5 numaralı kimlikle), SQL Fiddle numaralı örnekle MySql sürümü için FINISHED: 1 5

göreceksiniz. MySql'de WITH olmadığı için biraz daha uzun.

select 'FINISHED:' as Status , 
    group_concat(a.jobid separator ' ') as jobList 
from 
    (select jobid, 
      group_concat(Status separator ' ') steps 
     from job_status 
     group by jobid) a 
where instr(steps, 'Finished') > 0 
union all 
select 'CANCELED:' as Status , 
    group_concat(a.jobid separator ' ') as jobList 
from 
    (select jobid, 
      group_concat(Status separator ' ') steps 
     from job_status 
     group by jobid) a 
where instr(steps, 'Finished') = 0 and 
     instr(steps, 'Canceled') > 0 
union all 
select 'FAILED:' as Status , 
    group_concat(a.jobid separator ' ') as jobList 
from 
    (select jobid, 
      group_concat(Status separator ' ') steps 
     from job_status 
     group by jobid) a 
where instr(steps, 'Failed') > 0 and 
     instr(steps, 'Canceled') = 0 and 
     instr(steps, 'Finished') = 0 
union all 
select 'Active:' as Status , 
    group_concat(a.jobid separator ' ') as jobList 
from 
    (select jobid, 
      group_concat(Status separator ' ') steps 
     from job_status 
     group by jobid) a 
where instr(steps, 'Started') > 0 and 
     instr(steps, 'Failed') = 0 and 
     instr(steps, 'Canceled') = 0 and 
     instr(steps, 'Finished') = 0