2010-05-25 17 views
5

Devo selezionare le colonne comuni c1, c2, c3 dal risultato di due sql satements.È necessario combinare il risultato comune di due istruzioni select

1)

select c1, c2, c3,count(c3) from (select * from form_name 
where data_created >'1273446000' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>1 

2)

select c1, c2, c3,count(c3) from (select * from form_name 
where data_created>'1272236400' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>2 

devo selezionare c1, c2, c3 tutti uguali e comuni trovato sia il risultato della ricerca.

come si può fare ... qualcuno può aiutare per favore?

+0

Si potrebbe utilizzare UNION per combinare i due query ad uno – MUG4N

risposta

5

rimuovere il count(c3) dalla lista di selezione, che possono differire (la clausola HAVING garantisce questo) e l'OP vuole solo confrontare C1, C2 e C3. Se la colonna COUNT(c3) è diversa, quali righe possono essere in comune? nessuno o alcuni, varierà. Rimuovere anche le tabelle derivate, non sono necessarie. In modo da provare:

select 
    c1, c2, c3 
    from form_name 
    where data_created >'1273446000' and data_creazione<'1274569200' 
    group by c1,c2, c3 
    having count(c3)>1 
INTERSECT 
select 
    c1, c2, c3 
    from form_name 
    where data_created>'1272236400' and data_creazione<'1274569200' 
    group by c1,c2, c3 
    having count(c3)>2 
+0

Sì..Questo è quello che volevo ..... Grazie KM – Anup

0

È possibile utilizzare le tabelle derivate e quindi unirle per ottenere i risultati desiderati.

select a.c1, a.c2, a.c3, a.acount, b.bcount 
From 
(select c1, c2, c3, count(*) as acount from (select * from form_name 
where data_created >'1273446000' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>1) a 
join 
(select c1, c2, c3, count(*) as bcount from (select * from form_name 
where data_created>'1272236400' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>2)b 
    on a.c1 = b.c1 and a.c2 = b.c2 and a.c3 = b.c3 
2

Hai provato a unire le 2 query con "UNION"?

es.

select c1, c2, c3,count(c3) from (select * from form_name 
where data_created >'1273446000' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>1  
union  
select c1, c2, c3,count(c3) from (select * from form_name 
where data_created>'1272236400' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>2 
+0

sì ho provato ... ma io voglio solo i risultati comuni ..... come distinta – Anup

+0

DELL'UNIONE dà tutte le righe, OP vuole solo il righe comuni –

2

Penso che INTERSECT risolverà il problema. Altre informazioni here.

select c1, c2, c3,count(c3) from (select * from form_name 
where data_created >'1273446000' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>1  
INTERSECT 
select c1, c2, c3,count(c3) from (select * from form_name 
where data_created>'1272236400' and data_creazione<'1274569200') 
group by c1,c2, c3 having count(c3)>2 
+0

INTERSECT fornisce righe comuni, ma questa query non funzionerà, vedere la mia risposta sul perché. –