[英]Aggregate sql query not working if select statement contains columns of type DATE
I want to select a single record with the latest value on the version column.我想在版本列中选择具有最新值的单个记录。 However If I include the
retrn_period
column which is of type date my query returns multiple records.但是,如果我包含类型为 date 的
retrn_period
列,我的查询将返回多条记录。
SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin,
branch_code, tax_type, retrn_period, Max(version)
FROM rfp_returns_ref
WHERE tin = '000079108'
AND ftype_code = '1702EX'
AND Upper(status) = Upper('POSTED')
group by rdo_code, batch_no,reference_no, dln,
retrn_seq_num, ftype_code, tin, branch_code, tax_type, retrn_period;
If I include the retrn_period column ... my query returns multiple records.
如果我包含 retrn_period 列……我的查询将返回多条记录。
By including the retrn_period
column you are changing the non-aggregated projection of the query, so now you get the maximum version for each distinct date in the result set.通过包含
retrn_period
列,您正在更改查询的非聚合投影,因此现在您可以获得结果集中每个不同日期的最大版本。
You want to show the date of the latest version?你想显示最新版本的日期吗? Assuming your
retrn_period
increases alongside the version
this would work:假设您的
retrn_period
与version
一起增加,这将起作用:
SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin,
branch_code, tax_type
, max(retrn_period) as retrn_period
, max(version) as version
FROM rfp_returns_ref
WHERE tin = '000079108'
AND ftype_code = '1702EX'
AND Upper(status) = Upper('POSTED')
group by rdo_code, batch_no,reference_no, dln,
retrn_seq_num, ftype_code, tin, branch_code, tax_type;
A more general solution which would work for something which can't be aggregate, say name
, would be to use a subquery with an analytic function...一个更通用的解决方案适用于不能聚合的东西,比如
name
,是使用带有分析函数的子查询......
SELECT sq.rdo_code, sq.batch_no, sq.reference_no, sq.dln, sq.retrn_seq_num, sq.ftype_code, sq.tin,
sq.branch_code, sq.tax_type
, sq.retrn_period
, sq.name
, sq.version
from (
SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin,
branch_code, tax_type
, retrn_period
, version
, name
, rank() over (partition by rdo_code, batch_no,reference_no, dln,
retrn_seq_num, ftype_code, tin, branch_code, tax_type
order by version desc ) as rn
FROM rfp_returns_ref
WHERE tin = '000079108'
AND ftype_code = '1702EX'
AND Upper(status) = Upper('POSTED')
) sq
where sq.rn = 1 ;
If you're using Oracle 12c you can use the fetch limiting syntax, which is a lot simpler:如果您使用的是 Oracle 12c,则可以使用 fetch 限制语法,这要简单得多:
SELECT rdo_code, batch_no, reference_no, dln, retrn_seq_num, ftype_code, tin,
branch_code, tax_type
, retrn_period
, version
FROM rfp_returns_ref
WHERE tin = '000079108'
AND ftype_code = '1702EX'
AND order by version desc
fetch first 1 row only;
If you want to select a sinle record - with the highest version number - then I don't think you need to group any thing at all.如果你想选择一个单一的记录 - 具有最高版本号 - 那么我认为你根本不需要对任何东西进行分组。
Ordering the rows by the version number and grabbing the top 1 should do it:按版本号对行进行排序并获取前 1 行应该这样做:
SELECT
rdo_code,
batch_no,
reference_no,
dln,
retrn_seq_num,
ftype_code,
tin,
branch_code,
tax_type
retrn_period,
version
FROM rfp_returns_ref
WHERE
tin = '000079108'
AND ftype_code = '1702EX'
AND UPPER(status) = UPPER('POSTED')
ORDER BY version DESC
FETCH FIRST 1 ROWS ONLY;
This query assumes that you are running Oracle 12c.此查询假定您正在运行 Oracle 12c。
You can also use rownum=1
SELECT
rdo_code,
batch_no,
reference_no,
dln,
retrn_seq_num,
ftype_code,
tin,
branch_code,
tax_type
retrn_period,
version
FROM rfp_returns_ref
WHERE
tin = '000079108'
AND ftype_code = '1702EX'
AND UPPER(status) = UPPER('POSTED')
AND rownum=1
ORDER BY version DESC;
---------------------------------------------------------------------------------------
or subquery like
SELECT
rdo_code,
batch_no,
reference_no,
dln,
retrn_seq_num,
ftype_code,
tin,
branch_code,
tax_type
retrn_period,
version
FROM rfp_returns_ref a
WHERE
tin = '000079108'
AND ftype_code = '1702EX'
AND UPPER(status) = UPPER('POSTED')
AND a.version = (SELECT Max(b.version)
FROM rfp_returns_ref b
WHERE b.tin = a.tin
AND b.ftype_code = a.ftype_code
AND b.UPPER(status) = UPPER(a.status));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.