1

如何划分嵌套查询的两个结果?我的意思是:我有一个存储过程

SELECT s.sheep_id,
       ( s.sheepThatHadBirth / ( s.sheepThatHadBirth + s1.sheepThatNotHadBirth ) ) * 100
FROM   (SELECT sheep_id,
               Count(*) AS sheepThatHadBirth
        FROM   FemaleSheep_Has_Wave
        WHERE  pregnencies_succeded = 1
        GROUP  BY sheep_id)AS s
       CROSS JOIN (SELECT sheep_id,
                          Count(*) AS sheepThatNotHadBirth
                   FROM   FemaleSheep_Has_Wave
                   WHERE  pregnencies_succeded = 0
                   GROUP  BY sheep_id)AS s1 

结果不正确,因为交叉加入我想请帮助谢谢

4

1 回答 1

0

JOIN你在这里根本不需要

SELECT sheep_id,
       100.0 * Sum(CASE
                     WHEN pregnencies_succeded = 1 THEN 1
                     ELSE 0
                   END) / Count(*) AS pregnencies_succeded_pct
FROM   FemaleSheep_Has_Wave
GROUP  BY sheep_id 
于 2012-07-22T17:40:47.907 回答