(1)id name
1 aa
2 bb
1 dd
2 ee
我想要的结果是 id唯一的一条记录,但是我又要把name取出来的
1 dd
2 bb
也可以是1 dd
2 eeselect distinct id,name from table where name!=aaselect id,name from table group by id,name这2种方法搜索不出来,都不是我想要的结果(2)用hql语句的话,好像不认识distinct的,分页出来的总记录是没有distinct的那个值
1 aa
2 bb
1 dd
2 ee
我想要的结果是 id唯一的一条记录,但是我又要把name取出来的
1 dd
2 bb
也可以是1 dd
2 eeselect distinct id,name from table where name!=aaselect id,name from table group by id,name这2种方法搜索不出来,都不是我想要的结果(2)用hql语句的话,好像不认识distinct的,分页出来的总记录是没有distinct的那个值
解决方案 »
- oracle10 装好几遍也装不上
- 在数据库中如何实现? 希望能给出代码
- 建一视图,其中有一字段是varchar2型,要求该字段反映数据变动的最后时间
- oracle 存储过程
- ORA-00942: 表或视图不存在?
- 请问:引用远程数据链路中,关于使用case when 语句的问题,谢谢
- 各位来看看初学者的一个简单问题:在哪里置在环境变量classpath?
- oracle9i sqlplus的登陆问题
- 百分求解高速SQL文
- 请教大神,这段SQL写的觉得太复杂,能否再优化,最好能把优化后的SQL写出来,oracle数据库。
- 这个sql怎么写?
- 求助在这种表结构下如何写出Sql语句进行汇总统计,希望高手指点。
---sql有点复杂..呵呵..抛砖引玉吧...好象hql还不行
SQL> with tab as
2 (
3 select 1 id, 'aa' name
4 from dual
5 union all
6 select 2 id, 'bb' name
7 from dual
8 union all
9 select 1 id, 'dd' name
10 from dual
11 union all
12 select 2 id, 'ee' name from dual
13 )
14 select t.id, t.name
15 from tab t,
16 (select id, name
17 from (select id,
18 name,
19 row_number() over(partition by id order by dbms_random.value()) rn
20 from tab) tt
21 where tt.rn = 1) t1
22 where t1.id = t.id
23 and t1.name = t.name
24 / ID NAME
---------- ----
1 dd
2 bbSQL>
SQL> with tab as
2 (
3 select 1 id, 'aa' name
4 from dual
5 union all
6 select 2 id, 'bb' name
7 from dual
8 union all
9 select 1 id, 'dd' name
10 from dual
11 union all
12 select 2 id, 'ee' name from dual
13 )
14 select t.id, t.name
15 from tab t,
16 (select id, name
17 from (select id,
18 name,
19 row_number() over(partition by id order by dbms_random.value()) rn
20 from tab) tt
21 where tt.rn = 1) t1
22 where t1.id = t.id
23 and t1.name = t.name
24 / ID NAME
---------- ----
1 aa
2 eeSQL>
from USER tab1 inner join (select ID,max(NAME) as NAME from USER group by ID)tab2 on tab1.ID=tab2.ID
where
tab1.NAME=tab2.NAME吸铁石童鞋 我来抢分了
select 1 id, 'aa' name from dual
union all
select 2 id, 'bb' name from dual
union all
select 1 id, 'dd' name from dual
union all
select 2 id, 'ee' name from dual
)
select t.id,t.name from temp t,
(
select id,name from (
select id,row_number() over(partition by id order by id desc) rn,name from temp
) where rn = 1
) a where t.id = a.id and t.name != a.name
SELECT ID,NAME FROM ( SELECT t.*,RANK() OVER (PARTITION BY ID ORDER BY NAME) rn FROM table1 t) a WHERE a.rn=1;
1 aa
2 bb
1 dd
2 ee
这边的id和name是两张表的,就是tab1里面有个主键id,是tab2的外键,而name是tab2的中的一个字段
23 test4
23 test10好几条记录,而我想要的是23的就只有一条记录,后面的name随便哪一个都可以的
(SELECT ID,NAME FROM ( SELECT t.*,RANK() OVER (PARTITION BY ID ORDER BY NAME) rn FROM tab2 t) a WHERE a.rn=1) tab2
WHERE tab1.id=tab2.id;