简体   繁体   中英

How to join result of two sql statements into one table and different columns

I have three select queries which return total records , succesful records and failure records from same table based on different where clauses . I want to join the result of all these statements into one table in order to make my stored procedure but the resulting table shall have three different columns for cdr , success , failure

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 gives out the result in one column so it won't work . any other ideas

Just wrap each select statement in parentheses, give each select statement an alias, and use SELECT at the top:

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

Basically you're treating each query as an individual column.

  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
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
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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM