2012-07-29 104 views
1

您好我想從3個不同的表中獲得一個特定的值。每個表具有相同的結構,但包含一年不同時期的數據。在oracle中多選擇SELECT

Temp_Table_Jun (Contains data for June month) 
--------------- 
CustNo CustName Revenue 
1000  John  5.55 

與之相似,我有兩個更多的表

  • Temp_table_Apr
  • Temp_table_May

現在我運行一個查詢

select sum(Revenue)Rev_June from Temp_Table_Jun where CustNo='1000' 

得到的結果6月進行。現在我的問題是我想在單個查詢中獲取全部月份的收入詳情。

我需要這樣的東西,(這是錯誤的並不起作用)

select Rev_June,Rev_Apr,Rev_May,((Rev_June+Rev_Apr+Rev_May)/3)Avg_3_Mon from 
(
     select sum(Revenue)Rev_June from Temp_Table_Jun where CustNo='1000', 
     select sum(Revenue)Rev_Apr from Temp_Table_Apr where CustNo='1000', 
     select sum(Revenue)Rev_May from Temp_Table_May where CustNo='1000' 

) 

如何才能實現這一目標?我正在使用Oracle 10g。提前致謝。

回答

5

爲了讓你可以做到這一點通過以下方式中的數據。 Oracle 10g中沒有一個PIVOT但你可以使用CASE語句複製功能:

SELECT sum(case when month = 'June' then Rev END) June 
    , sum(case when month = 'May' then Rev END) May 
    , sum(case when month = 'April' then Rev END) April 
    , avg(Rev) as Average 
FROM 
(
    SELECT sum(revenue) Rev, 'June' as Month from rev_june where cust_no = '1000' 
    union all 
    SELECT sum(revenue) Rev, 'April' from rev_apr where cust_no = '1000' 
    union all 
    SELECT sum(revenue) Rev, 'May' from rev_may where cust_no = '1000' 
) x 

SQL Fiddle with Demo

或者你也可以做到以下幾點:

SELECT June, April, May, (June + April + May)/3 as Average 
FROM 
(
    select 
    (SELECT sum(revenue) from rev_june where cust_no = '1000') as June 
    , (SELECT sum(revenue) from rev_apr where cust_no = '1000') as April 
    , (SELECT sum(revenue) from rev_may where cust_no = '1000') as May 
    from dual 
) x 

SQL Fiddle with Demo

+1

+1,'avg(Rev)'和'sum(Rev)/ count(rev)'一樣好。 – 2012-07-29 21:40:22

+0

你是對的...再次! – Taryn 2012-07-29 23:04:40

+0

@bluefeet偉大的答案bluefeet - 我想不出如何做到從行到列的支點 – DavidHyogo 2012-07-30 06:22:34

1

我不知道,如果它在Oracle 10g中,但UNION查詢大致是以下通常會在數據庫中的工作,我曾經使用過:

select sum(Revenue) Rev_June from Temp_Table_Jun where CustNo='1000' 
UNION 
select sum(Revenue) Rev_Apr from Temp_Table_Apr where CustNo='1000' 
UNION 
select sum(Revenue) Rev_May from Temp_Table_May where CustNo='1000' 

我不crysal明確標點符號 - 你在末尾需要一個分號嗎? - 但那可能會讓你開始。

我想我會做到這一點略有不同,因爲每個查詢必須具有相同的結構UNION查詢工作:

select sum(Revenue) Revenue_Sum, 'Jun' Month from Temp_Table_Jun where CustNo='1000' 
UNION 
select sum(Revenue) Revenue_Sum, 'Apr' Month from Temp_Table_Apr where CustNo='1000' 
UNION 
select sum(Revenue) Revenue_Sum, 'May' Month from Temp_Table_May where CustNo='1000' 

現在,這並不完全獲得平均總和與3列,你正在尋找,但也許你可以用這種UNION查詢創建一個VIEW,然後在該VIEW上使用GROUP BY查詢來聚合數據。

+2

你想要UNION ALL而不是UNION。 UNION可能代價高昂,因爲它可識別並消除所有重複項。 – Codo 2012-07-29 15:09:46

+0

Thanks @Codo我不知道這件事。 – DavidHyogo 2012-07-29 15:11:33

+0

嗨大衛,我需要結果在不同的列,但與上述查詢我得到他們在同一列。可以做任何事情嗎? – 2012-07-29 15:22:42

3

下可能的工作:

select Rev_June, Rev_Apr, Rev_May, 
    (Rev_June + Rev_Apr + Rev_May)/3 Avg_3_Mon 
from (
    (select sum(Revenue) from Temp_Table_Jun where CustNo='1000') as Rev_June, 
    (select sum(Revenue) from Temp_Table_Apr where CustNo='1000') as Rev_Apr, 
    (select sum(Revenue) from Temp_Table_May where CustNo='1000') as Rev_May 
    from dual 
)