我有一个包含这些文件的表:
**Date Name Table Direction Code**
13/01/1978 Jacks xxxx stret ... 1
13/01/1978 John xxxx xxxxx 0
...........
12/01/1978 Dave xxxx xxxxxx 0
12/01/1978 Suse xxxxx xxxxxx 0
...........
11/01/1978 Mickey xxxx xxxxx 1
11/01/1978 Suse xxxxxx xxxxxxx 2
11/01/1978 Nune xxxxxx xxxxxxx 2
.....
09/01/1978 ..... ...... ....... 1
08/01/1978 ..... ...... ....... 0
21/01/1978 ..... ...... ....... 1
.....
我想提取按日期后代分组的三个第一个事件,并检测字段代码在具有相同日期的任何行中是否具有值“1”。我想要的结果是:
The first date is 21/01/1978, and have code 1
The second date is 13/01/1978 and have code 1
The third date is 12/01/1978 and don't have code 1
我正在尝试rownum但不行。请问你能帮帮我吗?谢谢,抱歉我的英文!
答案 0 :(得分:0)
我不知道为什么rownum不能为你工作,也许因为你试图在订单之前插入内部查询(你首先要订购,然后才使用rownum因为每一行会得到另一个rownum)
SELECT Date,code from (
(SELECT Date,max(Code) as code from YourTable
GROUP BY Date
ORDER BY Date DESC
)
WHERE rownum <= 3
答案 1 :(得分:0)
试试这希望它有帮助
SELECT DISTINCT B.DT,
DECODE(B.VL,1,' has code 1',999,' dont have code 1') inf
FROM
(SELECT A.DT,
CASE
WHEN A.CODE = 1
THEN 1
ELSE 999
END VL,
ROW_NUMBER() OVER(PARTITION BY A.DT ORDER BY A.CODE DESC) RN
FROM
(SELECT '01/03/2016' AS DT,1 AS CODE FROM DUAL
UNION ALL
SELECT '01/03/2016' AS DT,0 AS CODE FROM DUAL
UNION ALL
SELECT '05/03/2016' AS DT,0 AS CODE FROM DUAL
UNION ALL
SELECT '05/03/2016' AS DT,0 AS CODE FROM DUAL
UNION ALL
SELECT '06/03/2016' AS DT,1 AS CODE FROM DUAL
)A
) B
WHERE B.RN = 1;
答案 2 :(得分:-2)
select date,
decode(date_cnt, 0, 'dont have code = 1', 'have code = 1)
from (
select t.date,
max(t.code) as date_cnt
from table_name t
group by date
order by date desc)
where rownum <=3
这样你首先在内部查询中按日期对它们进行排序,以及准备有关代码= 1的信息。外部查询将获取前3行并将代码更改为所需信息(如果0 =&gt;没有代码,否则就有代码)