表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
8 tdi  230ce  0
..............
要求查询的结果是  
1、取出每一品种现在各批号的数量情况  
2、但是同一品种的所有批号的数量都为0则只取该品种的其中一个批号就行了(即只取一条记录如名称为swed的只取一条)
3、同一品种若有两个或以上批号,其中批号的数量为0的记录就不要了,只要有数量的记录(如名称为test 的)。
不知道说明白没?求SQL在sql查询分析器中查询语句??
以下是别人给的意见代码,但有个问题就是某品种只有一条记录且数量为0时不在查询结果中了。
SQL code
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
*/

解决方案 »

  1.   

    --那就补上:
    select * 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 or count(*)=1)
    union select id from @T where 数量 <> 0)不过,感觉此sql还是有点问题
      

  2.   

    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', 239 union all
    select 12,'tecd','s343',0 union all
    select 23,'sd','ph',0
    select * from @T where 数量<>0
    union 
    select * from @T where 数量=0
    and 名称 not in (select 名称 from @T where 数量<>0)
    and 名称 not in(select 名称 from @T where 数量=0 group by 名称 having count(名称)<>1)
    union 
    select * from @T c where 数量=0
    and 名称 not in (select 名称 from @T where 数量<>0)
    and id in(select top 1 id from @T d where 数量=0 and 名称=c.名称 order by id desc)
    order by 名称
      

  3.   

    5 swed 058ef 0
    6 swed 082sc 02、但是同一品种的所有批号的数量都为0则只取该品种的其中一个批号就行了(即只取一条记录如名称为swed的只取一条)
    3、同一品种若有两个或以上批号,其中批号的数量为0的记录就不要了,只要有数量的记录(如名称为test 的)。这个条件矛盾了
      

  4.   

    这是改后的,不好意思,今天才有空上了CSDN
    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', 239 union all
    select 8, 'fdfd', '058ef', 0 union all
    select 9, 'srsr', '082sc', 0 union all
    select 10, 'fdfd', '058ef', 0 union all
    select 11, 'srsr', '082sc', 0 union all
    select 12, 'fdfdlll', '058ef', 0 select * from @T where id in (
    select min(id) from @T where 名称+'0' in  
    (select 名称+cast(sum(数量) as varchar) from @T  group by 名称 having sum(数量) = 0)
    group by 名称
    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
    8 fdfd 058ef 0
    9 srsr 082sc 0
    12 fdfdlll 058ef 0
    */