Answer the question
In order to leave comments, you need to log in
What variant of request from two is more productive?
CREATE TABLE TempForTest (
id INT,
Cus INT,
Nm VARCHAR(10),
Sm NUMBER,
Tp Number
);
INSERT INTO TempForTest VALUES (1, 1, 'Ivan', 100, 0);
INSERT INTO TempForTest VALUES (2, 1, 'Ivan', 150, 0);
INSERT INTO TempForTest VALUES (3, 1, 'Ivan', 400, 1);
INSERT INTO TempForTest VALUES (4, 2, 'Petr', 100, 0);
INSERT INTO TempForTest VALUES (5, 2, 'Petr', 350, 0);
INSERT INTO TempForTest VALUES (6, 2, 'Petr', 5, 0);
INSERT INTO TempForTest VALUES (7, 2, 'Petr', 15, 1);
INSERT INTO TempForTest VALUES (8, 2, 'Petr', 35, 1);
Cus Nm Sm0 Sm1 Raz
1 Ivan 250 400 -150
2 Petr 455 50 405
SELECT Cus, Nm, SUM(TP0) AS Sm0, SUM(TP1) AS Sm1, SUM(TP0-TP1) AS Raz FROM (
SELECT Cus, Nm, TP0, TP1 FROM (
SELECT Cus, Nm,
SUM(DECODE(Tp, 0, Sm, 0)) OVER (PARTITION BY Cus, Tp) AS TP0
,SUM(DECODE(Tp, 1, Sm, 0)) OVER (PARTITION BY Cus, Tp) AS TP1
FROM TempForTest
GROUP BY Cus, Nm, Tp, Sm )
GROUP BY Cus, Nm, TP0, TP1)
GROUP BY Cus, Nm
WITH
TP0 AS (
SELECT Cus, Nm, SUM(Sm) AS SM
FROM TempForTest
WHERE Tp = 0
GROUP BY Cus, Nm),
TP1 AS (
SELECT Cus, Nm, SUM(Sm) AS SM
FROM TempForTest
WHERE Tp = 1
GROUP BY Cus, Nm)
SELECT TP0.Cus, TP0.Nm, TP0.SM AS SM0, TP1.SM AS SM1, TP0.SM - TP1.SM AS RAZ
FROM TP0
JOIN TP1 ON TP1.Cus = TP0.Cus
Answer the question
In order to leave comments, you need to log in
In the first query, the middle grouping can be removed, it will become even easier. I like him more. Without seeing the plan, one has to speculate purely from experience.
If your version of Oracle doesn't support pivot , then with isn't very performant, so it should be avoided if possible. In my past practice, I was of the same opinion. But I looked at the plans. Are you looking at plans? What's in them?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question