SELECT DESCR254, COUNT(*) AS COUNT
FROM sysadm.PS_IS_STATS_URLS
GROUP BY DESCR254
ORDER BY COUNT(*)
SQL Query to Find Most Popular Instances of Data
Pregunta
I am trying to build a query that will sort occurrences of the field DESCR254. So for example, test1 is used by 3 different USERIDs and test2 would be the second most. Does anyone know how I would construct this query given there are many more rows of data and many different instances of USERID and DESCR254?
select * from sysadm.PS_IS_STATS_URLS
PS_IS_STATS_URLS
USERID DESCR254
ZX test1
ZX test2
ZC test1
ZB test3
ZA test1
ZA test2
I would want the result to look something like this:
DESCR254 COUNT
test1 3
test2 2
test3 1
Solución
Otros consejos
To count only accesses from different userids as your question suggest, you need to use COUNT DISTINCT
;
SELECT DESCR254, COUNT(DISTINCT USERID) "COUNT"
FROM PS_IS_STATS_URL
GROUP BY DESCR254
ORDER BY "COUNT" DESC;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow