3

我使用 SQL Server 2008 R2。

我有一个奇怪的问题如下。我有一张如图所示的表格

在此处输入图像描述

我需要编写这样的查询:

SELECT DISTINCT Field1 
FROM MYTABLE 
WHERE Field2 IN (96,102)

在这个查询中,WHERE Field2 IN (96,102)给我 96 或 102 或两者!

此外,我想同时返回包含 96 和 102 的行!

有什么建议吗?请以结果为导向写...

4

4 回答 4

2

I have made a sqlfiddle for this..

create table a (id int, val int)
go
insert into a select 1, 22
insert into a select 1, 122
insert into a select 2, 22
insert into a select 3, 122
insert into a select 4, 22
insert into a select 4, 122

then select like this

select count(distinct id), id
from a
where val in (22, 122)
group by id
having count(id) > 1

EDIT: count(distinct id) will only show distinct counts..

于 2012-10-27T07:28:37.123 回答
1

编辑:

这是一个 sqlfiddle 示例(感谢 Mark Kremers):

http://sqlfiddle.com/#!3/df201/1

create table mytable (field1 int, field2 int)
go
insert into mytable values (199201, 84)
insert into mytable values (199201, 96)
insert into mytable values (199201, 102)
insert into mytable values (199201, 103)
insert into mytable values (581424, 96)
insert into mytable values (581424, 84)
insert into mytable values (581424, 106)
insert into mytable values (581424, 122)
insert into mytable values (687368, 79)
insert into mytable values (687368, 96)
insert into mytable values (687368, 102)                                                       
insert into mytable values (687368, 104)                            
insert into mytable values (687368, 106)

这是查询:

select distinct a.field1 from
( select field1 from mytable where field2=96) a
  inner join 
( select field1 from mytable where field2=102) b
  on a.field1 = b.field1

结果如下:

FIELD1
199201
687368

最后,这是查询的简化版本(而不是 pst):

select distinct a.field1 from  mytable a 
inner join mytable b   
  on a.field1 = b.field1 
where a.field2=96 and b.field2=102
于 2012-10-27T05:48:39.310 回答
1

使用自加入?不是最整洁的,但我认为它适用于 2 个值

SELECT *
FROM T R1
JOIN T R2                        -- join table with itself
ON R1.F1 = R2.F1                 -- where the first field is the same
WHERE R1.F2 = 96 AND R2.F2 = 102 -- and each has one of the required values

(T = 表,Rx = 关系别名,Fx = 字段)

如果可以有任意数量的字段,这可以解决为

CREATE TABLE #T (id int, val int)
GO
INSERT INTO #T (id, val)
VALUES
  (1, 22), (1, 22),     -- no,  only 22 (but 2 records)
  (2, 22), (2, 122),    -- yes, both values (only)
  (3, 122),             -- no,  only 122
  (4, 22), (4,122),     -- yes, both values ..
  (4, 444), (4, null),     -- and extra values
  (5, 555)              -- no,  neither value
GO
-- Using DISTINCT over filtered results first, as
-- SQL Server 2008 does not support HAVING COUNT(DISTINCT F1, F2)
SELECT id
FROM (SELECT DISTINCT id, val
      FROM #T
      WHERE val IN (22, 122)) AS R1
GROUP BY id
HAVING COUNT(id) >= 2 -- or 3 or ..
GO
-- Or a similar variation, as can COUNT(DISTINCT ..)
-- in the SELECT of a GROUP BY
SELECT id
FROM (SELECT id, COUNT(DISTINCT val) as ct
      FROM #T
      WHERE val IN (22, 122)
      GROUP BY id) AS R1
WHERE ct >= 2 -- or 3 or ..
GO

对于较大的IN (..)尺寸,比如超过 20 个值,出于性能原因,建议使用单独的表或表值和 a JOIN

于 2012-10-27T05:53:38.380 回答
0

从您的原始查询中尝试:

SELECT DISTINCT Field1 
FROM MYTABLE 
WHERE rtrim(ltrim(cast(Field2 as varchar))) IN ('96','102')
于 2012-10-29T16:02:48.370 回答