SQL Server到PL / SQL

时间:2011-03-19 20:13:17

标签: sql sql-server plsql

这是需要转换为Oracle 11g的sql server脚本:

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 locale, everecondis

如何在Oracle 11g上运行PL / SQL? (PL / SQL新手)

1 个答案:

答案 0 :(得分:3)

查询似乎没有意义,因为s.enr1yr >= 1子句中的WHERE排除了您稍后尝试计算的记录,但是在SQL Server和Oracle AFAIK中都应该有效的文字重写是

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  

我将WHERE子句更改为s.enr1yr IN (0,1),因为这似乎是最可能的理想语义。如果不是这样,请告诉我!