如何划分嵌套查询的两个结果?我的意思是:我有一个存储过程
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
结果是不对的,因为我觉得交叉加入 请帮助谢谢
答案 0 :(得分: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