Tsql 哪里存在-所有-分组方式?

Tsql 哪里存在-所有-分组方式?,tsql,sql-server-2008-r2,where,exists,Tsql,Sql Server 2008 R2,Where,Exists,我使用SQLServer2008R2 我有一个奇怪的问题如下。我有一张表,如所示 我需要编写这样一个查询,如: SELECT DISTINCT Field1 FROM MYTABLE WHERE Field2 IN (96,102) 在这个查询中,WHERE Field2 in(96102)给我96或102或两者 此外,我想返回同时包含96和102的行 有什么建议吗?请编写面向结果的…编辑: 下面是一个SQLFIDLE示例(感谢Mark Kremers): 问题是: select di

我使用SQLServer2008R2

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

我需要编写这样一个查询,如:

SELECT DISTINCT Field1 
FROM MYTABLE 
WHERE Field2 IN (96,102)
在这个查询中,
WHERE Field2 in(96102)
给我96或102或两者

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

有什么建议吗?请编写面向结果的…

编辑:

下面是一个SQLFIDLE示例(感谢Mark Kremers):

问题是:

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
最后,这里是查询的简化版本(从thans到pst):


使用自联接?不是最整洁的,但我认为它适用于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
对于(..)中较大的
大小,例如超过20个值,出于性能原因,建议使用单独的表或表值和
连接

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
然后像这样选择

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

编辑:计数(不同id)将仅显示不同的计数。

从原始查询中尝试:

SELECT DISTINCT Field1 
FROM MYTABLE 
WHERE rtrim(ltrim(cast(Field2 as varchar))) IN ('96','102')
这是我的工作

create table a (id int, val int)

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


create table b (id int)

insert into b values (22)
insert into b values (122)



select id
from a
where val in (select Id from b)
group by id
having count(id) = (select count(*) from b) 

似乎
(1,22),(1,22),…
输入会使其失效?错误的解决方案。。。但是sqlfiddle的想法是+1!感谢您的关注,但不幸的是,这不是我的解决方案。因为,如果只有(22)两行,它也算2:)谢谢你的兴趣,但幸运的是,这不是我想要的。不只是两个变量,如字段2的(96102),用户还可以为字段2选择三个变量,例如字段2的(96102104),在这种情况下,结果查询应该是671982作为字段1。感谢您的兴趣,但幸运的是,这不是我想要的。不仅字段2有两个变量(96102),用户还可以为字段2选择三个变量,例如字段2有三个变量(96102104),在这种情况下,结果查询应为字段1。根据你的查询,我必须写为:从T R1,T R2,T R3中选择*其中R1.F1=R2.F1和R1.F1=R3.F1和R1.F2=96,R2.F2=102和R3.F2=104???????里格斯?如果用户为字段2选择四个变量,我们将在from部分添加“TR4”和“R1.F1=R4.F1和R4.F2=XXX”,依此类推。。。有没有其他的解决办法?@Mustafa是的,它变得非常乏味:-)我添加了马克答案的一个变体来解决这个问题,以及清晰度问题。
SELECT DISTINCT Field1 
FROM MYTABLE 
WHERE rtrim(ltrim(cast(Field2 as varchar))) IN ('96','102')
create table a (id int, val int)

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


create table b (id int)

insert into b values (22)
insert into b values (122)



select id
from a
where val in (select Id from b)
group by id
having count(id) = (select count(*) from b)