我有下表名为TransportBooking的表,其中包含以下列中的数据:
PrintBatchNo Count Created AdditionalInfo C_Country_ID
1 10 08-nov-17 13:48:00 test1 100
1 10 08-nov-17 13:48:00 test2 100
2 25 08-nov-17 19:02:42 test3 101
2 25 08-nov-17 19:02:42 test4 101
2 25 08-nov-17 19:02:42 test5 102
我的sql是:
SELECT q1.printbatchno,
q1.count,
Min (q2.created),
q2.ainfo,
q2.countryid
FROM (SELECT Count(tb.printbatchno) AS Count,
tb.printbatchno
FROM transportbooking tb
WHERE tb.printbatchno IS NOT NULL
AND tb.printbatchno > 0
GROUP BY tb.printbatchno)q1
INNER JOIN (SELECT tb.printbatchno,
To_char(tb.created, 'dd-mon-yy hh24:mi:ss') AS Created,
tb.additional_info AS ainfo,
tb.c_country_id AS countryid
FROM transportbooking tb
WHERE tb.printbatchno IS NOT NULL
AND tb.printbatchno > 0)q2
ON q2.printbatchno = q1.printbatchno
GROUP BY q1.printbatchno,
q1.count,
q2.created,
q2.countryid,
q2.ainfo
ORDER BY q2.created;
上面的sql给出了以下结果:
PrintBatchNo Count MIN(q2.Created) Ainfo CountryID
1 10 08-nov-17 13:48:00 test1 100
1 10 08-nov-17 13:48:00 test2 100
2 25 08-nov-17 19:02:42 test3 101
2 25 08-nov-17 19:02:42 test4 101
2 25 08-nov-17 19:02:42 test5 102
我希望它看起来像这样:
PrintBatchNo Count Created AdditionalInfo C_Country_ID
1 10 08-nov-17 13:48:00 test1 100
2 25 08-nov-17 19:02:42 test3 101
所以,我希望sql每个printbatchno只显示1行,并且应该显示该printbatchno的最小创建日期行。此外,它应显示AdditionalInfo和C_Country_ID列。
有人可以告诉我,我出了什么事吗?
由于
答案 0 :(得分:0)
在标准SQL中,您将使用窗口函数:
select tb.*
from (select tb.*,
row_number() over (partition by PrintBatchNo order by Created) as seqnum
from transportbooking tb
) tb
where seqnum = 1;