tags:

views:

50

answers:

5
SELECT 
    sum(qty) as accept,
    (SELECT sum(qty) 
     FROM pile 
     WHERE pf=false) as reject 
FROM pile 
WHERE pf=true;

That's the SQL I use currently, but I'm guessing its not best practice? The other alternative I used before was SELECT sum(qty) FROM pile GROUP BY pf but I need them as columns and not as rows.

Are there any other solutions?

+2  A: 
SELECT pileTrue.sumTrue as accept, pileFalse.sumFalse as reject
FROM
(SELECT sum(qty) sumFalse FROM pile WHERE pf=false) as pileFalse,
(SELECT sum(qty) sumTrue  FROM pile WHERE pf=true ) as pileTrue
eumiro
A: 

Not any better, but more readable IMO.

SELECT
accept = (SELECT sum(qty) FROM pile WHERE pf = true),
reject = (SELECT sum(qty) FROM pile WHERE pf = false)
Dustin Laine
A: 
  check it



  select max(accept), max(v2 ) 
    from
    (
              SELECT 
                    sum(qty) as accept,null v2  
                FROM pile 
                WHERE pf=true
                union
                 SELECT null accept,sum(qty) v2  
                     FROM pile 
                     WHERE pf=false
    )
    group by accept,v2  
AEMLoviji
A: 
SELECT (SELECT SUM(qty)
          FROM pile
         WHERE pf = true) AS accept,
       (SELECT SUM(qty)
          FROM pile
         WHERE pf = false) AS reject
zerkms
+5  A: 

Single pass through the table.

SELECT 
    sum(CASE WHEN pf = TRUE THEN qty ELSE 0 END) as accept,
    sum(CASE WHEN pf = FALSE THEN qty ELSE 0 END) as reject
FROM pile;
beach