可以通过以下方式创建数据:
create table rows (
client_tracking_id text,
external_id text,
transaction_date timestamp,
transaction_type text,
transaction_amount numeric(18,2)
);
INSERT INTO rows
VALUES ('281101002240', '281101002240','2019-08-14 18:03:04.000000'::timestamp,'Purchase',2000),
('RC000610808201930230', '281101002240','2019-08-19 07:56:45.000000'::timestamp,'AuthorizationRequest',0),
('RC000610808201930230', '281101002240','2019-08-19 07:56:46.000000'::timestamp,'AuthorizationRequest',-2000),
('RC000610808201930230', '281101002240','2019-08-19 07:56:46.000000'::timestamp,'Purchase',-2000);
此查询的目标是检索第一次购买的(正)日期时间,但返回client_tracking_id列的RC值。
尝试1:
SELECT max(client_tracking_id) AS client_tracking_id,
min(transaction_date) AS min
from rows
GROUP BY external_id;
尝试2:
SELECT max(client_tracking_id) AS client_tracking_id,
min(transaction_date) AS min
from rows
where (transaction_type = 'Purchase'::text) AND (transaction_amount > (0)::numeric)
GROUP BY external_id;
在这种情况下尝试尝试正确返回[RC00061080820193023,'2019-08-14 18:03:04.000000']
集的情况,但是在其他情况下并非总是如此。因此,额外的要求是将日期时间仅作为尝试两次满足where条件的交易返回。
在SQL Fiddle上可用:http://www.sqlfiddle.com/#!17/642fd/6
答案 0 :(得分:0)
首先过滤,您只需要“购买”>0。然后按日期排序并采用最早的日期。
fileInfo = fileInfo.Name.Replace("__", "_")
Dim a() As String = fileInfo.Name.Split(New Char() {"_", "-"})
输出
select *
from rows
WHERE transaction_amount > 0
AND transaction_type = 'Purchase'
ORDER BY transaction_date
LIMIT 1
;
答案 1 :(得分:0)
我已经开发出满足我所有要求的以下内容:
select distinct
(
select max(client_tracking_id) as id
from rows r1
where r1.external_id = r.external_id
) as client_tracking_id,
(
select min(transaction_date) as date
from rows r2
WHERE transaction_type = 'Purchase' AND transaction_amount > 0
and r2.external_id = r.external_id
) as date
from rows r