2014-08-29 177 views
-1

我需要一些幫助,我正在處理SQL查詢。這是我正在使用的數據的簡化版本。我有3個表:複雜的SQL服務器查詢

聯繫

- ContactID 
- ContactName 

提交

- SubmissionID 
- ContactID 
- SubmissionTypeID 

SubmissionTypes

- SubmissionTypeID 
- SubmissionType 

我需要返回所有聯繫人(加入Submissions,ContactID),其中SubmissionTypeIDsSubmissionTypeIDs列表匹配。棘手的部分是,我只想要結果,其中聯繫人的提交記錄SubmissionTypeID匹配列表中的每個值。所以,舉例來說,如果我有這樣的數據:

Contacts 
---------------- 
1 | Jim Johnson 
2 | Sally Anderson 

SubmissionTypes 
---------------------- 
1 | Contact Form 
2 | Request Form 
3 | Generic Form 

Submissions 
---------------------- 
1 | 1 | 1 
2 | 1 | 2 
3 | 2 | 1 

如果我SubmissionTypeID值是1和2,我想爲下面的結果:

Jim Johnson | Contact Form 
Jim Johnson | Request Form 

我不想看到莎莉安德森,因爲她沒有提交這兩個值的記錄。

我猜測有幾種方法可以做到這一點。我很高興看到你的想法。

謝謝!

回答

1

一種方法是用INTERSECT:

select c.contactname, t.submissiontype 
    from contacts c 
    join submissions s 
    on c.contactid = s.contactid 
    join submissiontypes t 
    on s.submissiontypeid = t.submissiontypeid 
    join (select c.contactid 
      from contacts c 
      join submissions s 
      on c.contactid = s.contactid 
     where s.submissiontypeid = 1 
     intersect 
     select c.contactid 
      from contacts c 
      join submissions s 
      on c.contactid = s.contactid 
     where s.submissiontypeid = 2) v 
    on c.contactid = v.contactid 
where s.submissiontypeid in (1, 2) 

小提琴:http://sqlfiddle.com/#!6/9ee4e/2/0

可以,也算哪裏等於2(你有2個值,你檢查):

select c.contactname, t.submissiontype 
    from contacts c 
    join submissions s 
    on c.contactid = s.contactid 
    join submissiontypes t 
    on s.submissiontypeid = t.submissiontypeid 
    join (select c.contactid 
      from contacts c 
      join submissions s 
      on c.contactid = s.contactid 
     where s.submissiontypeid in (1, 2) 
     group by c.contactid 
     having count(distinct s.submissiontypeid) = 2) v 
    on c.contactid = v.contactid 
where s.submissiontypeid in (1, 2) 

小提琴:http://sqlfiddle.com/#!6/9ee4e/1/0

+0

取決於其它約束,'具有COUNT(*)= 2'可能需要是'具有計數(不同submissiontypeid)= 2'。不過,對'intersect'+1。 – Laurence 2014-08-29 21:56:21

+0

@Laurence同意,通常我會默認沒有明顯的,但我只是編輯它,因爲聯繫人可能有不止一次提交相同類型的提交。 – 2014-08-29 22:02:29

1

這是一個使用雙重否定的複雜方法。

declare @list table (SubmissionTypeID int not null primary key); 

insert into @list values (1), (2); -- values to search for. 

with c as (
    select 
     c.ContactID, 
     c.ContactName 
    from 
     Contacts c 
    where 
     not exists (
      select 
       'x' 
      from 
       @list l 
      where 
       not exists (
        select 
         'x' 
        from 
         Submissions s 
        where 
         s.ContactID = c.ContactID and 
         s.SubmissionTypeID = l.SubmissionTypeID 
       ) 
     ) 
) 
select 
    c.ContactName, 
    t.SubmissionType 
from 
    c 
     inner join 
    Submissions s 
     on c.ContactId = s.ContactId 
     inner join 
    SubmissionTypes t 
     on s.SubmissionTypeID = t.SubmissionTypeID 
     inner join 
    @list l 
     on t.SubmissionTypeID = l.SubmissionTypeID; 

Example SQLFiddle

0
Try this.. It works fine to me 

DECLARE @list TABLE (SubmissionTypeID int not null primary key); 

INSERT INTO @list VALUES(1); -- values to search for. 

SELECT C.ContactName, ST.SubmissionTypeName 
FROM 
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY S.ContactID ORDER BY S.ContactID) AS RCount 
FROM 
Submission S 
WHERE EXISTS 
(SELECT 1 FROM @list l WHERE l.SubmissionTypeID = S.SubmissionTypeID)) AS Result 
INNER JOIN Submission S1 ON S1.ContactID = Result.ContactID 
INNER JOIN Contacts C ON C.ContactID = Result.ContactID 
INNER JOIN SubmissionTypes ST ON ST.SubmissionTypeID = S1.SubmissionTypeID 
WHERE RCOunt = (SELECT COUNT(DISTINCT SubmissionTypeID) FROM @list) 
AND EXISTS 
(SELECT 1 FROM @list l WHERE l.SubmissionTypeID = ST.SubmissionTypeID) 
;