sql语句。

zhangA 2010-09-14 05:07:47
表A 字段 编号,流程
数据 编号 流程
1 0
2 0
2 1
3 0
3 1

要求查询 表中流程未进行到1的。

表中数据几百万条,求最优sql语句。

...全文
152 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
hunter0147 2010-09-15
  • 打赏
  • 举报
回复
select 编号 from tb a where a.编号 not in (select 编号 from tb a where a.流程=1)
binbinazi 2010-09-15
  • 打赏
  • 举报
回复
我也同意5楼的的方法,创建一个个索引
baiying15 2010-09-15
  • 打赏
  • 举报
回复
越看越晕,不晓得楼主到底想要咋个;觉得这个不错。[Quote=引用 8 楼 haiwer 的回复:]
SQL code
select 编号,流程 from A a1
where not exists (
select 1 from a
where 编号 = a1.编号
and 流程=1
)
[/Quote]
fengyun142415 2010-09-15
  • 打赏
  • 举报
回复

select 编号 from t group by 编号 having count(编号)<2
fengyun142415 2010-09-15
  • 打赏
  • 举报
回复

select 编号 from t
where 编号 not in
(select 编号 from t
where 流程=1)
samchoy 2010-09-15
  • 打赏
  • 举报
回复
按我的理解楼主要的是这个
select 编号 from tab 
where 编号 not in (select 编号 from tab where 流程=1)
group by 编号
hovy_yang 2010-09-15
  • 打赏
  • 举报
回复
select 编号,流程 from A where 流程<>1
ask_chang 2010-09-15
  • 打赏
  • 举报
回复
select 编号,流程 from A a1
where not exists (select 1 from a where 编号 = a1.编号 and 流程=1)
siegebaoniu 2010-09-15
  • 打赏
  • 举报
回复
/*if object_id('H') is not null drop table H
create table H(num int,data int)

insert H
select 1, 0 union all
select 2, 0 union all
select 2, 1 union all
select 3, 0 union all
select 3, 1 union all
select 4, 0 */

select num from H group by num having max(data)=0
shadowdudu 2010-09-14
  • 打赏
  • 举报
回复

if object_id('tb') IS NOT NULL
DROP TABLE tb
go
create table tb
(bh int,
lc int
)
insert into tb
select 1,0
union all
select 2,0
union all
select 2,1
union all
select 3,0
union all
select 3,1
GO

select * from tb where bh not in( select bh from tb where lc=1)

shadowdudu 2010-09-14
  • 打赏
  • 举报
回复
if object_id('tb') IS NOT NULL
DROP TABLE tb
go
create table tb
(bh int,
lc int
)
insert into tb
select 1,0
union all
select 2,0
union all
select 2,1
union all
select 3,0
union all
select 3,1
GO

select * from tb where bh not in( select bh from tb where lc=1)
王向飞 2010-09-14
  • 打赏
  • 举报
回复

--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:[TB]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]')
AND type in (N'U'))
DROP TABLE [TB]
GO

---->建表
create table [TB]([编号] int,[流程] int)
insert [TB]
select 1,0 union all
select 2,0 union all
select 2,1 union all
select 3,0 union all
select 3,1
GO



--> 查询结果
SELECT a.* FROM [TB] a join (select distinct 编号 from TB) b
on a.编号 = b.编号 and a.编号<=1
--> 删除表格
--DROP TABLE [TB]

HSBOY86 2010-09-14
  • 打赏
  • 举报
回复
如果 流程为INT类
select 编号,流程 from A where 流程>1 and 流程<1

zhangA 2010-09-14
  • 打赏
  • 举报
回复
编号是有用的。
zhangA 2010-09-14
  • 打赏
  • 举报
回复
我要的结果不是<=1

比如数据中2这个编号已经有过流程为1的记录了,所以2就不应该在查询结果中,
我要的是所有编号没有>1的记录的数据
shadowdudu 2010-09-14
  • 打赏
  • 举报
回复
那就把不等修改成小于
可以按照前面那位说的先建立索引,效率会更高些
昵称被占用了 2010-09-14
  • 打赏
  • 举报
回复
建立复合的聚集索引(编号,流程),不过你不带其它条件的情况下查询,估计作用不太大
昵称被占用了 2010-09-14
  • 打赏
  • 举报
回复
select 编号,流程 from A a1
where not exists (
select 1 from a
where 编号 = a1.编号
and 流程=1
)
王向飞 2010-09-14
  • 打赏
  • 举报
回复
<=1
zhangA 2010-09-14
  • 打赏
  • 举报
回复
查询的结果应该只有 1 0 这条记录。

条件是 未进行到1

这个表是一个日志表,记录的是流程,每个编号都会从0开始执行到1,2,3,4

现在要查没有执行到1的记录。

select 编号,流程 from A where 流程<>1

会把所有记录都查出来的。
加载更多回复(6)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧