说我们有下表
Name Date reg_no
aa 12/12/2013 10:13:15 rty003
aa 13/12/2013 11:14:16 jyu887
bb 13/14/2013 09:45:10 rty003
bb 12/12/2013 10:13:27 rty003
我想只显示具有匹配的reg_no和日期值(没有秒数)的名称。 结果应如下所示。
Name1 Name2 Date reg_no
aa bb 12/12/2013 10:13 rty003
我如何在oracle中实现这一目标?
答案 0 :(得分:2)
尝试使用listagg
功能:
SELECT listagg( NAME,',') WITHIN GROUP ( ORDER BY NAME),
to_char(date_time,'mm/dd/yyyy hh24:mi'),
reg_no
FROM t
GROUP BY to_char(date_time,'mm/dd/yyyy hh24:mi'), reg_no
HAVING count(*) > 1;
答案 1 :(得分:0)
可能更好但值得,
WITH TAB(NAME,DDATE, REG_NO) AS (
SELECT 'aa', TO_DATE('12/12/2013 10:13:15', 'MM/DD/YYYY HH24:MI:SS'), 'rty003' FROM DUAL UNION ALL
SELECT 'aa', TO_DATE('03/12/2013 11:14:16', 'MM/DD/YYYY HH24:MI:SS'), 'jyu887' FROM DUAL UNION ALL
SELECT 'bb', TO_DATE('03/14/2013 09:45:10', 'MM/DD/YYYY HH24:MI:SS'), 'rty003' FROM DUAL UNION ALL
SELECT 'bb', TO_DATE('12/12/2013 10:13:27', 'MM/DD/YYYY HH24:MI:SS'), 'rty003' FROM DUAL),
----------------
-- End of Data Preparation
----------------
TAB2 AS
(SELECT NAME,
TRUNC(DDATE, 'MI') DDATE,
REG_NO,
ROW_NUMBER() OVER(PARTITION BY TRUNC(DDATE, 'MI'), REG_NO ORDER BY NAME) RN,
COUNT(1) OVER(PARTITION BY TRUNC(DDATE, 'MI'), REG_NO) CNT
FROM TAB)
SELECT MIN(DECODE(RN, 1, NAME, NULL)) NAME1,
MIN(DECODE(RN, 2, NAME, NULL)) name2,
DDATE,
REG_NO
FROM TAB2
WHERE CNT = 2
group by ddate, reg_no;
输出:
| NAME1 | NAME2 | DDATE | REG_NO |
|-------|-------|---------------------------------|--------|
| aa | bb | December, 12 2013 10:13:00+0000 | rty003 |
您的最终查询将是
WITH TAB2 AS
(SELECT NAME,
TRUNC(DDATE, 'MI') DDATE,
REG_NO,
ROW_NUMBER() OVER(PARTITION BY TRUNC(DDATE, 'MI'), REG_NO ORDER BY NAME) RN,
COUNT(1) OVER(PARTITION BY TRUNC(DDATE, 'MI'), REG_NO) CNT
FROM <your_table_name>)
SELECT MIN(DECODE(RN, 1, NAME, NULL)) NAME1,
MIN(DECODE(RN, 2, NAME, NULL)) name2,
DDATE,
REG_NO
FROM TAB2
WHERE CNT = 2
group by ddate, reg_no;
答案 2 :(得分:0)
SELECT MIN(NAME),MAX(NAME),
TRUNC(DDATE, 'MI'),
REG_NO
FROM tab
GROUP BY
TRUNC(DDATE, 'MI'),
REG_NO
HAVING COUNT(*) >= 2