2012-10-16 2 views
3

J'ai trois requêtes select qui renvoient des enregistrements totaux, des enregistrements réussis et des enregistrements d'échec de la même table en fonction de différentes clauses where. Je veux rejoindre le résultat de toutes ces déclarations dans une table afin de rendre ma procédure stockée, mais la table résultante ont trois colonnes différentes pour cdr, le succès, l'échecComment joindre le résultat de deux instructions sql dans une table et des colonnes différentes

SELECT Count(*) AS cdr 
FROM ABC AS c WITH (NOLOCK) 
WHERE APPID IN(1, 2) 
     AND CALLDATE = '2012-10-09' 

SELECT Count(*) AS success 
FROM ABC AS d WITH (NOLOCK) 
WHERE APPID IN(44, 45) 
     AND CALLDATE = '2012-10-09' 
     AND HANGUPCODE IN ('man', 'mach') 

SELECT Count(*) AS fail 
FROM ABC WITH (NOLOCK) 
WHERE APPID IN(44, 45) 
     AND CALLDATE = '2012-10-09' 
     AND HANGUPCODE NOT IN ('man', 'mach') 

Union donne le résultat dans une colonne donc ça ne marchera pas. d'autres idées

+1

Je ne comprends pas, vous voulez un ResultSet avec une seule ligne et 3 colonnes? –

+0

@DylanSmith oui –

Répondre

7

Juste Envelopper chaque instruction select entre parenthèses, donnent à chaque instruction select un alias, et utiliser SELECT en haut:

SELECT 
    (select count(*) as cdr 
    from abc as c with (nolock) 
    where appid in(1,2) and calldate = '2012-10-09' 
) AS Column1, 
    (select count(*) as success 
    from abc as d with (nolock) 
    where appid in(44,45) and calldate = '2012-10-09' 
     and hangupcode in ('man', 'mach') 
) AS Column2, 
    (select count(*) as fail 
    from abc with (nolock) 
    where appid in(44,45) and calldate = '2012-10-09' 
     and hangupcode not in ('man', 'mach') 
) AS Column3 

Fondamentalement, vous traitez chaque requête comme une colonne individuelle.

0
SELECT a.cdr, b.success, c.failure FROM 
    (SELECT count(*) AS cdr 
    FROM abc as c WITH (NOLOCK) 
    WHERE appid IN (1,2) AND 
     calldate = '2012-10-09' 
) AS a, 
    (SELECT count(*) AS success 
    FROM abc AS d WITH (NOLOCK) 
    WHERE appid IN (44,45) AND 
     calldate = '2012-10-09' AND 
     hangupcode IN ('man', 'mach') 
) AS b, 
    (SELECT count(*) AS fail 
    FROM abc WITH (NOLOCK) 
    WHERE appid IN (44,45) AND 
     calldate = '2012-10-09' AND 
     hangupcode NOT IN ('man', 'mach') 
) AS c 
1
select a.cdr, b.success, c.fail from 
(select count(*) as cdr 
from abc as c with (nolock) where appid in(1,2) 
and calldate = '2012-10-09') a 
, (select count(*) as success 
from abc as d with (nolock) where appid in(44,45) 
and calldate = '2012-10-09' 
and hangupcode in ('man', 'mach')) b 
, (select count(*) as fail from abc with (nolock) where appid in(44,45) and calldate = '2012-10-09'and hangupcode not in ('man', 'mach')) c 
1
select 
    sum(case when appid in(1,2) and calldate = '2012-10-09' then 1 else 0 end) as cdr, 
    sum(case when appid in(44,45) and calldate = '2012-10-09'and hangupcode in ('man', 'mach') then 1 else 0 end) as success, 
    sum(case when appid in(44,45) and calldate = '2012-10-09'and hangupcode not in ('man', 'mach') then 1 else 0 end)as fail 
from abc 
Questions connexes