2013-02-21 20 views
0

行有一個表作爲之間計算:如何不同組同桌

Year Month Value 
2011 1  500 
2011 2  550 
2011 3  600 
... 
... 
2012 1  600 
2012 2  750 
2012 3  930 

有沒有一種方法我可以計算同月/不同年份的價值之間的差額,所以得到的結果如:

Month Value 
1  100 
2  200 
3  330 
... 

我試着這樣做:

select month, a.value-b.value 
from 
    (select month, value from table where year = 2012) a, 
    (select month, value from table where year = 2011) b 

但輸出爲12個月(中選擇(2012)*12個月選擇B(2011)的..


編輯:

的查詢正在通過ODBC一個excel片製成:爲缺乏的重要信息抱歉JDBC橋。

爲「從」的條款總是這樣的:[工作表Sheet1 $]我不能夠創建任何連接或情況:(

+1

你想要的輸出是什麼? – DevelopmentIsMyPassion 2013-02-21 12:59:45

回答

1

您的查詢不工作,因爲你在表上做一個CROSS JOIN(因爲,),因此各行中的其他表的每一行匹配,而不是在每月INNER JOIN

修改您的查詢:

select a.month, a.value-b.value 
from 
    (select month, value from table where year = 2012) a 
    JOIN 
    (select month, value from table where year = 2011) b 
    ON a.month = b.month 

更快的查詢:

select a.month, a.value-b.value 
from 
    yourTable a 
    join yourTable b 
    on a.month = b.month 
    where a.year = 2012 and b.year = 2011 

如果每月多行每年爲:

select a.month, a.value-b.value 
from 
    (select month, sum(value) as value 
    from yourTable where year = 2012 
    group by month) a 
    join 
    (select month, sum(value) as value 
    from yourTable where year = 2011 
    group by month) b 
    on a.month = b.month 

SQLFiddle

+0

謝謝!它使用你的冷杉建議! – 2013-02-21 13:35:38

3

你非常接近如何得到的結果我想建議使用類似這樣的一些東西,樞軸數據爲列,然後你可以把每年的區別:

select month, 
    Year2012-Year2011 as Diff 
from 
(
    select month, 
    sum(case when year = 2011 then value end) Year2011, 
    sum(case when year = 2012 then value end) Year2012 
    from yourtable 
    group by month 
) src  

SQL Fiddle with Demo

+0

當他有2013年,2014年等等時會發生什麼。我們可以有廣義查詢嗎? – DevelopmentIsMyPassion 2013-02-21 13:04:14

+0

@AshReva您可以創建一個動態查詢來生成年份列表,但是OP必須陳述他們正在使用的RDBMS,因爲每種語法的語法都不相同。 – Taryn 2013-02-21 13:06:21

+0

hsan給出的查詢很好。你怎麼看? – DevelopmentIsMyPassion 2013-02-21 13:08:24

0

你想要做自連接的總體思路是從同一個表中選擇兩次,但使用不同的別名。

1

這樣的事情?假設year/month是唯一的。

SELECT 
    a.month, 
    a.value - b.value 
FROM 
    tablename a 
    INNER JOIN tablename b 
     ON a.year - 1 = b.year 
     AND a.month = b.month 
0

選擇a.month,(a.value-(從@tablename b。選擇所需b.value其中b.year = @ YEAR2和b.month = a.month))作爲DIFF 從@tablename一個 其中a.year = @ year1

我認爲它會幫助你。事實上,我們知道我們只能在減法中使用兩個數字,所以我在這裏使用了1年和2年。把你的表名在適當的地方

1

只需添加where子句查詢..

select month, a.value-b.value 
from 
    (select month, value from table where year = 2012) a, 
    (select month, value from table where year = 2011) b 
where a.month = b.month 

你在做什麼是交叉連接,使得各行的組合從第一臺與第二列的每一行在哪裏會過濾出與不同的月份號碼匹配的行。

0

此外,如果您的示例中每月僅有2個值,並且您的SQL版本中提供了分析函數,那麼這將起作用。您可能有不同的分區呢...:

SELECT distinct month, (l_val - f_val) value 
    FROM 
    (
    SELECT year, month, month_val 
     , FIRST_VALUE(month_val) OVER (PARTITION BY month ORDER BY month) f_val 
     , LAST_VALUE(month_val) OVER (PARTITION BY month ORDER BY month) l_val 
    FROM your_table 
) 
ORDER BY 1 
/

MONTH VALUE 
---------------- 
1  100 
2  200 
3  330 

我個人認爲聯視圖是更好地使用再加入,雖然你無論如何都會加入,但它仍然是更具可讀性的廣告更容易調試......