SQL question
Добавлено: 06 сен 2006, 13:28
Код: Выделить всё
CREATE TABLE Charge
(
id INTEGER NOT NULL,
code1 INTEGER NULL,
code2 INTEGER NULL,
code3 INTEGER NULL,
amount INTEGER NOT NULL
);
I think the following select is not the most efficient way to do it (#id#, #code1#, #code2#, #code3# are some values).
Код: Выделить всё
SELECT amount FROM
(
SELECT 1 AS n, amount FROM Charge
WHERE id=#id# AND code1=#code1# AND code2=#code2# AND code3=#code3#
UNION
SELECT 2 AS n, amount FROM Charge
WHERE id=#id# AND code1=#code1# AND code2=#code2# AND code3 is null
UNION
SELECT 3 AS n, amount FROM Charge
WHERE id=#id# AND code1=#code1# AND code2 is null AND code3=#code3#
UNION
SELECT 4 AS n, amount FROM Charge
WHERE id=#id# AND code1=#code1# AND code2 is null AND code3 is null
UNION
SELECT 5 AS n, amount FROM Charge
WHERE id=#id# AND code1 is null AND code2=#code2# AND code3=#code3#
UNION
SELECT 6 AS n, amount FROM Charge
WHERE id=#id# AND code1 is null AND code2=#code2# AND code3 is null
UNION
SELECT 7 AS n, amount FROM Charge
WHERE id=#id# AND code1 is null AND code2 is null AND code3=#code3#
UNION
SELECT 8 AS n, amount FROM Charge
WHERE id=#id# AND code1 is null AND code2 is null AND code3 is null
ORDER BY n
LIMIT 1) AS s
Thanks.