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 个月(选择 a (2012) * 选择 b (2011) 的 12 个月..


编辑:抱歉缺少重要信息:

正在通过 odbc:jdbc 桥对 excel 表进行查询。

因为“from”子句总是这样:[sheet1$] 我无法创建任何连接或大小写:(

4

7 回答 7

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

于 2013-02-21T13:00:45.090 回答
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

于 2013-02-21T13:01:29.487 回答
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
于 2013-02-21T13:01:56.680 回答
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

您正在做的是交叉连接,将第一个表中的每一行与第二列的每一行组合在一起,其中将过滤掉与不同月份数匹配的行。

于 2013-02-21T13:58:51.820 回答
0

你想做一个自我加入。一般的想法是从同一个表中选择两次,但使用不同的别名。

select t1.something, t2.something
from yourtable t1 join yourtable t2 on something
etc
于 2013-02-21T13:00:48.483 回答
0

select a.month,(a.value-(select b.value from @tablename b where b.year = @year2 and b.month = a.month)) as diff from @tablename a where a.year = @year1

我想它会对你有所帮助。实际上,正如我们所知,我们只能在减法中使用两个数字,因此我在这里使用了 year1 和 year 2。将您的表名放在正确的位置

于 2013-02-21T13:19:34.527 回答
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

在我个人看来,内联视图比加入更好,虽然你无论如何都会加入,但它仍然更具可读性,更易于调试......

于 2013-02-21T14:24:57.253 回答