根据条件从python中的数据库服务器中提取选择性数据

时间:2018-05-14 08:05:24

标签: python postgresql psycopg2

我有以下数据:

per_id      month_id    area_grp    area_hrs    level
754         201803      WNY         19.93       OVue
754         201802      MT          0.52        OVue
754         201802      WBS         0.34        OVue
754         201801      WYW         33.04       OVue
754         201801      VC          16.62       OVue
754         201801      PX          3.05        OVue
754         201712      RZN         5.05        OVue
754         201711      WYW         17.85       OVue
754         201711      NLN         0.8         OVue
754         201711      DJr         45.67       OVue
754         201711      TC          11.81       OVue
754         201710      MBN         1.61        OVue
754         201709      WAD         4.72        OVue
754         201709      DJr         23.82       OVue

**147           201803      WBC         13.44       OVue
147         201803      WBS         4.17        OVue
147         201803      WYW         1.80        OVue
147         201802      VD          5.14        OVue
147         201802      LNY         0.05        OVue
147         201801      LEIF        6.94        OVue
147         201801      WBC         25.35       OVue
147         201801      VD          8.53        OVue**
147         201712      MVR         7.17        OSver
147         201711      WDA         9.89        OSver
147         201711      YS          1.65        OSver
147         201711      N1T         3.02        OSver
147         201710      LEIF        1.83        OSver
147         201710      Hi          3.15        OVue
147         201710      TLD         1.43        OVue
147         201710      WBC         21.56       OSver
147         201710      NTT         2.88        OSver
147         201710      WBS         5.64        OSver
147         201710      LNY         2.67        OSver
147         201710      BRV         2.91        OSver
147         201710      N1T         3.78        OSver

以上是我的数据样本,在第1列和第1列中排序。这个数据驻留在postgresql中的一个表中,我根据参数级别构建一个查询。

level_list = ['OSver','Ovue']

for i in range(0,len(level_list)):
    sql_per_hours = str("""select * from table_name where level = %s ;""") \
                        %("'"+''.join(level_list[i]) + "'") 
    df_net = pd.read_sql(sql_per_hours,cnxn)

现在,对于per_id 754,因为他在单一级别下工作,所以应该获取他的所有记录。但对于工作超过1级的人来说。 只应采用最新级别的信息。因此,对于per_id 147,只能选择要提取的前8个记录。最新级别是month_id为max的每个per_id的级别值。因此,从该行开始,直到值中断为止,将采取行。

如何构造此sql查询并从python中调用它?这也有助于我减少我在level_list中为每个级别提取的数据。

预期输出为df_net:

per_id      month_id    area_grp    area_hrs    level
754         201803      WNY         19.93       OVue
754         201802      MT          0.52        OVue
754         201802      WBS         0.34        OVue
754         201801      WYW         33.04       OVue
754         201801      VC          16.62       OVue
754         201801      PX          3.05        OVue
754         201712      RZN         5.05        OVue
754         201711      WYW         17.85       OVue
754         201711      NLN         0.8         OVue
754         201711      DJr         45.67       OVue
754         201711      TC          11.81       OVue
754         201710      MBN         1.61        OVue
754         201709      WAD         4.72        OVue
754         201709      DJr         23.82       OVue
147         201803      WBC         13.44       OVue
147         201803      WBS         4.17        OVue
147         201803      WYW         1.80        OVue
147         201802      VD          5.14        OVue
147         201802      LNY         0.05        OVue
147         201801      LEIF        6.94        OVue
147         201801      WBC         25.35       OVue
147         201801      VD          8.53        OVue

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

这与SQL有关,而不是python。首先获取与每个per_id对应的最后一个级别,然后使用该信息获取所需的行。

select table_name.* -- get all records for last level
from 
    table_name
left join
    (select distinct per_id, level from -- get latest level
        table_name tn
    left join
        (select
            per_id,
            max(month_id) as latest_month -- get latest month
        from
            table_name 
        group by per_id) sub1
    on 
        tn.per_id = sub1.per_id 
        and tn.month_id = sub1.latest_month)sub2
on 
    table_name.per_id = sub2.per_id
    and table_name.level = sub2.level

您还可以使用窗口函数进行探索。