如何为多组 id 获取符合某些条件的行
How can I fetch rows upto some condition for multiple group of ids
如何为多个组获取符合某些条件的行
请参考下面my_table当前申请的订单
(1) fk_user_id 升序 (2) created_date 降序
我正在使用 postgresql 和 spring 启动 jpa、jpql。
1) 请找到查询以创建 table 并插入数据,如下所示
创建和插入语句:
CREATE TABLE public.my_table
(
id bigint,
condition boolean,
fk_user_id bigint,
created_date date
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.my_table
OWNER TO postgres;
INSERT INTO public.my_table(
id, condition, fk_user_id, created_date)
VALUES
(137, FALSE, 23, '2019-08-28'),
(107, FALSE, 23, '2019-05-13'),
(83, TRUE, 23, '2019-04-28'),
(78, FALSE, 23, '2019-04-07'),
(67, TRUE, 23, '2019-03-18'),
(32, FALSE, 23, '2019-01-19'),
(181, FALSE, 57, '2019-11-04'),
(158, TRUE, 57, '2019-09-27'),
(146, FALSE, 57, '2019-09-16'),
(125, FALSE, 57, '2019-07-24'),
(378, TRUE, 71, '2020-02-16'),
(228, TRUE, 71, '2019-12-13'),
(179, FALSE, 71, '2019-10-06'),
(130, FALSE, 71, '2019-08-19'),
(114, TRUE, 71, '2019-06-29'),
(593, FALSE, 92, '2020-03-02'),
(320, FALSE, 92, '2020-01-30'),
(187, FALSE, 92, '2019-11-23'),
(180, TRUE, 92, '2019-10-17'),
(124, FALSE, 92, '2019-08-05');
我想获取所有具有 所有最新 FALSE 条件直到找到最后一个 TRUE 的所有行,然后跳过该用户的其他行。
例如,
1) 用户 id = 23 -- id 为 (137, 107) 的前 2 行将在 2019-04-28 获取,它具有 TRUE 条件,因此将跳过其他行
2) 用户 ID = 57 -- 只有 1 行 ID (181)
3) 用户 id = 71 -- 不会获取任何行,因为它具有最新的 TRUE 条件
同样,我的结果行应该如下所示。
我只能通过以下查询找到 1 个用户的行
select * from user_condition where
fk_user_id = 23 and created_date > (select max(created_date) from user_condition where fk_user_id = 23 and condition like 'TRUE' group by fk_user_id);
但是,我想要所有的行 fk_user_id
SELECT t1.*
FROM sourcetable t1
WHERE t1.condition = 'FALSE'
AND NOT EXISTS ( SELECT NULL
FROM sourcetable t2
WHERE t1.fk_user_id = t2.fk_user_id
AND t1.created_date < t2.created_date /* or <= */
AND t2.condition = 'TRUE' )
如何为多个组获取符合某些条件的行
请参考下面my_table当前申请的订单 (1) fk_user_id 升序 (2) created_date 降序
我正在使用 postgresql 和 spring 启动 jpa、jpql。
1) 请找到查询以创建 table 并插入数据,如下所示
创建和插入语句:
CREATE TABLE public.my_table
(
id bigint,
condition boolean,
fk_user_id bigint,
created_date date
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.my_table
OWNER TO postgres;
INSERT INTO public.my_table(
id, condition, fk_user_id, created_date)
VALUES
(137, FALSE, 23, '2019-08-28'),
(107, FALSE, 23, '2019-05-13'),
(83, TRUE, 23, '2019-04-28'),
(78, FALSE, 23, '2019-04-07'),
(67, TRUE, 23, '2019-03-18'),
(32, FALSE, 23, '2019-01-19'),
(181, FALSE, 57, '2019-11-04'),
(158, TRUE, 57, '2019-09-27'),
(146, FALSE, 57, '2019-09-16'),
(125, FALSE, 57, '2019-07-24'),
(378, TRUE, 71, '2020-02-16'),
(228, TRUE, 71, '2019-12-13'),
(179, FALSE, 71, '2019-10-06'),
(130, FALSE, 71, '2019-08-19'),
(114, TRUE, 71, '2019-06-29'),
(593, FALSE, 92, '2020-03-02'),
(320, FALSE, 92, '2020-01-30'),
(187, FALSE, 92, '2019-11-23'),
(180, TRUE, 92, '2019-10-17'),
(124, FALSE, 92, '2019-08-05');
我想获取所有具有 所有最新 FALSE 条件直到找到最后一个 TRUE 的所有行,然后跳过该用户的其他行。
例如,
1) 用户 id = 23 -- id 为 (137, 107) 的前 2 行将在 2019-04-28 获取,它具有 TRUE 条件,因此将跳过其他行
2) 用户 ID = 57 -- 只有 1 行 ID (181)
3) 用户 id = 71 -- 不会获取任何行,因为它具有最新的 TRUE 条件
同样,我的结果行应该如下所示。
我只能通过以下查询找到 1 个用户的行
select * from user_condition where
fk_user_id = 23 and created_date > (select max(created_date) from user_condition where fk_user_id = 23 and condition like 'TRUE' group by fk_user_id);
但是,我想要所有的行 fk_user_id
SELECT t1.*
FROM sourcetable t1
WHERE t1.condition = 'FALSE'
AND NOT EXISTS ( SELECT NULL
FROM sourcetable t2
WHERE t1.fk_user_id = t2.fk_user_id
AND t1.created_date < t2.created_date /* or <= */
AND t2.condition = 'TRUE' )