以下是我的查询。我正在按月份和机构提取总数据。
select to_char(BILL_TRANSACTION_DATE, 'YYYY-MM') as Month,
institution_id as institutions,
Count(*) as total
from bill_tx tx
group by to_char(BILL_TRANSACTION_DATE, 'YYYY-MM'), institution
order by to_char(BILL_TRANSACTION_DATE, 'YYYY-MM') desc
在bill_tx表中,我有一列称为版本。我想更改以上查询以获取具有最大版本值的行。
实际上,我想要按Bill_transaction月份和机构划分的记录总数。
条件是根据BILL_REF_NBR的不同而定,如果存在多个版本,则仅计算一条记录。
我尝试在查询中添加以下内容
where tx.version = (SELECT MAX(version) FROM bill_tx b WHERE b.institution_id = tx.institution_id and rownum = 1 group by b.BILL_REF_NBR )
但没有获得正确的数据
下面的示例数据
+----+------------+-------------+--------------+---------+ | id | Month | institution | bill_ref_nbr | version | +----+------------+-------------+--------------+---------+ | 1 | 2020-08-01 | child care | A001 | 1 | | 2 | 2020-08-03 | child care | A001 | 2 | | 3 | 2020-08-04 | child care | M001 | 1 | | 4 | 2020-08-09 | child care | K001 | 1 | | 5 | 2020-08-09 | child care | K001 | 2 | | 6 | 2020-08-04 | infant care | AC01 | 1 | | 7 | 2020-08-05 | infant care | AC01 | 2 | | 8 | 2020-08-07 | Playgroup | PL01 | 1 | | 9 | 2020-07-07 | infant care | XX01 | 1 | | 10 | 2020-07-07 | infant care | XX01 | 2 | +----+------------+-------------+--------------+---------+
输出应为
+---------+-------------+-------+ | Month | institution | total | +---------+-------------+-------+ | 2020-08 | child care | 3 | | 2020-08 | infant care | 1 | | 2020-08 | Playgroup | 1 | | 2020-07 | infant care | 1 | +---------+-------------+-------+
如果查询错误,请更正我的查询。
答案 0 :(得分:1)
在您的上一个请求注释中,您似乎想要完全不同的东西。这就是您在请求中所说的:
我想更改以上查询以获取具有最大版本值的行。
我认为:不,您想忽略版本。因为在评论中您说:
对于每个bill_ref_nbr,我都有多个具有不同版本的记录。如果存在多个版本,我应该算作一个。基本不同的bill_ref_nbr
您要COUNT(DISTINCT bill_ref_nbr)
:
select
to_char(bill_transaction_date, 'yyyy-mm') as month,
institution_id as institutions,
count(distinct bill_ref_nbr) as total
from bill_tx tx
group by to_char(bill_transaction_date, 'yyyy-mm'), institution
order by to_char(bill_transaction_date, 'yyyy-mm') desc, , institution;
答案 1 :(得分:0)
根据您的评论进行了更新。
select to_char(BILL_TRANSACTION_DATE, 'YYYY-MM') as Month,
institution_id as institutions,
Count(distinct bill_ref_nbr) as total
from table1
group by to_char(BILL_TRANSACTION_DATE, 'YYYY-MM'), institution_id
order by to_char(BILL_TRANSACTION_DATE, 'YYYY-MM') desc, total desc;
答案 2 :(得分:0)
您想要每月和研究所的条目数,但是您只想对最大版本数进行计数。
您可以使用MAX OVER
获得最高版本。取决于您是在谈论总体最高版本还是每月每个研究所的最高版本,或者您想要的每个研究所的最高版本
max(version) over ()
或
max(version) over (partition by to_char(bill_transaction_date, 'yyyy-mm'), institution_id)
或
max(version) over (partition by institution_id)
完整查询:
select month, institution, total
from
(
select
to_char(bill_transaction_date, 'yyyy-mm') as month,
institution_id,
version,
max(version) over () as max_version,
count(*) as total
from bill_tx
group by to_char(bill_transaction_date, 'yyyy-mm'), institution, version
)
where version = max_version
order by month desc, institution;
您可以通过添加任一原始查询来实现相同的目的
where version = (select max(version) from bill_tx b)
或
where (institution_id, version) in
(
select institution_id, max(version)
from bill_tx b
group by institution_id
)
或
where (to_char(bill_transaction_date, 'yyyy-mm'), institution_id, version) in
(
select to_char(bill_transaction_date, 'yyyy-mm'), institution_id, max(version)
from bill_tx b
group by to_char(bill_transaction_date, 'yyyy-mm'), institution_id
)
再次取决于您想要的最高版本。