您的查询无效,因为您在表上执行
CROSS JOIN(是因为
,),因此每一行都与另一个表中的每一行都匹配,而不是
INNER JOIN一个月匹配。
修改查询:
select a.month, a.value-b.valuefrom (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.valuefrom yourTable a join yourTable b on a.month = b.month where a.year = 2012 and b.year = 2011
每年每月多行:
select a.month, a.value-b.valuefrom (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条)