SELECT
sum(qty) as accept,
(SELECT sum(qty)
FROM pile
WHERE pf=false) as reject
FROM pile
WHERE pf=true;
这是我目前使用的SQL,但我猜它不是最佳做法?
我之前使用的另一种选择是SELECT sum(qty) FROM pile GROUP BY pf
,但我需要它们作为列而不是行。
还有其他解决方案吗?
答案 0 :(得分:8)
单人通过表格。
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;
答案 1 :(得分:2)
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
答案 2 :(得分:1)
没有更好,但更具可读性的IMO。
SELECT
accept = (SELECT sum(qty) FROM pile WHERE pf = true),
reject = (SELECT sum(qty) FROM pile WHERE pf = false)
答案 3 :(得分:0)
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
答案 4 :(得分:0)
SELECT (SELECT SUM(qty)
FROM pile
WHERE pf = true) AS accept,
(SELECT SUM(qty)
FROM pile
WHERE pf = false) AS reject