我试图在嵌套情况下从abcdef表中检索值,但在SELECTS MENTIONED ----错误中出现“单行子查询返回多个行”错误。 情况1:当colC值为NOT NULL且没有值('LM','OP','QR')时,当colD为NULL时,colD值介于'00'和'99'之间 情况2:当colC值为NOT NULL且何时具有值('LM','OP','QR')时,当colD为NULL时返回该行,或者当colE为null时返回该行
如何重写此查询以获得正确的结果
Table abcdef
colfKEY | colA | colB | colC | colD | colE
Table maintable
colPKEY | colTemp
select
a.colA , a.colB, a.colC,
CASE
WHEN a.colC IS NULL THEN ''
WHEN a.colC IS NOT NULL THEN
(CASE
WHEN (a.colC NOT IN ('LM','OP','QR')) THEN
(CASE WHEN (a.colD IS NULL) THEN
(SELECT T.colD FROM abcdef T where T.colD IS NULL) ---- error here
WHEN TT.colD IS NOT NULL THEN
(SELECT TT.colD from abcdef TT WHERE (TT.colD < '00') OR (TT.colD > '99') ) ---- error here
END)
WHEN (a.colC IN ('LM','OP','QR')) THEN
(CASE WHEN a.colD IS NULL THEN
(SELECT a.colD FROM abcdef a) ---- error here
WHEN a.colE IS NULL THEN
(SELECT a.colE FROM abcdef a) ---- error here
END )
END)
END
from abcdef a where
a.colfKEY IN
(SELECT m.colPKEY FROM maintable m where
m.colTemp= '999999');
答案 0 :(得分:0)
不用子查询重写查询:
SELECT a.colA,
a.colB,
a.colC,
CASE
WHEN a.colC IS NULL THEN
''
WHEN a.colC IS NOT NULL THEN
(CASE
WHEN (a.colC NOT IN ('LM', 'OP', 'QR')) THEN
(CASE
WHEN (a.colD IS NULL) THEN
a.colD
WHEN a.colD IS NOT NULL AND ((a.colD < '00') OR (a.colD > '99')) THEN
a.colD
END)
WHEN (a.colC IN ('LM', 'OP', 'QR')) THEN
(CASE
WHEN a.colD IS NULL THEN
a.colD
WHEN a.colE IS NULL THEN
a.colE
END)
END)
END
FROM abcdef a
JOIN maintable m
ON a.colfKEY = m.colPKEY
WHERE m.colTemp = '999999';