获取具有列

时间:2018-04-04 08:48:09

标签: sql postgresql greatest-n-per-group

我有以下SQL查询:

 SELECT DISTINCT a.project_id, a.acc_name, a.project_name, a.iot,a.ilc_code, a.active, a.license_no, c.line_id, c.chargable_fte, c.cost_call_date from Account a INNER JOIN account_version c USING (acc_id) where a.acc_name='APMM' AND EXTRACT (MONTH FROM c.cost_call_date)>=1 AND EXTRACT (MONTH FROM c.cost_call_date)<=4 AND EXTRACT (YEAR FROM c.cost_call_date)>=2018 AND EXTRACT (YEAR FROM c.cost_call_date)<=2018 order by c.line_id desc;

它给出了以下结果:

 project_id   acc_name  project_name  iot  ilc_code  active  license_no  line_id  chargable_fte  cost_call_date
 IN-16-10171   APMM      DAMCO        LA    AGP7GL    false               24        70             2018-04-03
 IN-16-10171   APMM      DAMCO        LA    AGP7GL    false               23        70             2018-04-03
 IN-16-10171   APMM      DAMCO        LA    AGP7GL    false               13        68             2018-03-20
 IN-16-10171   APMM      DAMCO        LA    AGP7GL    false               11        65             2018-03-20

此处,列cost_call_date的日期格式为yyyy-mm-dd。我想获取特定月份具有最大line_id的数据。我想每个月只有一行。另外,我不想在结果中使用line_id列。也就是说,所需的结果如下:

 project_id   acc_name  project_name  iot  ilc_code  active  license_no  chargable_fte  cost_call_date
 IN-16-10171  APMM       DAMCO        LA    AGP7GL    false                  70            2018-04-03
 IN-16-10171  APMM       DAMCO        LA    AGP7GL    false                  68            2018-03-20

请帮我实现这个结果。

0 个答案:

没有答案