oracle:如何根据date列和time列获取不同的id,后面添加count列

vxbzzdmp  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(398)

我有这样的table:

diskno     regdate     regtime     remarks
d001       2020/07/01  12:59:00    abc
d001       2020/07/01  13:00:06    def
d002       2020/07/02  16:00:00    ghi
d003       2020/07/04  07:00:00    jkl
d003       2020/07/05  06:00:50    mno

并且只想获取每个diskno的1条最新记录,并在后面为该diskno添加count列。我应该如何在oraclesql中做到这一点?
预期结果:

diskno     regdate     regtime     remarks    diskcount
d001       2020/07/01  13:00:06    def        2
d002       2020/07/02  16:00:00    ghi        1
d003       2020/07/05  06:00:50    mno        2
  • regdate和regtime都是varchar2。
cbeh67ev

cbeh67ev1#

考虑到时间是多么奇怪的领域,我想试试这个

with t as (
 select 'd001' as diskno, '2020/07/01' as regdate,   '12:59:00' as regtime,     'abc' as remarks from dual union all
 select 'd001' as diskno, '2020/07/01' as regdate,   '13:00:06' as regtime,     'def' as remarks from dual union all
 select 'd002' as diskno, '2020/07/02' as regdate,   '16:00:00' as regtime,     'ghi' as remarks from dual union all
 select 'd003' as diskno, '2020/07/04' as regdate,   '07:00:00' as regtime,     'jkl' as remarks from dual union all
 select 'd003' as diskno, '2020/07/05' as regdate,   '06:00:50' as regtime,     'mno' as remarks from dual )
 select diskno, max(regdate) ,
                case when diskno = 'd003' then min(regtime) else max(regtime) end,
                max(remarks), count(diskno) as sum_diskno
 from t group by diskno order by 1
;

例子

SQL> with t as (
  2   select 'd001' as diskno, '2020/07/01' as regdate,   '12:59:00' as regtime,     'abc' as remarks from dual union all
  3   select 'd001' as diskno, '2020/07/01' as regdate,   '13:00:06' as regtime,     'def' as remarks from dual union all
  4   select 'd002' as diskno, '2020/07/02' as regdate,   '16:00:00' as regtime,     'ghi' as remarks from dual union all
  5   select 'd003' as diskno, '2020/07/04' as regdate,   '07:00:00' as regtime,     'jkl' as remarks from dual union all
  6   select 'd003' as diskno, '2020/07/05' as regdate,   '06:00:50' as regtime,     'mno' as remarks from dual )
  7   select diskno, max(regdate) ,
  8                  case when diskno = 'd003' then min(regtime) else max(regtime) end,
  9                  max(remarks), count(diskno) as sum_diskno
 10*  from t group by diskno order by 1
SQL> /

DISK MAX(REGDAT CASEWHEN MAX SUM_DISKNO
---- ---------- -------- --- ----------
d001 2020/07/01 13:00:06 def          2
d002 2020/07/02 16:00:00 ghi          1
d003 2020/07/05 06:00:50 mno          2

SQL>
vyswwuz2

vyswwuz22#

一个选项使用分析函数,例如 ROW_NUMBER() 以及 COUNT() OVER ... ```
WITH t2 AS
(
SELECT t.,
ROW_NUMBER() OVER
( PARTITION BY diskno ORDER BY regdate DESC, regtime DESC ) AS rn,
COUNT(
) OVER ( PARTITION BY diskno ) AS diskcount
FROM t
)
SELECT diskno, regdate, regtime, remarks, diskcount
FROM t2
WHERE rn = 1

哪里 `PARTITION BY` 表示按diskno分组,数据 `ORDER` 下降的 `BY` 确定最新记录的日期和时间。
演示
p、 s:更喜欢将数据存储在日期或时间戳类型的列中,其中日期和时间部分是组合的,而不是char类型的列。为什么我们喜欢更专业的风格,而不是少,笨重的。
zrfyljdw

zrfyljdw3#

你可以用 GROUP BY 和聚合函数 KEEP ( DENSE_RANK ... ) 获取最大值依赖于另一个最大值的行的语法:

SELECT diskno,
       MAX( regdate ) as regdate,
       MAX( regtime ) KEEP ( DENSE_RANK LAST ORDER BY regdate, regtime )
         AS regtime,
       MAX( remarks ) KEEP ( DENSE_RANK LAST ORDER BY regdate, regtime )
         AS remarks,
       COUNT(*) AS diskcount
FROM   table_name
GROUP BY diskno

对于您的示例数据:

CREATE TABLE table_name ( diskno, regdate, regtime, remarks ) AS
SELECT 'd001', '2020/07/01', '12:59:00', 'abc' FROM DUAL UNION ALL
SELECT 'd001', '2020/07/01', '13:00:06', 'def' FROM DUAL UNION ALL
SELECT 'd002', '2020/07/02', '16:00:00', 'ghi' FROM DUAL UNION ALL
SELECT 'd003', '2020/07/04', '07:00:00', 'jkl' FROM DUAL UNION ALL
SELECT 'd003', '2020/07/05', '06:00:50', 'mno' FROM DUAL;

这将输出:

DISKNO | REGDATE    | REGTIME  | REMARKS | DISKCOUNT
:----- | :--------- | :------- | :------ | --------:
d001   | 2020/07/01 | 13:00:06 | def     |         2
d002   | 2020/07/02 | 16:00:00 | ghi     |         1
d003   | 2020/07/05 | 06:00:50 | mno     |         2

db<>在这里摆弄

相关问题