Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 两年前的结果_Sql_Postgresql - Fatal编程技术网

Sql 两年前的结果

Sql 两年前的结果,sql,postgresql,Sql,Postgresql,我在Postgres上,我有两个表table_a和table_b,它们的模式是: Table "public.table_a" Column | Type | Collation | Nullable | Default -----------------+-----------------------+-----------+----------+---------

我在Postgres上,我有两个表
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