表结构和数据: a b c ....flg111 1 1 ..... 1
111 1 1 ..... 0
222 1 1 ..... 1
333 1 1 ..... 0
选择数据的规则:选出所有flg为0的数据,根据a,b,c3个字段,如果有flg为0的记录,则需要选出其所有flg为1的记录,希望的结果如下:a b c ....flg111 1 1 ..... 1
111 1 1 ..... 0
333 1 1 ..... 0哪位这样的查询如何写呢?谢谢。
111 1 1 ..... 0
222 1 1 ..... 1
333 1 1 ..... 0
选择数据的规则:选出所有flg为0的数据,根据a,b,c3个字段,如果有flg为0的记录,则需要选出其所有flg为1的记录,希望的结果如下:a b c ....flg111 1 1 ..... 1
111 1 1 ..... 0
333 1 1 ..... 0哪位这样的查询如何写呢?谢谢。
解决方案 »
- 求时间间隔为6个月的sql语句?
- 数据统计!
- 对三大财务报表的数据运算得到指标,插入指标库,几十万条记录,用游标合理吗?
- sql server中与oracle 中nvl(,)函数相对应的函数是什么??
- set implicit_transactions on 事务问题
- 有SQL Server Management Studio,sql2000能连接sql2005吗
- 求助,关于从表A中查询大量符合条件的数据,添加到表B
- 一个让人头疼的SQL语句(急!急!急!)
- 请教 SQL Analysis Manager 问题:多维数据集无法显示
- 求一数据库部署、更新软件,谢谢。
- 时间转换???——在线等!!!!!!!!!
- 多级分类汇总可以一条SQL语句搞定吗?
insert into #test
select 111,1,1,1 union all
select 111,1,1,0 union all
select 222,1,1,1 union all
select 333,1,1,0select * from #test where flg = 0
union
select * from #test t where flg = 1 and exists(select 1 from #test where #test.a = t.a
and #test.b = t.b and #test.c = t.c and #test.flg <> t.flg) drop table #test
select * from ta as a
where exists(select 1 from ta where a=a.a and b=a.b and c=a.c and flg=0 )
insert into @test
select 111,1,1,1 union all
select 111,1,1,0 union all
select 222,1,1,1 union all
select 333,1,1,0 select * from @test as a
where exists(select 1 from @test where a=a.a and b=a.b and c=a.c and flg=0 )(4 行受影响)
a b c flg
----------- ----------- ----------- -----------
111 1 1 1
111 1 1 0
333 1 1 0(3 行受影响)
insert @a select 111, 1 ,1, 1
union all select 111 ,1, 1, 0
union all select 222, 1 ,1, 1
union all select 333 ,1, 1 , 0select a.a,a.b,a.c,a.flg from @a a Inner Join (select * from @a where flg=0) b on a.a=b.a and a.b=b.b and a.c=b.c