假设表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
并且不能使用游标 存储过程 或临时表,因为没有权限.
应该怎么写?
谢谢各位了.

解决方案 »

  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
    */
    你改下字段名和表名就可以了
      

  2.   

    select top 1 colb,colc from a where a.cola = '...'
      

  3.   


    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分组
      

  4.   

    怎么觉得你写的好象有些问题,最后只有Order by A,并没有按A分组,对楼主这个例子似乎正确,但是如果数据很多的话,A,B,C,有很多重复的就不正确了。
    楼主没有说明表的主码。其实建视图这个问题应该简单。
      

  5.   


    order by是排序
    where A=t.A 相对于分组。
      

  6.   

    我给你留了言。象下面的例子就不正确了。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,2select * from @table t where b=
    (select max(b) from @table where A=t.A) order by a
      

  7.   

    这个问题出在需求上:
    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条
      

  8.   

    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 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只能再嵌套一次,可以解决!!!