select 字段1 ,字段2 ,字段3,min(字段4),max(字段4) from a group by 字段1 ,字段2 ,字段3
SQL> SELECT * FROM T;F1 F2 F3 F4 -------- -------- -------- -------- A B C 001 A B C 002 A B C 003 A B D 004SQL> SQL> SELECT F1,F2,F3,MIN(F4),MAX(F4) FROM T 2 GROUP BY F1,F2,F3;F1 F2 F3 MIN(F4) MAX(F4) -------- -------- -------- -------- -------- A B C 001 003 A B D 004 004
--> --> (jake)生成测试数据
declare n number; begin select count(*) into n from user_tables where table_name=upper('T'); if n>0 then execute immediate 'drop table T'; end if; end; Create table T as select 'A' 字段1,'B' 字段2,'C'字段3 ,001 字段4 from dual union all select 'A','B','C',002 from dual union all select 'A','B','C',003 from dual union all select 'A','B','D',004 from dual ; /*对于上表 当字段1,字段2,字段3相同的时候 请找出字段4 的最小值和最大值 */select 字段1 ,字段2 ,字段3,min(字段4),max(字段4) from T group by 字段1 ,字段2 ,字段3
select 字段1 ,字段2 ,字段3,min(字段4),max(字段4) from a group by 字段1 ,字段2 ,字段3
字段1 字段2 字段3 字段4 A B C 001 A B C 002 A B C 003 A B D 004 不好意思 刚才我说错了 我的原意是select 字段1 ,字段2 ,字段3 from a where 字段1=a and 字段2=b and 字段3=c and (字段1/字段2/字段3相同的时候字段4的最小值 OR 字段1/字段2/字段3相同的时候字段4的最大值 ) group by 字段1 ,字段2 ,字段3
不理解你的意思呀,where条件
select 字段1 ,字段2 ,字段3,max(字段4) from a group by 字段1 ,字段2 ,字段3union select 字段1 ,字段2 ,字段3,min(字段4) from a group by 字段1 ,字段2 ,字段3
from a
group by 字段1 ,字段2 ,字段3
SQL> SELECT * FROM T;F1 F2 F3 F4
-------- -------- -------- --------
A B C 001
A B C 002
A B C 003
A B D 004SQL>
SQL> SELECT F1,F2,F3,MIN(F4),MAX(F4) FROM T
2 GROUP BY F1,F2,F3;F1 F2 F3 MIN(F4) MAX(F4)
-------- -------- -------- -------- --------
A B C 001 003
A B D 004 004
declare n number;
begin
select count(*) into n from user_tables where table_name=upper('T');
if n>0 then
execute immediate 'drop table T';
end if;
end;
Create table T
as
select 'A' 字段1,'B' 字段2,'C'字段3 ,001 字段4 from dual union all
select 'A','B','C',002 from dual union all
select 'A','B','C',003 from dual union all
select 'A','B','D',004 from dual
;
/*对于上表 当字段1,字段2,字段3相同的时候
请找出字段4 的最小值和最大值 */select 字段1 ,字段2 ,字段3,min(字段4),max(字段4)
from T
group by 字段1 ,字段2 ,字段3
from a
group by 字段1 ,字段2 ,字段3
A B C 001
A B C 002
A B C 003
A B D 004 不好意思 刚才我说错了
我的原意是select 字段1 ,字段2 ,字段3
from a
where 字段1=a and 字段2=b and 字段3=c and
(字段1/字段2/字段3相同的时候字段4的最小值 OR 字段1/字段2/字段3相同的时候字段4的最大值 )
group by 字段1 ,字段2 ,字段3
from a
group by 字段1 ,字段2 ,字段3union select 字段1 ,字段2 ,字段3,min(字段4)
from a
group by 字段1 ,字段2 ,字段3