日期:2014-05-19  浏览次数:20533 次

请问对以下的sql语句有没有解决办法?
select   aa,sum(qty)   as   qtytotal   from  
(
select   cc,qty   from   dd
union   all
select   ee,qty   from   ff

)a
group   by   aa

能不能把qtytotal=0的记录过滤掉???

------解决方案--------------------
-- 注意是否会出现NULL值

CREATE TABLE DD(CC NCHAR(10),QTY INT)
INSERT DD SELECT 'AB ',0
INSERT DD SELECT 'CC ',5
SELECT * FROM DD

CREATE TABLE FF(EE NCHAR(10),QTY INT)
INSERT FF SELECT 'CC ',0
SELECT * FROM FF

SELECT AA,SUM(QTY) AS QTYTOTAL FROM
(
SELECT CC AS AA ,QTY FROM DD
UNION ALL
SELECT EE AS AA,QTY FROM FF
)A
GROUP BY AA
HAVING SUM(QTY) <> 0

DROP TABLE DD
DROP TABLE FF