首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql过滤查询有关问题,顶者有分。请大家帮小弟我看看。

2012-12-15 
sql过滤查询问题,在线等。。。。顶者有分。。。请大家帮我看看。。。。select id,underid,jhwgtime from fb_clunderin

sql过滤查询问题,在线等。。。。顶者有分。。。请大家帮我看看。。。。
select id,underid,jhwgtime from fb_clunderinfo
得到的结果是:
id             xdno                day 
185  0000000A29-201111280520-1006  0
186  0000000A29-201111280520-1006  21
187  0000000A29-201111280520-1006  0
188  0000000A29-201111280520-1006  0
189  0000000A34-201111280521-988   45
190  0000000A34-201111280521-988   0
191  0000000A34-201111280521-988   0
192  0000000A31-201111280522-1036  37
193  0000000A31-201111280522-1036  23
194  0000000A31-201111280522-1036  0
195  0000000A33-201111280523-988   0
196  0000000A33-201111280523-988   0
197  0000000A33-201111280523-988   0
198  0000000A32-201111280524-1036  9
199  0000000A32-201111280524-1036  10

我现在需要查询出来的数据是:0000000A32-201111280524-1036(存在两条数据,两条数据的day都不等于0)

规则是:要检索每一项的day不等于0的xdno。。。。。

问题解决,立刻结贴,,请牛人帮忙。。。
[最优解释]


select id,underid,jhwgtime 
from fb_clunderinfo t
where not exists (select 1 from fb_clunderinfo where xdno=t.xdno and [day]=0)

[其他解释]
use Tempdb
go
--> --> 
 
if not object_id(N'fb_clunderinfo') is null
drop table fb_clunderinfo
Go
Create table fb_clunderinfo([id] int,[xdno] nvarchar(28),[day] int)
Insert fb_clunderinfo
select 185,N'0000000A29-201111280520-1006',0 union all
select 186,N'0000000A29-201111280520-1006',21 union all
select 187,N'0000000A29-201111280520-1006',0 union all
select 188,N'0000000A29-201111280520-1006',0 union all
select 189,N'0000000A34-201111280521-988',45 union all
select 190,N'0000000A34-201111280521-988',0 union all
select 191,N'0000000A34-201111280521-988',0 union all
select 192,N'0000000A31-201111280522-1036',37 union all
select 193,N'0000000A31-201111280522-1036',23 union all
select 194,N'0000000A31-201111280522-1036',0 union all
select 195,N'0000000A33-201111280523-988',0 union all
select 196,N'0000000A33-201111280523-988',0 union all
select 197,N'0000000A33-201111280523-988',0 union all
select 198,N'0000000A32-201111280524-1036',9 union all
select 199,N'0000000A32-201111280524-1036',10
Go


select id,[xdno],[day] from fb_clunderinfo AS a WHERE EXISTS(SELECT 1 FROM fb_clunderinfo WHERE [xdno]=a.[xdno] AND id<>a.id AND [day]>0) AND [DAY]>0

/*
idxdnoday
1920000000A31-201111280522-103637
1930000000A31-201111280522-103623
1980000000A32-201111280524-10369
1990000000A32-201111280524-103610
*/

[其他解释]
----------------------------
-- Author  :fredrickhu(小F,向高手学习)


-- Date    :2011-11-30 17:13:44
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
--Apr 22 2011 11:57:00 
--Copyright (c) Microsoft Corporation
--Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([id] int,[xdno] varchar(28),[day] int)
insert [tb]
select 185,'0000000A29-201111280520-1006',0 union all
select 186,'0000000A29-201111280520-1006',21 union all
select 187,'0000000A29-201111280520-1006',0 union all
select 188,'0000000A29-201111280520-1006',0 union all
select 189,'0000000A34-201111280521-988',45 union all
select 190,'0000000A34-201111280521-988',0 union all
select 191,'0000000A34-201111280521-988',0 union all
select 192,'0000000A31-201111280522-1036',37 union all
select 193,'0000000A31-201111280522-1036',23 union all
select 194,'0000000A31-201111280522-1036',0 union all
select 195,'0000000A33-201111280523-988',0 union all
select 196,'0000000A33-201111280523-988',0 union all
select 197,'0000000A33-201111280523-988',0 union all
select 198,'0000000A32-201111280524-1036',9 union all
select 199,'0000000A32-201111280524-1036',10
--------------开始查询--------------------------
select
  *
from
  tb t
where
  not exists(select xdno from tb where xdno=t.xdno and day=0)
  
----------------结果----------------------------
/* id          xdno                         day
----------- ---------------------------- -----------
198         0000000A32-201111280524-1036 9
199         0000000A32-201111280524-1036 10

(2 行受影响)
*/


[其他解释]
别发重复贴了,第一帖已有答案。
[其他解释]

create table fb_clunderinfo([id] int,[xdno] nvarchar(28),[day] int)
insert fb_clunderinfo
select 185,N'0000000a29-201111280520-1006',0 union all
select 186,N'0000000a29-201111280520-1006',21 union all
select 187,N'0000000a29-201111280520-1006',0 union all
select 188,N'0000000a29-201111280520-1006',0 union all
select 189,N'0000000a34-201111280521-988',45 union all
select 190,N'0000000a34-201111280521-988',0 union all
select 191,N'0000000a34-201111280521-988',0 union all
select 192,N'0000000a31-201111280522-1036',37 union all
select 193,N'0000000a31-201111280522-1036',23 union all
select 194,N'0000000a31-201111280522-1036',0 union all
select 195,N'0000000a33-201111280523-988',0 union all
select 196,N'0000000a33-201111280523-988',0 union all
select 197,N'0000000a33-201111280523-988',0 union all


select 198,N'0000000a32-201111280524-1036',9 union all
select 199,N'0000000a32-201111280524-1036',10
go

select [id],[xdno],[day] 
from fb_clunderinfo t
where not exists (select 1 from fb_clunderinfo where xdno=t.xdno and [day]=0)

drop table fb_clunderinfo

/*********************

id          xdno                         day
----------- ---------------------------- -----------
198         0000000a32-201111280524-1036 9
199         0000000a32-201111280524-1036 10

(2 行受影响)


[其他解释]
一楼的结果是对的,,
二楼的结果有点问题,按这些数据,应该能查到的编号是0000000A32-201111280524-1036    

谢谢两位,我贴子一贴出来,去数据库在鼓捣鼓捣,就出来了,我写的和一楼的差不多,我用的是not in。。。

非常感谢两位,,,马上结贴!
[其他解释]
存在两条数据,两条数据的day都不等于0

理解錯了,理解為兩條以上不為0的記錄

這樣改


use Tempdb
go
--> --> 
 
if not object_id(N'fb_clunderinfo') is null
drop table fb_clunderinfo
Go
Create table fb_clunderinfo([id] int,[xdno] nvarchar(28),[day] int)
Insert fb_clunderinfo
select 185,N'0000000A29-201111280520-1006',0 union all
select 186,N'0000000A29-201111280520-1006',21 union all
select 187,N'0000000A29-201111280520-1006',0 union all
select 188,N'0000000A29-201111280520-1006',0 union all
select 189,N'0000000A34-201111280521-988',45 union all
select 190,N'0000000A34-201111280521-988',0 union all
select 191,N'0000000A34-201111280521-988',0 union all
select 192,N'0000000A31-201111280522-1036',37 union all
select 193,N'0000000A31-201111280522-1036',23 union all
select 194,N'0000000A31-201111280522-1036',0 union all
select 195,N'0000000A33-201111280523-988',0 union all
select 196,N'0000000A33-201111280523-988',0 union all
select 197,N'0000000A33-201111280523-988',0 union all
select 198,N'0000000A32-201111280524-1036',9 union all
select 199,N'0000000A32-201111280524-1036',10
Go

SELECT * FROM fb_clunderinfo AS a WHERE [day]>0 AND  EXISTS(SELECT * FROM fb_clunderinfo WHERE [xdno]=a.[xdno] HAVING COUNT(DISTINCT SIGN([day]))=1)
/*
idxdnoday
1980000000A32-201111280524-10369
1990000000A32-201111280524-103610
*/
;WITH b
AS
(
select id,[xdno],[day],ROW_NUMBER()OVER(ORDER BY ID)-ROW_NUMBER()OVER(PARTITION BY [xdno] ORDER BY [id] )  AS group1
from fb_clunderinfo
)
SELECT a.[id],a.[xdno],a.[day]
FROM b AS a
INNER JOIN 
(SELECT [xdno],group1 FROM b GROUP BY [xdno],group1 HAVING COUNT(CASE WHEN [day]>0 THEN 1 end)>1 AND COUNT(DISTINCT SIGN([day]))=1) AS b ON a.[xdno]=b.[xdno] AND a.group1=b.group1

/*
idxdnoday
1980000000A32-201111280524-10369
1990000000A32-201111280524-103610


*/

热点排行