How can I extract the number of occurences where a value occurs as the MAX?

Thissiteusescookies

I have a summary table as below

user_id     service     no_of_trx
1           A           56
1           C           43
1           B           22
2           C           10
2           A           3
3           B           45
3           C           7
4           A           77
4           B           63

It summarizes all the different types of services a user_id has used, sorted by the number of transactions they make per service. How do I extract How many times each service appears as the top service? Expected results

service     occurrence_as_max
A           2
B           1
C           1

Because service A is the top service for users 1 and 4, and services B and C are top services for users 3 and 2 respectively.

What I have so far:

WITH a as

(SELECT user_id, service, count(service) no_of_trx
FROM transactions
GROUP BY user_id, service
ORDER BY no_of_trx desc),

b as
(SELECT distinct(user_id) user, max(no_of_trx) occurrence_as_max
FROM a
GROUP BY user_id
ORDER by user)


SELECT distinct(service), b.occurrence_as_max
FROM b
LEFT JOIN a ON a.user_id=b.user.
ORDER by b.occurrence_as_max desc;

But this clearly will not work.

mkRabbani

This following script should work. This is standard query syntax. You may required some adjustment in BigQuery but the logic should be OK.

SELECT A.service, COUNT(*)
FROM your_table A
INNER JOIN 
(
    SELECT user_id, MAX(no_of_trx) no_of_trx
    FROM your_table
    GROUP BY user_id
)B ON A.user_id = B.user_id 
AND A.no_of_trx = B.no_of_trx
GROUP BY A.service

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I count the number of occurences of a specific value in a group?

How to extract different occurences of a character in a string ? (Max consecutive, last consecutive, number of blocks)

How do I extract the first number that occurs after a matching pattern

extract number of occurences of `!?` in a string

How can I count the number of instances a value occurs within a subgroup in R?

How can I count the number of columns a value has after a Where?

Counting the max number of times a value occurs consecutively

Java: number of times that the max value occurs in the array

How do I count number of occurences in Django?

How do I print the number of occurences

How do I get the rows where a certain value occurs?

Performing a COUNT on the MAX number of occurences

How can i print number of times each alphabet occurs in the string?

How can I count the number of times a byte sequence occurs in a file?

How can I set up logic to count the number of occurences in a month for a list of dates?

How can I count the number of integer occurences from a txt file from certain columns in Java?

R: how can i make str_count look for the number of occurences of zero between two specific numbers?

How can I print number of occurences of an array from an array list in Java?

How can I store the occurences of a number in a sudoku subgrid (3x3)?

How can I extract the places values of a number?

How can I extract the number from this BeautifulSoup?

How can I get a count of times a value occurs by date in mysql

In a pandas column, how to find the max number of consecutive rows that a particular value occurs?

How can I get the max value from one column where values match another column?

How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?

How can I calculate min or max value under some cases, where the some information are coming from vector?

How to get a row number or ID of where the MAX() value was found

How can I use JavaScript to limit a number between a min/max value?

Z3 SMT Solver - How can I extract the value of a floating point number in FPA?