简体   繁体   中英

Using SQL in Domo to apply multiple groups or filters

I am trying to filter for unique UserIDs and then see which IDs have a value of >1 in another column. I have tried pretty much every example from SQL - Selecting unique values from one column then filtering based on another and at least 1 or two more sites but I cant find the appropriate links for that as it turned out to be a little unrelated anyways.

An example of the relevant columns of my dataset are as follows:

+---------+-----------+
| UserIDs | EventType |
+---------+-----------+ 
| 100     | Start     |
| 100     | Start     |
| 100     | Finish    |
| 100     | Finish    |
| 200     | Start     |
| 200     | Start     |
| 200     | Start     |
| 200     | Finish    |
| 200     | Finish    |
| 200     | Finish    |
| 300     | Start     |
| 400     | Start     |
| 400     | Finish    |
+---------+-----------+

What I am trying to figure out is how many users triggered EventType-Finish more than once. The data I would want from the example above would be:

+------------------------------------------------+
| Total # of students that battled more than once|
+------------------------------------------------+  
| 2                                              |
+------------------------------------------------+ *edited*

None of the Group By stuff seems right because it would just compress the other rows into each other?

For the record I am very new to SQL and programming in general so try not to be too technical in your answer. Anytime I get remotely close to thinking I have solved it, I run it and it gives me syntax errors so I have no idea where else to turn.

Sorry guys I wrote the output incorrectly, I am actually just looking for the number of students who triggered Finish more than once. Also, what would I use in place of a table name eg: FROM "TABLE" since Domo has a kind of strange way of breaking up the hierarchy. For context, my table is called "Metrics Data" so trying to type that into the table name generally converts data into SQL code.

Attempt at answer

Try this answer,

SELECT UserId,COUNT(1) [# of Finishes] 
FROM Your_Table
WHERE EventType='Finish'
GROUP BY UserID
HAVING COUNT(1)>1

Hope it helps.

I would like to do some conditional aggregation with help of case expressions

SELECT 
      UserIDs [UserID],
      SUM(CASE(EventType) WHEN 'Finish' THEN 1 ELSE 0 END) [# of Finishes]
FROM <table> GROUP BY UserIDs 
HAVING SUM(CASE(EventType) WHEN 'Finish' THEN 1 ELSE 0 END) > 1

Result :

UserID  # of Start   
100     2            
200     3            

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