简体   繁体   中英

Query to count the number record contain text

I am trying to count the number records based on the text in the table

Am having Table Structure Like this

SN_ID           NUMBER                      
PERSON_ID       NUMBER      
NOTICE_TYPE     VARCHAR2    

and the contents of the table like this

    SN_ID  PERSON_ID   NOTICE_TYPE
  -------+-----------+--------------
    1      5           Appreciation
    2      5           Warning
    3      1           Warning
    4      5           Incident
    5      2           Warning
    6      5           Warning

I want to count the number Appreciation, Warning and Incident records for the person with an Id = 5

select Notice_type, count(*) from [Table] 
where person_id=5
group by notice_type
SELECT NOTICE_TYPE, count(SN_ID) 
FROM [Table] 
WHERE PERSON_ID = 5
GROUP BY NOTICE_TYPE

This is slightly different from MikkaRin answer.

Difference is count(SN_ID) . I took only one column here. Because it is more optimized method than taking whole column into the count() function. This will affected to large queries.

ps actually we should get the primary key into the count() function. Here SN_ID look like the PK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM