表Ade结构如下(ID列为自动填充列,同一品种的批号不会相同):
ID  名称   批号  数量
1   test   a001   10
2   tecd   8978   23
3   test   a002   0
4   asff   2d3fe  100
5   swed   058ef  0
6   swed   082sc  0
7   test   a3c08  239
..............
要求查询的结果是 
1、取出每一品种现在各批号的数量情况 
2、但是同一品种的所有批号的数量都为0则只取该品种的其中一个批号就行了(即只取一条记录如名称为swed的只取一条)
3、同一品种若有两个或以上批号,其中批号的数量为0的记录就不要了,只要有数量的记录(如名称为test 的)。
不知道说明白没?求SQL查询语句??

解决方案 »

  1.   

    1、select * from Ade where 名称='''+'品种'+'''
    2、跟第一条一样,但是查到结果后判断记录数量是否都为0
    3、跟第一条一样。但是查处结果判断数量是否是0 ,不是就保留
      

  2.   

    with aq do 
    begin
     close
    sql.clear;
    sql.add('select * from Ade where 名称='''+'品种'+'''');
    open;
     if recordcount<>0 then
     begin
      for i:=1 to recordcount do
      begin
       if fieldbyname('数量').asvalue<>0 then 
       begin
          ....不全是0....有效记录
            count:=count+1;
       end;
       next;
      end;
      if count:=0 then ....说明这个品种数量都是 0
     end;
    end;
    end;
      

  3.   

    我是想在SQL查询分析器中用SQL查询出来,
    求查询语句?
      

  4.   


    DECLARE @T TABLE (id INT,名称 VARCHAR(12), 批号 varchar(12), 数量 int)
    INSERT INTO @T
    select 1, 'test', 'a001', 10 union all
    select 2, 'tecd', '8978', 23 union all
    select 3, 'test', 'a002', 0 union all
    select 4, 'asff', '2d3fe', 100 union all
    select 5, 'swed', '058ef', 0 union all
    select 6, 'swed', '082sc', 0 union all
    select 7, 'test', 'a3c08', 239select * from @T where id in (
    select top 1 id from @T where 名称+'0' in  
    (select 名称+cast(sum(数量) as varchar) from @T  group by 名称 having sum(数量) = 0)
    union select id from @T where 数量 <> 0)/* result
    1 test a001 10
    2 tecd 8978 23
    4 asff 2d3fe 100
    5 swed 058ef 0
    7 test a3c08 239
    */