我有一个SQL查询,我无法以我想要的方式显示。基本上,我们已经有了一张充满交易的表格,我希望从今年2月份开始购买一笔交易(特定类型的交易)。下面是该查询:显示SELECT查询结果 - 从相同的表 - 并排
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
现在,我也想看看这些相同的交易的总和,但前一年:
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
似乎我无法弄清楚是如何并行获得这些总和。我试过UNION all
,但这些显示在单独的行中,而不是列。
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
UNION all
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
我也读到这里对堆栈溢出是PIVOT
可能是一种选择,但我还没有看到,我可以操控/调整上面查询的例子。
有关我如何并行获取此数据的任何建议?我确信我忽略了一些简单的东西。
非常感谢!
你永远只能去本年度来比较以前还是有这个方程的变量?如果没有其他变量,那么我不明白为什么你需要使用PIVOT,波希米亚的例子就足够了。 – deutschZuid