2015-06-12 110 views
1

下面是select語句要根據列在第1列數不同的值2個值

banner_id subject_code 

N00MATH 

N00012963 ENGL 

N00012963 MATH 

N00013406 ENGL 

N00013406 ENGL 

N00013406 MATH 

N00013998 ENGL 

N00016217 MATH 

N00017367 MATH 

N00017367 ENGL 

N00017833 MATH 

N00018132 MATH 

N00019251 ENGL 

N00019251 ENGL 

N00019312 MATH 

N00019312 ENGL 


N00019312 ENGL 

N00020261 ENGL 

我想banner_id它既有「英格蘭」和「數學」 前的數我的樣本數據:

N00019312 MATH 

N00019312 ENGL 

N00019312 ENGL 

是基於banner_id 1個值,雖然它有2個英格蘭和1個數學

感謝您的幫助,

感謝

回答

1

試試這個:

SELECT banner_id AS [COUNT] 
FROM @t 
WHERE subject_code IN ('ENGL', 'MATH') 
GROUP BY banner_id 
HAVING COUNT(DISTINCT subject_code) > 1 

如果你只想算:

SELECT COUNT(*) 
FROM (SELECT banner_id AS [COUNT] 
      FROM  @t 
      WHERE  subject_code IN ('ENGL', 'MATH') 
      GROUP BY banner_id 
      HAVING COUNT(DISTINCT subject_code) > 1 
     ) t 
+0

感謝它幫了我很多。 – RAP

3

如果你想看到的banner_ids,這應該讓你的每個banner_id其中既mathengl存在banner_id

SELECT banner_id 
FROM YourTable 
WHERE subject_code IN ('engl', 'math') 
GROUP BY banner_id 
HAVING COUNT(DISTINCT subject_code) > 1 
+1

@ X.L.Ant你是對的。增加了不同。這應該照顧你指出的情況。 –

1
select count(*) 
from 
(
    SELECT banner_id 
    FROM Table 
    WHERE subject_code = 'engl' 
INTERSECT 
    SELECT banner_id 
    FROM Table 
    WHERE subject_code = 'math' 
) tt 
+0

這是完美的! – RAP

相關問題