拜求各位高手,帮帮小弟!
table1
列1        列2         列3
1          记-1        现金
1          记-1        其他应付款
2          记-2        银行存款
2          记-2        现金
2          记-2        应付账款
3          记-3        应付票据
3          记-3        原材料
4          记-4        银行存款
4          记-4        应付账款
sql查询后形成表如下:(即把列3中为'应付账款’的所有凭证回笼到table2)
table2
列1        列2         列3
2          记-2        银行存款
2          记-2        现金
2          记-2        应付账款
4          记-4        银行存款
4          记-4        应付账款

解决方案 »

  1.   

    --> 测试数据:table1
    if object_id('table1') is not null
    drop table table1---->建表
    create table table1([列1] int,[列2] varchar(4),[列3] varchar(10))
    insert table1
    select 1,'记-1','现金' union all
    select 1,'记-1','其他应付款' union all
    select 2,'记-2','银行存款' union all
    select 2,'记-2','现金' union all
    select 2,'记-2','应付账款' union all
    select 3,'记-3','应付票据' union all
    select 3,'记-3','原材料' union all
    select 4,'记-4','银行存款' union all
    select 4,'记-4','应付账款'--> 查询结果
    select * from table1 where 列1 in (
    SELECT 列1 FROM table1
    where 列3 = '应付账款')
    --> 删除表格
    --DROP TABLE table1
    ----------------------------------------
    列1 列2 列3
    2 记-2 银行存款
    2 记-2 现金
    2 记-2 应付账款
    4 记-4 银行存款
    4 记-4 应付账款
      

  2.   

    select *
    from tb t 
    where exists(select 1 from tb where 列1=t.列1 and 列3='应付账款')
      

  3.   

    insert into table2(col1,col2,col3)
    select col1,col2,col3
    from table1
    where col1 in(select col1 from table1 where col3='应付账款')
      

  4.   


    if object_id('table1') >0 
     drop table table1
    create table table1
    (
    列1 int,
    列2 varchar(20),
    列3 varchar(40)
    )insert into table1 
    select 1,'记-1','现金'
    union all
    select 1,'记-1','其他应付款'
    union all
    select 2,'记-2','银行存款'
    union all
    select 2,'记-2','现金'
    union all
    select 2,'记-2','应付账款'
    union all
    select 3,'记-3','应付票据'select * from table1
    select *  from table1 where 列1 in (select 列1 from table1 where 列3='应付账款')
    结果2 记-2 银行存款
    2 记-2 现金
    2 记-2 应付账款
    再执行下面的代码即可
    select * into table2 from table1 where 列1 in (select 列1 from table1 where 列3='应付账款')
      

  5.   


    create table table2([列1] int,[列2] varchar(6),[列3] varchar(20))
    insert into table2
    select * from table1
    where 列1 in (select 列1 from table1 where 列3='应付账款' )
      

  6.   

    select 
      *
    from 
      tb t 
    where 
      exists
        (
            select * 
            from tb 
            where 列1=t.列1 and 列3='应付账款')
      

  7.   

    Select #T.ID,#T.Desc1,#T.Desc2
    Into Table2
    From (Select * From table1 Where Id In (Select Id From table1 Where Desc2=N'应付账款'))#T