假设表A里有3个字段 ColA,ColB,ColC我要取 select ColB,ColC from 表A Group By ColA;
其中 ColB,ColC是随便一条记录都可以,但是必须是同一条记录的
比如 ColA ColB ColC
1 2 1
2 3 1我希望的结果 可以是 1.2.1 或者 2.3.1,但是不能是 1.3.1
并且不能使用游标 存储过程 或临时表,因为没有权限.
应该怎么写?
谢谢各位了.
其中 ColB,ColC是随便一条记录都可以,但是必须是同一条记录的
比如 ColA ColB ColC
1 2 1
2 3 1我希望的结果 可以是 1.2.1 或者 2.3.1,但是不能是 1.3.1
并且不能使用游标 存储过程 或临时表,因为没有权限.
应该怎么写?
谢谢各位了.
declare @table table (A int,B varchar(7),C int)
insert into @table
select 1,'2009-06',1 union all
select 2,'2009-06',2 union all
select 2,'2009-07',3 union all
select 3,'2009-06',2select * from @table t where b=
(select max(b) from @table where A=t.A) order by a/*
A B C
----------- ------- -----------
1 2009-06 1
2 2009-07 3
3 2009-06 2
*/
你改下字段名和表名就可以了
declare @table table (A int,B int,C int)
insert into @table
select 1,2,1 union all
select 2,3,1 UNION ALL
SELECT 2,2,2select * from @table t where b=
(select max(b) from @table where A=t.A) order by a/*
A B C
----------- ----------- -----------
1 2 1
2 3 1
*/你说的是按A分组
楼主没有说明表的主码。其实建视图这个问题应该简单。
order by是排序
where A=t.A 相对于分组。
insert into @table
select 1,2,1 union all
select 2,3,1 UNION ALL
SELECT 2,3,2select * from @table t where b=
(select max(b) from @table where A=t.A) order by a
declare @table table (A int,B int,C int)
insert into @table
select 1,2,1 union all
select 2,3,1 UNION ALL
SELECT 2,3,2 UNION ALL
SELECT 2,1,2 select * from @table t where b=
(select max(b) from @table where A=t.A) order by a例如上面的数据,
按a分类后,b有两个3,c有两个2,按谁取都取2条
insert into @table
select 1,2,1 union all
select 2,3,1 UNION ALL
SELECT 2,3,2 UNION ALL
SELECT 2,1,2 SELECT aa.a,aa.b,MIN(c) AS c FROM (
select * from @table t where b=
(select max(b) from @table where A=t.A)
) aa GROUP BY aa.a,aa.b order by aa.a只能再嵌套一次,可以解决!!!