PSQL:选择子查询和分组依据

时间:2016-03-18 18:02:05

标签: sql postgresql group-by distinct

我有以下表结构:

dev=> \d membership_subscription
                                        Table "public.membership_subscription"
       Column       |           Type           |                              Modifiers                               
--------------------+--------------------------+----------------------------------------------------------------------
 id                 | integer                  | not null default nextval('membership_subscription_id_seq'::regclass)
 membership_id      | bigint                   | not null
 name               | character varying        | not null
 description        | character varying        | 
 price_gross        | integer                  | not null
 price_net          | integer                  | not null
 tax                | double precision         | not null
 priority           | integer                  | not null default 0
 currency_code      | character varying        | not null
 calculated_gross   | integer                  | not null
 calculated_net     | integer                  | not null
 state              | character varying        | 
 reference_date     | timestamp with time zone | not null
 created_at         | timestamp with time zone | not null
 created_by         | json                     | not null
 updated_at         | timestamp with time zone | not null
 updated_by         | json                     | not null
 payment_method_id  | bigint                   | 
 code               | character varying        | 
 bookings_per_month | integer                  | not null default 0
 bookings_per_venue | integer                  | not null default 0
Indexes:
    "membership_subscription_pkey" PRIMARY KEY, btree (id)
    "membership_id_reference_date_index" UNIQUE, btree (membership_id, reference_date)
Foreign-key constraints:
    "membership_subscription_membership_id_fkey" FOREIGN KEY (membership_id) REFERENCES membership(id) ON UPDATE CASCADE
    "membership_subscription_payment_method_id_fkey" FOREIGN KEY (payment_method_id) REFERENCES payment_saved_method(id) ON UPDATE CASCADE ON DELETE SET NULL

我想:

  • SELECT membership _id&名称
  • GROUP BY membership_id
  • 获取最大reference_date
  • 不在(' new',' discurded')

注意: 每个会员都有12个订阅,所以我想选择那些只处于状态(' new','丢弃')然后是它们的最大reference_date

我的尝试:

dev=> SELECT membership_id, name FROM membership_subscription WHERE state NOT IN ('new', 'discurded') GROUP BY membership_id, name, reference_date ORDER BY reference_date DESC;

但是没有回复对我的预期

2 个答案:

答案 0 :(得分:0)

WITH membership_subscription(membership_id,name,state,reference_date) AS ( VALUES
  (1,'name-1','new','2016-03-18'::DATE),
  (1,'name-2','discurded','2016-03-17'::DATE),
  (1,'name-3','state 1','2016-03-16'::DATE), -- record #1 to select 
  (1,'name-4','state 2','2016-03-15'::DATE), 
  (2,'name-5','new','2015-03-15'::DATE),
  (2,'name-6','discurded','2015-03-16'::DATE),
  (2,'name-7','state 3','2015-03-17'::DATE),
  (2,'name-8','state 4','2015-03-18'::DATE) -- record #2 to select
)
SELECT
  DISTINCT ON (membership_id)
  membership_id,
  name,
  max(reference_date)
FROM membership_subscription
WHERE state NOT IN ('new','discurded')
GROUP BY membership_id,name;

答案 1 :(得分:0)

您的错误是将reference_date包含在分组中,将其删除,您的查询应该有效。要在每个组中获取最新的reference_date,您应该使用最大聚合函数

代码(我没有测试它):

SELECT membership_id, name, max(reference_date) FROM membership_subscription WHERE state NOT IN ('new', 'discurded') GROUP BY membership_id, name ORDER BY 3 DESC;