2011-01-27 95 views
0

我有一個遺留數據庫的問題,最初設置它的人留下了很少的文檔。如何篩選SQL中特定的一組值,並僅篩選特定集合?

我有一組用戶,每個用戶都有三個潛在的職業。查詢時,它返回值這樣的:

USER_ID CAREER_ID TITLE 
1  44   Agricultural Engineer 
1  136   Educational Psychologist 
1  132   Clinical Psychologist 
18  245   3D Designer 
18  2   Accountant - Private Practice 
18  1   Accountant - Industry and Commerce 
19  245   3D Designer 
19  2   Accountant - Private Practice 
19  1   Accountant - Industry and Commerce 
20  128   Advice Centre Worker 
20  130   Careers Adviser 
20  129   Care Assistant 
21  1   Accountant - Industry and Commerce 
21  245   3D Designer 
21  2   Accountant - Private Practice 
23  245   3D Designer 
23  2   Accountant - Private Practice 
23  1   Accountant - Industry and Commerce 
29  245   3D Designer 
29  2   Accountant - Private Practice 
29  1   Accountant - Industry and Commerce 
30  219   PC Games Tester 
30  173   Bouncer 
30  103   Stunt Person 
32  245   3D Designer 
27  2   Accountant - Private Practice 
27  1   Accountant - Industry and Commerce 
27  245   3D Designer 
30  219   PC Games Tester 
30  173   Bouncer 
30  103   Stunt Person 

正如你所看到的,由於某種原因,職業生涯1,2,和245被設置爲默認值。現在,我想過濾掉那些具有特定職業生涯的用戶,但不是所有用戶的實例,因爲任何用戶都可以合法地選擇其中的一個或兩個。

我可以過濾掉那些可能實際上已經真正選擇了特定設置的奇怪字符。

希望有人能幫忙。我確信解決方案很簡單,但我無法想出。

+0

以上查詢的表結構是什麼? – Dan 2011-01-27 14:58:40

+0

您需要的操作員被稱爲'關係部門'(「供應商提供所有部件的供應商編號」)。 SQL不直接實現這一點,你將需要推出自己的。許多常用的方法在這裏討論:http://www.simple-talk.com/sql/t-sql-programming/divided-we-stand-the-sql-of-relational-division/ – onedaywhen 2011-01-27 15:16:56

回答

1
Select ... 
From Occupations O 
Where Not Exists (
        Select 1 
        From Occupations O1 
        Where O1.Career_Id In(1,2,245) 
         And O1.User_Id = O.User_Id 
        Group By O1.User_Id 
        Having Count(*) = 3 
        ) 

另一種解決方案:

Select ... 
From Occupations O 
Where Exists (
       Select 1 
       From Occupations O1 
       Where O1.Career_Id Not In(1,2,245) 
        And O1.User_Id = O.User_Id 
       ) 

上述解決方案的結果是,它將排除只有少於三個默認職業的人。即,它將排除只有只有具有(1,2),(1,245),(2,245),(1),(2),(245)的用戶。如果他們必須有這三個,只有這三個,那麼您需要修改這個解決方案,像這樣:

Select ... 
From Occupations O 
Where Exists (
       Select 1 
       From Occupations O1 
       Where O1.Career_Id Not In(1,2,245) 
        And O1.User_Id = O.User_Id 
       ) 
Or Exists (
      Select 1 
      From Occupations O2 
      Where O2.User_Id = O.User_Id 
      Having Count(*) < 3 
      ) 
0

我想這應該找出誰擁有職業1,2和245的用戶中列出:

SELECT User_ID 
    FROM Occupations 
WHERE Career_ID IN (1, 2, 245) 
GROUP BY Career_ID 
HAVING COUNT(*) = 3 

如果你想沒有那一套的事業,那麼誰的用戶列表:

SELECT User_ID 
    FROM Occupations 
WHERE User_ID NOT IN 
     (SELECT User_ID 
      FROM Occupations 
     WHERE Career_ID IN (1, 2, 245) 
     GROUP BY Career_ID 
     HAVING COUNT(*) = 3) 
0

假設你有一個用戶表,職業表,映射表(user_career_map),那麼這將讓你在找什麼:

SELECT user_id, career_id, title 
FROM (
    SELECT distinct m.user_id, m.career_id, c.title 
    , sum(case when m.career_id in (1, 2, 245) then 1 else 0 end) over (partition by  m.user_id) filter 
    FROM user_career_map m inner join career c on m.career_id = c.career_id 
) WHERE filter <> 3 
ORDER BY user_id, career_id;