Sql 两年前的结果
我在Postgres上,我有两个表Sql 两年前的结果,sql,postgresql,Sql,Postgresql,我在Postgres上,我有两个表table_a和table_b,它们的模式是: Table "public.table_a" Column | Type | Collation | Nullable | Default -----------------+-----------------------+-----------+----------+---------
table_a
和table_b
,它们的模式是:
Table "public.table_a"
Column | Type | Collation | Nullable | Default
-----------------+-----------------------+-----------+----------+---------
name | character varying(10) | | |
asof_yrmo | integer | | |
Table "public.table_b"
Column | Type | Collation | Nullable | Default
-----------+-----------------------+-----------+----------+---------
name | character varying(10) | | |
comp | character varying(1) | | |
asof_yrmo | integer | | |
以下是每个表的一些示例数据:
select name, asof_yrmo from table_a limit 10;
name | asof_yrmo
--------+-----------
Will | 202101
James | 202101
Samuel | 202101
John | 202101
George | 202101
Will | 202012
James | 202012
Samuel | 202012
John | 202012
George | 202012
(10 rows)
select name, comp, asof_yrmo from table_b limit 10;
name | comp | asof_yrmo
--------+------+-----------
Will | Y | 202101
James | Y | 202101
Samuel | Y | 202101
John | N | 202101
George | N | 202101
Will | N | 202012
James | N | 202012
Samuel | Y | 202012
John | Y | 202012
George | Y | 202012
(10 rows)
有趣的列是asof_yrmo
,它的值包括202101
和201901
(基本上,它是年和月)
我有以下SQL查询:
select
a.name,
a.asof_yrmo,
plu.comp
from
table_a as a
left join
table_b as plu
on plu.name = a.name
and plu.asof_yrmo = a.asof_yrmo
where
a.asof_yrmo =
(
select
max(s60.asof_yrmo)
from
table_a as s60
)
group by
a.name,
a.asof_yrmo,
plu.comp;
其结果是:
name | asof_yrmo | comp
--------+-----------+------
George | 202101 | N
James | 202101 | Y
John | 202101 | N
Samuel | 202101 | Y
Will | 202101 | Y
(5 rows)
我想做的是在末尾有另一列,名为comp\u from\u previous\u two\u years
,显示前2年的comp值。因此,如果我们有202101
用于asof_yrmo
,则该列将显示201901
的comp
值
因此,预期产出将为:
name | asof_yrmo | comp | comp_from_previous_two_years
--------+-----------+------+------------------------------
George | 202101 | N | Y
James | 202101 | Y | N
John | 202101 | N | N
Samuel | 202101 | Y | Y
Will | 202101 | Y | N
我知道我必须抓住这一年的部分,我已经设法做到这一点与
左(演员阵容(asof_yrmo as varchar),4)
但我真的在努力创建联盟,以显示两者。你需要通过使用条件组来做一个轴心。这可以使用过滤器
子句完成:
name
和asof_yrmo
FILTER
子句仅识别一个asof_yrmo
值中的值。不要介意MAX()
函数。这是必要的,因为我们实际上做了一个组
聚合。但是因为这里只有一个数据集,MAX(x)
等于x
asof_yrmo
值也可以这样做ASOF_YRMO
列包含年和月作为YYYYMM整数,则前两年的数据具有“年-月”值,当前值为-200,因为年的低位数字位于值的百位。因此,您的查询应该如下所示:
select a.name,
a.asof_yrmo,
b.comp,
bb.comp AS comp_from_previous_two_years
from table_a as a
left join table_b as b
on b.name = a.name and
b.asof_yrmo = a.asof_yrmo
LEFT OUTER JOIN table_b AS BB
ON bb.name = a.name AND
bb.ASOF_YRMO = b.ASOF_YRMO - 200
where a.asof_yrmo = (select max(s60.asof_yrmo)
from table_a as s60)
ORDER BY a.name
请提供一些样本数据和预期输出。请考虑最小化您的示例-我想,没有必要向我们展示11列带有神秘名称的列来解释您的问题…公平,我现在就这么做。@S-Man我已经做了,让事情更简单了,但是我该如何提供一些示例数据呢?您可以像处理结果一样:只需向我们显示生成结果的表格数据。请解释产生“是/否”的逻辑谢谢,我已经为每个表添加了一些示例数据。comp字段的Y/N没有逻辑。它只是列中存在的一个值。这非常聪明!非常感谢你。
select a.name,
a.asof_yrmo,
b.comp,
bb.comp AS comp_from_previous_two_years
from table_a as a
left join table_b as b
on b.name = a.name and
b.asof_yrmo = a.asof_yrmo
LEFT OUTER JOIN table_b AS BB
ON bb.name = a.name AND
bb.ASOF_YRMO = b.ASOF_YRMO - 200
where a.asof_yrmo = (select max(s60.asof_yrmo)
from table_a as s60)
ORDER BY a.name