Sql 查询多个group by列,获取最大值为一列的行
下面是我的问题。我正在按月份和机构提取总数据Sql 查询多个group by列,获取最大值为一列的行,sql,oracle,Sql,Oracle,下面是我的问题。我正在按月份和机构提取总数据 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
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表中,我有一列名为version。我想将上述查询更改为获取具有最大版本值的行
实际上,我需要按bill_交易月和机构列出的记录总数
条件由disctint BILL\u REF\u 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 |
+----+------------+-------------+--------------+---------+
+----+------------+-------------+--------------+---------+
|id |月|机构|法案|参考编号|版本|
+----+------------+-------------+--------------+---------+
|1 | 2020-08-01 |儿童保育| A001 | 1|
|2 | 2020-08-03 |儿童保育| A001 | 2|
|3 | 2020-08-04 |儿童保育| M001 | 1|
|4 | 2020-08-09 |儿童保育| K001 | 1|
|5 | 2020-08-09 |儿童保育| K001 | 2|
|6 | 2020-08-04 |婴儿护理| AC01 | 1|
|7 | 2020-08-05 |婴儿护理| AC01 | 2|
|8 | 2020-08-07 |游戏组| PL01 | 1|
|9 | 2020-07-07 |婴儿护理| XX01 | 1|
|10 | 2020-07-07 |婴儿护理| XX01 | 2|
+----+------------+-------------+--------------+---------+
输出应该是
+---------+-------------+-------+
| Month | institution | total |
+---------+-------------+-------+
| 2020-08 | child care | 3 |
| 2020-08 | infant care | 1 |
| 2020-08 | Playgroup | 1 |
| 2020-07 | infant care | 1 |
+---------+-------------+-------+
+---------+-------------+-------+
|月|机构|总数|
+---------+-------------+-------+
|2020-08 |儿童保育| 3|
|2020-08 |婴儿护理| 1|
|2020-08 |游戏组| 1|
|2020-07 |婴儿护理| 1|
+---------+-------------+-------+
如果我的查询错误,请更正。根据您的评论进行更新
您需要每月和协会的条目数,但只需要计算最大版本的行数 您可以使用
MAX OVER
获得最大版本。这取决于您所谈论的是总体最高版本还是每月最高版本,以及您想要的研究所或每个研究所的最高版本
max(version) over ()
或
或
完整查询:
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)
或
或
再次取决于您希望最高版本与之相关的内容。对于您上次的请求注释,您似乎想要完全不同的内容。这是您在请求中所说的: 我想将上述查询更改为获取具有最大版本值的行 我想:不,你想忽略这个版本。因为你在评论中说: 对于每个账单参考号,我有多个不同版本的记录。如果存在多个版本,我应将计数作为一。基本不同的账单参考编号 您想
计数(不同票据参考号)
:
请标记适当的数据库名称。对于
Orange
,预期输出不应为5 Orange 2
,而应为4 Orange 2
?此处仅取版本的最高值,即4 Orange或5 Orange。查询与示例数据非常不同。这两个人似乎没有任何关系。那么,日期在查询中不再重要了?计数也不是吗?现在我添加了真实的样本数据。很抱歉,我不知道如何以表格格式设置我的查询目的是按机构和月份获取记录组的总数。其中一个条件是选择版本号最高的记录。如果您已获得此输出,请解释逻辑。2020-08儿童保育3关于2020-08月份儿童保育的示例三个法案参考编号:A001、M001、K001。但是A001有两个版本,所以我应该计算为1(A001)+1(M001)+1(K001),所以第8个月的儿童保育总数是3。就像第8个月的婴儿护理有相同的账单参考号和两个版本,所以计数应该是1。版本基于一个账单参考号。我不知道如何通过查询在broup中添加不同的bill\u ref\u nbr。这就是为什么我尝试使用max版本/感谢您花费时间并给出答复。我尝试了上面的查询,但没有得到预期的结果。因为您说过要计算具有最大版本的行,但您没有。我会删除这个答案。我已经发布了新请求的新答案。
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
)
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;