菜鸟求两SQL语句。

MOmo400 2011-09-14 04:29:43
有一表。

CREATE TABLE NFLog
(
NotificationLogId NUMERIC(17,0) NOT NULL,
StationId INT NOT NULL,
EquipmentId INT NOT NULL,
EventId INT NOT NULL,
EventConditionId INT NOT NULL,
StartTime DATETIME NOT NULL,
EventStatus INT NOT NULL
)

其中EventStatus的值只为1或者2

第一条SQL语句,取得数据 StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus不同的数据
第二条SQL语句,取得数据 只有EventStatus=1的数据,并且不能存在StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus=2的数据.

注,数据量很大,一百万条数据以上,请注意运行效率。
...全文
181 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
MOmo400 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 bitls 的回复:]
帮楼主顶顶
[/Quote]
谢谢,你是好人
bitls 2011-09-15
  • 打赏
  • 举报
回复

帮楼主顶顶
MOmo400 2011-09-15
  • 打赏
  • 举报
回复
求更好的SQL呀。
Warren 2011-09-15
  • 打赏
  • 举报
回复
PK是什么?
如果StationId,EquipmentId,EventId,EventConditionId,StartTime这些字段上有索引,可以考虑用Join语句:
select 
Log_a.*
from dbo.NFLog Log_a
join dbo.NFLog Log_b
on Log_a.StationId = Log_b.StationId
and Log_a.EquipmentId = Log_b.EquipmentId
and Log_a.EventId = Log_b.EventId
and Log_a.EventConditionId = Log_b.EventConditionId
and Log_a.StartTime = Log_b.StartTime
--and Log_a.[NotificationLogId] != Log_b.[NotificationLogId]
where Log_a.EventStatus != Log_b.EventStatus;
MOmo400 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 acherat 的回复:]
引用 10 楼 momo400 的回复:
引用 5 楼 acherat 的回复:
引用 4 楼 chuanzhang5687 的回复:
一楼跑哪去了?


我的1楼没呢???

SQL code

--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.Statio……
[/Quote]

我还刚刚想说大家肯定会让我加上索引什么的。

索引肯定是有了。

不过发这个帖子的目地是想得到在相同环境下最佳的方案。

谢谢你的方案,是否可以有更加优化的呢?
AcHerat 元老 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 momo400 的回复:]
引用 5 楼 acherat 的回复:
引用 4 楼 chuanzhang5687 的回复:
一楼跑哪去了?


我的1楼没呢???

SQL code

--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = ……
[/Quote]

关于那几个等于条件的字段加索引,其他没啥可看的了。
dawugui 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 momo400 的回复:]
引用 9 楼 dawugui 的回复:
--第一条SQL语句,取得数据 StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus不同的数据

select distinct StationId,EquipmentId,EventId,EventConditionId,StartTime,EventStatu……
[/Quote]
这么大数据量,又是多个字段的group by,不可能快得起来.
MOmo400 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dawugui 的回复:]
--第一条SQL语句,取得数据 StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus不同的数据

select distinct StationId,EquipmentId,EventId,EventConditionId,StartTime,EventStatus from nflog

--第二条……
[/Quote]

你理解的意思是正确的,
但是
用group by与having的结果是跑了五分钟了还没出数据来。
有更加优化的方案吗?
MOmo400 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
引用 4 楼 chuanzhang5687 的回复:
一楼跑哪去了?


我的1楼没呢???

SQL code

--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
……
[/Quote]

运行了三分钟才出结果,有没有优化方案?
dawugui 2011-09-14
  • 打赏
  • 举报
回复
--第一条SQL语句,取得数据 StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus不同的数据

select distinct StationId,EquipmentId,EventId,EventConditionId,StartTime,EventStatus from nflog

--第二条SQL语句,取得数据 只有EventStatus=1的数据,并且不能存在StationId,EquipmentId,EventId,EventConditionId,StartTime相同。但EventStatus=2的数据.
但EventStatus=2的数据是什么意思?不清楚.

select m.* from nflog m,
(select StationId,EquipmentId,EventId,EventConditionId,StartTime from nflog group by StationId,EquipmentId,EventId,EventConditionId,StartTime having count(1) = 1) n
where m.EventStatus=1 and
m.StationId = n.StationId and
m.EquipmentId = n.EquipmentId and
m.EventId = n.EventId and
m.EventConditionId = n.EventConditionId and
m.StartTime = n.StartTime
-晴天 2011-09-14
  • 打赏
  • 举报
回复
?
select distinct StationId,EquipmentId,EventId,EventConditionId,StartTime,EventStatus
from NFLog
行不?
MOmo400 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
引用 4 楼 chuanzhang5687 的回复:
一楼跑哪去了?


我的1楼没呢???

SQL code

--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
……
[/Quote]
意思是对了。谢谢

有没有可以优化的。
MOmo400 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 areswang 的回复:]
搞几条数据出来,可能更直观
[/Quote]

没错,就两值。
AcHerat 元老 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chuanzhang5687 的回复:]
一楼跑哪去了?
[/Quote]

我的1楼没呢???


--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
and EventId = t.EventId and EventConditionId = t.EventConditionId and StartTime = t.StartTime
and EventStatus <> t.EventStatus)

--2

select *
from NFLog t
where not exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
and EventId = t.EventId and EventConditionId = t.EventConditionId and StartTime = t.StartTime
and EventStatus = 2)
and EventStatus = 1


chuanzhang5687 2011-09-14
  • 打赏
  • 举报
回复
一楼跑哪去了?
chuanzhang5687 2011-09-14
  • 打赏
  • 举报
回复
但EventStatus不同的数据
EventStatus 这列不就俩值吗?
AcHerat 元老 2011-09-14
  • 打赏
  • 举报
回复

--1

select *
from NFLog t
where exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
and EventId = t.EventId and EventConditionId = t.EventConditionId and StartTime = t.StartTime
and EventStatus <> t.EventStatus)

--2

select *
from NFLog t
where not exists (select 1 from NFLog where StationId = t.StationId and EquipmentId = t.EquipmentId
and EventId = t.EventId and EventConditionId = t.EventConditionId and StartTime = t.StartTime
and EventStatus = 2)
and EventStatus = 1


???
areswang 2011-09-14
  • 打赏
  • 举报
回复
搞几条数据出来,可能更直观

34,594

社区成员

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

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