select a,distinct b from table order by a

解决方案 »

  1.   

    select table1.a,distinct table2.b from table1,table2
    where table1.关联字段=table2.相应的关联字段
      

  2.   

    我的代码是这样写的, 
    QueryTable1.SQL.Text:='select StartTime,StopTime, distinct Blt '+
                  'from Flow where StartTime>=:StartTime '+
                  'and StartTime<=:EndTime order by StartTime';
    在我开始查询的时候出现异常,说“语法错误(操作符丢失)在查询表达式'distict Blt'中”
    如果我将distinct提到最前面去就不会有问题了,但这样显然不符合我的要求。
      

  3.   

    select distinct b from table1
    order by a;
      

  4.   

    sql 的 格式
    SELECT DISTINCT <field_name,...field_name_n> FROM <table_name>
      

  5.   

    erickleung的这种写法是不对的,这样就不能将a也选到查询结果中去了。
      

  6.   

    回: zhangpeigao,我是要从同一个表中选择两个字段,而不是两个表
      

  7.   

    假如table1中的关键字段为c,sql可如下描述:  select a,b from table1 ee,(select distinct b,c from table1) ff
       where ee.c = ff.c
        order by ee.a
      

  8.   


    上面错了,sql应为:
       select ee.a,ee.b from table1 ee,(select distinct b,c from table1) ff
       where ee.c = ff.c
        order by ee.a
      

  9.   

    Blt   StartTime    StopTimeA     2000-01-01 2000-01-02
    A     2000-01-01 2000-01-03
    B     2000-01-01 2000-01-02
    B     2000-01-01 2000-01-04
    你想要如何结果
      

  10.   

    如果没有关键字段怎么办?(现在假设这个表里就A,B两个字段,且A,B都可能重复)
      

  11.   

    我想知道在DELPHI中,有没有函数把Memo中的信息以每行为单位读出呢,或者是MEMO 中有无属性完成此功能呢?
      

  12.   

    Blt   StartTime    StopTime
    A     2000-01-01 2000-01-02
    B     2000-01-01 2000-01-02
      

  13.   

    我想知道在DELPHI中,有没有函数把Memo中的信息以每行为单位读出内容呢,或者是MEMO 中有无属性完成此功能呢?
         请高手们帮个忙,我正在做这方面的事,很麻烦呀!
      

  14.   

    select  ee.a,ee.b from table1 ee,(select distinct b,a from table1) ff
       where ee.a = ff.a and ee.b = ff.b
        order by ee.a
      

  15.   

    select Bit,StartTime,StopTime from Flow AAA
    where AAA.StartTime=(select min(StartTime) from Flow BBB where    AAA.Bit=BBB.Bit)
    and AAA.StopTime =(select min(StopTime ) from Flow BBB where    AAA.Bit=BBB.Bit)
    and StartTime>=:StartTime and StartTime<=:EndTime
    //建议可以的话加以标志字段,每次新增一条同Bit时,对标志字段加以控制这样简单
      

  16.   

    我取数据的标准就是按照A的值来取的,A可以重复,但B不允许重复
      

  17.   

    恐怕是做不到的,如:table内容如下:
      A  B
      1  0
      2  0请问你如何对A进行取舍呢?没有这个规则,上面写的我想都是错的。
      

  18.   

    /* 没有调试过 */SELECT A, B
    FROM table1
    WHERE B IN(
       SELECT B
       FROM Table1
       GROUP BY B
       HAVING Count(*) > 1)
    ORDER BY B
      

  19.   

    你的table里,你说A、B都有重复的可能,那你说,在字段B里重复的数据,你想按什么标准提取它?
      

  20.   

    我明白了Bit是标记,Bit为A可以重复,Bit为B只取一条最早的?
      

  21.   

    那你最好在table 里加一个自动增长的id
    然后:
    select a,b from table1 where id=min(id)
    group by b
      

  22.   

    是的,Bit不能重复,而A则随意取,一般来说取第一个。
      

  23.   

    还是tinyapfel(tinyapfel) 说的有道理,你不会转牛角尖吧?还是重新设置以下你的表吧。
      

  24.   

    select bit,StartTime,StopTime from Flow 
    where (bit='A') or
    ((bit='B')and(StartTime=(select min(StartTime) from Flow BBB where Flow.Bit=BBB.Bit )))
      

  25.   

    当然,可以改变表的结构,还有很多方法都能做到这一点。比如用两个查询,但我想知道能不能用一个SQL语句做到这一点,因为这个问题看上去不是很复杂。
      

  26.   

    select a,distinct b from table order by a
      

  27.   

    zhangpeigao可能误会我的意思了,我举的A,B是两个字段,是抽象出来的一个例子,而bit,StartTime,StopTime三个字段是我在实际编程中的源代码,Bit也不是字符型变量
      

  28.   

    select a,distinct b from table order by a
    这句话能用的话,我们也不会费这么多口舌了。我一开始就是这样用的,但SQL语法并不支持
      

  29.   

    Memo的信息当然可以按行读取
    Memo.lines[i]就是行
      

  30.   

    select a,b from t1 where a=(select min(a) from t1 group by b)
    order by a
      

  31.   

    SELECT MAX(A) AS AA, B FROM Table
    GROUP BY B
      

  32.   

    sorry ,错了, 要加上distinct
      

  33.   

    ACCESS上面已经举过例子,
    输入:
    Blt   StartTime    StopTimeA     2000-01-01 2000-01-02
    A     2000-01-01 2000-01-03
    B     2000-01-01 2000-01-02
    B     2000-01-01 2000-01-04输出:Blt   StartTime    StopTimeA     2000-01-01 2000-01-02
    B     2000-01-01 2000-01-02
      

  34.   

    tinyapfel
    似乎给出了正确答案
      

  35.   

    SELECT Blt, MAX(StartTime) AS StartTime FROM Table
    GROUP BY Blt
      

  36.   

    tinyapfel,似乎不行呀,出现异常说,子查询最多能返回一个纪录
      

  37.   

    mengmengy的好像也不行,说语法错误
      

  38.   

    select Blt,StartTime,StopTime
    from table1 as A
    where StartTime = 
      (select top 1 StartTime 
       from table1 
       where Blt = A.Blt) and   
      StopTime = 
      (select top 1 StopTime 
       from table1 
       where Blt = A.Blt)
    group by Blt,StartTime,StopTime