简体   繁体   English

Oracle - Select 行 Group By 基于多列

[英]Oracle - Select rows Group By based on multiple columns

I want to fetch duplicate rows for a particular date & time, group by both bname and txn_date AND txn_date up to Hours and Minutes (Seconds do not need to match)我想获取特定日期和时间的重复行,按bnametxn_date AND txn_date ,直到小时和分钟(秒不需要匹配)

For Example, I fetch the rows below for txn_date = 2020/07/28 , txn_type = OPD , payment_mode = CREDIT and inv_num begins with AGL例如,我获取txn_date = 2020/07/28txn_type = OPDpayment_mode = CREDITinv_numAGL开头的行

Out of these, I want only rows with IDs from 1 - 4其中,我只想要 ID 为 1 - 4 的行

ID |         txn_date       |   bname        
-----------------------------------------------
 1 | 2020/07/28 10:21:58 PM |  MRS R A W B VERNON
 2 | 2020/07/28 10:21:56 PM |  MRS R A W B VERNON
 3 | 2020/07/28 09:51:58 AM |  MRS TRIDENTZ
 4 | 2020/07/28 09:51:58 AM |  MRS TRIDENTZ
 5 | 2020/07/28 09:49:51 AM |  MRS TRIDENTZ
 6 | 2020/07/28 08:33:14 AM |  MRS TRIDENTZ
 7 | 2020/07/28 08:25:06 AM |  MRS S F D SHERILA
 8 | 2020/07/28 08:11:35 AM |  MRS S F D SHERILA

I use this query below.我在下面使用这个查询。

select to_char(l.txn_date,'YYYY/MM/DD HH12:MI:SS AM') "DATE",l.bname
from linv l where (bname) IN (select bname from linv
where txn_date like '28-JUL-20%'
group by bname, txn_date
having count(*) > 1) and l.txn_type = 'OPD' and payment_mode = 'CREDIT' 
and inv_num like 'AGL%' 
and txn_date like '28-JUL-20%'
order by l.txn_date desc, l.bname

But this does not give me the desired output as some of the required rows are not fetching.但这并没有给我所需的 output,因为某些所需的行未获取。

I tried to change group by to to_char(txn_date, 'HH24') but that did not work too.我试图将 group by 更改为to_char(txn_date, 'HH24')但这也不起作用。

I think the group_by txn_date has the issue but couldn't get it fixed.我认为group_by txn_date有问题但无法修复。 Can some one please help?有人可以帮忙吗?

Thanks!谢谢!

If you're trying to group based on datetime up-to the minute, you can use TRUNC function to get rid of seconds & milliseconds.如果您尝试根据最新的日期时间进行分组,您可以使用 TRUNC function 来摆脱秒和毫秒。

https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions209.htm#SQLRF06151 https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions209.htm#SQLRF06151

Use this in your group by instead在你的小组中使用这个

group by bname, to_char(TRUNC(txn_date, 'MI'), 'DD-MON-YYYY HH24:MI:SS')

Does exists do what you want?是否exists做你想要的?

select l.*
from linv l
where exists (select 1
              from linv l2
              where trunc(l2.txn_date) = trunc(l.txn_date) and
                    l2.bname = l.bname
             );

You can add additional filters as well.您也可以添加其他过滤器。 Your query suggests:您的查询建议:

with l as (
      select l.*
      from linv l
      where l.txn_type = 'OPD' and
            l.payment_mode = 'CREDIT' and
            l.inv_num like 'AGL%' 
            l.txn_date >= date '2020-07-28' and
            l.txn_date < date '2020-07-29' 
           )
select l.*
from l l
where exists (select 1
              from l l2
              where trunc(l2.txn_date) = trunc(l.txn_date) and
                    l2.bname = l.bname
             );

Or using window functions:或者使用 window 函数:

with l as (
      select l.*
      from linv l
      where l.txn_type = 'OPD' and
            l.payment_mode = 'CREDIT' and
            l.inv_num like 'AGL%' 
            l.txn_date >= date '2020-07-28' and
            l.txn_date < date '2020-07-29' 
     )
select l.*
from (select l.*, count(*) over (partition by trunc(txn_date), bname) as cnt
      from l
     )l
where cnt >= 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM