COUNT DISTINCT with CONDITIONS

前端 未结 5 663
南方客
南方客 2020-12-07 09:10

I want to count the number of distinct items in a column subject to a certain condition, for example if the table is like this:

tag | entryID
----+---------
         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 09:25

    Try the following statement:

    select  distinct A.[Tag],
         count(A.[Tag]) as TAG_COUNT,
         (SELECT count(*) FROM [TagTbl] AS B WHERE A.[Tag]=B.[Tag] AND B.[ID]>0)
         from [TagTbl] AS A GROUP BY A.[Tag]
    

    The first field will be the tag the second will be the whole count the third will be the positive ones count.

提交回复
热议问题