2016-05-31 10 views
-4

Ich habe einen Tabellennamen Benutzer. Es sei angenommen, in der Tabelle Ich habe folgende Felder:SQL group_by query

Benutzer-ID, UUID1, UUID2

Ich versuche, die folgende SQL-Abfrage erstellen die zurückkehren wird: .Anzahl der Zeilen einschließlich der gleichen UUID1 und UUID2. nicht, dass UUID1 gleich UUID2 ist, sondern nur die Nummer der Zeile, die beide enthält (GROUP BY) und zusätzlich die Anzahl der Zeilen UUID1 oder UUID2 enthält (Getrennt, keine Gruppierung).

So würde Ich mag eine Tabelle ausgegeben haben, wie folgt: UUID1, UUID2, Number_of_Rows_Contain_both, Number_Of_Rows_Contains_Only_Only_One

Jede Idee, wie kann ich eine solche Abfrage erzeugen?

+0

einige Beispieltabellendaten hinzufügen, und das erwartete Ergebnis. – jarlh

+0

Beispieldaten pls – theDbGuy

+0

Auch: welche DBMS benutzen Sie? –

Antwort

2

Hoffnung ist das, was Sie aksing für:

select 
user_id, 
sum 
(case when uuid1 is not null and uuid2 is not null then 1 else 0 end) both_uuid_avlbl, 
sum 
(case when uuid1 is not null then 1 else 0 end) uuid1_avlbl, 
sum 
(case when uuid2 is not null then 1 else 0 end) uuid2_avlbl 
from sample group by user_id; 

unten ist das Script DDL i für die obige Abfrage verwendet:

create table sample as (user_id number(10),uuid1 number(10),uuid2 number(10)); 
    Insert into sample (USER_ID,UUID1,UUID2) values (1,2,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (2,3,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (3,1,1); 
Insert into sample (USER_ID,UUID1,UUID2) values (4,1,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (5,2,0); 
Insert into sample (USER_ID,UUID1,UUID2) values (8,null,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (7,null,null); 
Insert into sample (USER_ID,UUID1,UUID2) values (6,null,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (3,null,1); 
Insert into sample (USER_ID,UUID1,UUID2) values (1,3,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (1,null,2); 
Insert into sample (USER_ID,UUID1,UUID2) values (5,3,0); 
Insert into sample (USER_ID,UUID1,UUID2) values (5,3,null); 
insert into sample (user_id,uuid1,uuid2) values (5,null,null); 

I verwendet 11g Oracle.

+0

Kann diesen Ansatz "Conditional Aggregation" nennen, obwohl ich nicht weiß, ob user_ID und group by benötigt wird, da OP nur nach Anzahl der Zeilen und "Getrennt, keine Gruppierung" fragt – xQbert

+0

Gruppierung entfernen wenn nicht erforderlich.Ich habe es einfach hinzugefügt Dinge leicht verständlich machen – theDbGuy

1

Für Ihr Ergebnis können Sie für diese gehen:

declare @both INT 
    declare @a INT 
    declare @b INT 
    declare @c INT 
    select @both = count(*) from users where UUID1 is not null and UUID2 is not null 
    select @a = count(*) from users where UUID1 is not null and UUID2 is null 
    select @b = count(*) from users where UUID1 is null and UUID2 is not null 
    select @c = count(*) from users where UUID1 is null and UUID2 is null 
    Select @both as BothCount,@a AS UUID1Count,@b AS UUID2Count,@c AS Bothnull 
0

Lassen Sie mich mein Verständnis für Ihre Frage an erster Stelle; Das Tabellenformat ist wie folgt;

und der Ausgang für eine solche Eingabe Probe, die Sie suchen, ist wie folgt;

a 1 1 
b 1 2 
c 0 2 
d 0 1 

Eine mögliche Lösung wäre;

SELECT UUID, sum(SAME) SAME, sum(DIFFERENT - SAME) DIFFERENT 
FROM (
    select a.UUID, 
     case when (a.UUID = t.UUID1 and a.UUID = t.UUID2) THEN 1 else 0 end SAME, 
     case when a.UUID = t.UUID1 or a.UUID = t.UUID2 THEN 1 else 0 end DIFFERENT 
    from 
    (
     select distinct UUID1 as UUID 
     from THE_TABLE 

     UNION 

     select distinct UUID2 as UUID 
     from THE_TABLE 
    ) a, THE_TABLE t 
) GROUP BY UUID 

mit DB2 v getestet 10.5.5