我需要将以下在SQL Server中执行的查询转换为Oracle。
SELECT
LABEL_A, LABEL_B,
CASE
WHEN IS_C = '0' AND IS_B = 'S' THEN 'L1'
WHEN IS_C = '1' AND IS_B = 'B' THEN 'L1'
WHEN IS_C = '0' AND IS_B = 'B' THEN 'L2'
WHEN IS_C = '1' AND IS_B = 'S' THEN 'L2'
END AS L,
QTY
FROM
DATA
WHERE
PRICE > 0 AND E_DATE >= getdate()-1) AS LS
GROUP BY
LABEL_A, LABEL_B, L
HAVING
SUM(QTY/100) >= 150
你能帮我吗?
感谢。
答案 0 :(得分:3)
大多数错误都在这一行:
WHERE PRICE > 0 AND E_DATE] >= getdate()-1) AS LS
您有额外的]
和)
个字符,getdate()
被SYSDATE
取代,您需要删除该行末尾的别名。除此之外,您不能在GROUP BY
子句中使用列别名,并且需要使用整个case语句(或将其包装在子查询中)。
您还遇到QTY
不属于GROUP BY
子句的问题。
SELECT LABEL_A,
LABEL_B,
CASE
WHEN IS_C = '0' AND IS_B = 'S' THEN 'L1'
WHEN IS_C = '1' AND IS_B = 'B' THEN 'L1'
WHEN IS_C = '0' AND IS_B = 'B' THEN 'L2'
WHEN IS_C = '1' AND IS_B = 'S' THEN 'L2'
END AS L,
SUM( QTY ) AS QTY
FROM DATA
WHERE PRICE > 0
AND E_DATE >= SYSDATE-1
GROUP BY
LABEL_A,
LABEL_B,
CASE
WHEN IS_C = '0' AND IS_B = 'S' THEN 'L1'
WHEN IS_C = '1' AND IS_B = 'B' THEN 'L1'
WHEN IS_C = '0' AND IS_B = 'B' THEN 'L2'
WHEN IS_C = '1' AND IS_B = 'S' THEN 'L2'
END
HAVING SUM(QTY/100) >= 150
答案 1 :(得分:3)
从SQL Server移植到Oracle的主要挑战是后者不允许在SELECT
子句中使用GROUP BY
语句中的别名。但是,我们可以使用子查询来重用它,而不是重复冗长的CASE
表达式。
SELECT t.LABEL_A,
t.LABEL_B,
t.L,
SUM(t.QTY) AS QTY
FROM
(
SELECT LABEL_A,
LABEL_B,
CASE WHEN IS_C = '0' AND IS_B = 'S' THEN 'L1'
WHEN IS_C = '1' AND IS_B = 'B' THEN 'L1'
WHEN IS_C = '0' AND IS_B = 'B' THEN 'L2'
WHEN IS_C = '1' AND IS_B = 'S' THEN 'L2'
END AS L,
QTY
FROM DATA
WHERE PRICE > 0 AND E_DATE >= SYSDATE - 1
) t
GROUP BY t.LABEL_A, t.LABEL_B, t.L
HAVING SUM(t.QTY)/100 >= 150
答案 2 :(得分:1)
在这一行:
WHERE PRICE > 0 AND E_DATE] >= getdate()-1) AS LS
删除此处]
和)
(在MS中似乎也无效)并将getDate()
替换为sysdate
。休息应该没问题,除了group by需要用L
语句替换CASE
。