SQL Server to PL/SQL
This is sql server script that needs to be turned into Oracle 11g:
select p.locale, q.everecondis, [0] as No, [1] as Yes,
[0]* 100.0/([0] + [1]) as [pctNo],
[1]* 100.0/([0] + [1]) as [pctYes]
from
(select p.locale, q.everecondis, s.enr1yr
from s, q, p
where s.student_info_key = q.student_info_key
and s.pse_school_key = p.pse_school_key and s.enr1yr >=1) as tbl
pivot
(count(enr1yr) for enr1yr in ([0],[1])) as pvt
order by local开发者_运维知识库e, everecondis
How do I get this to run in PL/SQL on Oracle 11g? (New to PL/SQL)
The query doesn't seem to make sense as the s.enr1yr >= 1
in the WHERE
clause excludes records that you are later trying to count but a literal rewrite that should work in both SQL Server and Oracle AFAIK is
SELECT p.locale,
q.everecondis,
COUNT(CASE WHEN s.enr1yr = 0 THEN 1 END) AS No,
COUNT(CASE WHEN s.enr1yr = 1 THEN 1 END) AS Yes,
AVG(CASE WHEN s.enr1yr = 0 THEN 100.0 ELSE 0.0 END) AS [pctNo],
AVG(CASE WHEN s.enr1yr = 1 THEN 100.0 ELSE 0.0 END) AS [pctYes]
FROM s
JOIN q
ON s.student_info_key = q.student_info_key
JOIN p
ON s.pse_school_key = p.pse_school_key
WHERE s.enr1yr IN (0,1)
GROUP BY p.locale,
q.everecondis
ORDER BY p.locale,
q.everecondis
I changed the WHERE
clause to s.enr1yr IN (0,1)
as that seems by far the most probable desired semantics. Let me know if this is not the case!
精彩评论