假設我有這張表SELECT SUM as field
表(a,b,c,d)。數據類型不重要。
我想這樣做
select a as a1,b as b1,c as c1,
(select sum(d) from table where a=a1 and b=b1) as total
from table
group by a,b,c
...但我不能找到一種方法(「FROM子句中未找到」的SQLDeveloper保持與抱怨。)
有沒有辦法?可能嗎?
假設我有這張表SELECT SUM as field
表(a,b,c,d)。數據類型不重要。
我想這樣做
select a as a1,b as b1,c as c1,
(select sum(d) from table where a=a1 and b=b1) as total
from table
group by a,b,c
...但我不能找到一種方法(「FROM子句中未找到」的SQLDeveloper保持與抱怨。)
有沒有辦法?可能嗎?
SELECT a as a1,b as b1,c as c1,
(
SELECT SUM(d)
FROM mytable mi
WHERE mi.a = mo.a
AND mi.b= mo.b
) as total
FROM mytable
GROUP BY
a, b, c
它更簡單,高效重寫它作爲這樣的:
SELECT a AS a1, B AS b1, c AS c1, SUM(SUM(d)) OVER (PARTITION BY a, b) AS total
FROM mytable
GROUP BY
a, b,
注意SUM(SUM(d))
這裏。
最內層的SUM
是聚合函數。它會計算SUM(d)
a-b-c
-wise。
最外層的SUM
是分析函數。它將預先計算的SUM(d)
的a-b
相加,然後返回每一行的值。
杜你的意思是這樣的?
select a as a1,
b as b1,
c as c1,
sum(sum(d)) OVER (PARTITION BY a, b) AS total
from table
group by a,b,c
你可以用別名做到這一點:
SELECT a AS a1, b AS b1, c AS c1,
(SELECT SUM(d)
FROM test_t t_in
WHERE t_in.a = t.a
AND t_in.b = t.b) AS total
FROM test_t t
GROUP BY a, b, c
謝謝你們。我得到的東西比我想要的要多得多,並且知道我會閱讀有關分區和分區的內容。這是我喜歡的。 – Tom 2009-10-08 15:38:25
多數民衆贊成在「現在我會讀...」 – Tom 2009-10-08 15:54:38