想要根据第2列值计算第1列中的不同值

时间:2015-06-12 13:33:36

标签: sql sql-server tsql

以下是select语句的示例数据

banner_id   subject_code

N00012301   MATH

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的数量,其中包含'engl'和'math' 例如:

N00019312   MATH

N00019312   ENGL

N00019312   ENGL

是基于banner_id的1个值,尽管它有2个engl和1个数学

感谢您的帮助,

感谢

3 个答案:

答案 0 :(得分:3)

如果您想查看banner_ids,那么Using TPerlRegEx Regex: (?'word'\b[a-zA-Z]{3,}\b)|(?'id'\b\d{1,3}\b)|(?'course'\b[BL]\d{3}\b) Text: externship L763 clinic 207 B706 b512 'externship' with 1 groups: length 10 at 1, value 'externship' 'L763' with 3 groups: length 0 at 1, value '' length 0 at 1, value '' length 4 at 12, value 'L763' 'clinic' with 1 groups: length 6 at 17, value 'clinic' '207' with 2 groups: length 0 at 1, value '' length 3 at 24, value '207' 'B706' with 3 groups: length 0 at 1, value '' length 0 at 1, value '' length 4 at 28, value 'B706' banner_id同时存在banner_id的每个math

engl

答案 1 :(得分:1)

var finalArray = _.chain(myArray)
    .reject('ignore')
    .pluck('myProperty')
    .value();

答案 2 :(得分: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