Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether value exists in column for each group

Tags:

sql

postgresql

Having a difficult time putting what I am trying to do into words so searching is also difficult.

Basically I am trying to look whether a certain value exists in a column, partitioned by group, and then propagate that value forward.

In this example I want to check whether a user has completed the tutorial and set a flag that carries forward.

pk | user | ... | activity
 1 |    A | ... |  "login"
 2 |    A | ... |  "started_tutorial"
 3 |    A | ... |  "completed_tutorial"
 4 |    A | ... |  "some other activity"
 5 |    A | ... |  "logout"
 5 |    B | ... |  "login"
 6 |    B | ... |  "logout"

I think this should be something like

select *,
    check(activity in ('completed_tutorial')) as completed_activity
    from tbl

but I don't think I can use check in a select statement and this would be a constant flag rather than set to true only after it has been found.

Example of what I am trying to get:

pk | user | ... | activity               | completed_tutorial
 1 |    A | ... |  "login"               |                 0
 2 |    A | ... |  "started_tutorial"    |                 0
 3 |    A | ... |  "completed_tutorial"  |                 1
 4 |    A | ... |  "some other activity" |                 1
 5 |    A | ... |  "logout"              |                 1
 5 |    B | ... |  "login"               |                 0
 6 |    B | ... |  "logout"              |                 0
like image 579
Ellis Valentiner Avatar asked Jan 30 '15 16:01

Ellis Valentiner


People also ask

How do you check if value in one column exists in another column in SQL?

Example using VLOOKUP You can check if the values in column A exist in column B using VLOOKUP. Select cell C2 by clicking on it. Insert the formula in “=IF(ISERROR(VLOOKUP(A2,$B$2:$B$1001,1,FALSE)),FALSE,TRUE)” the formula bar. Press Enter to assign the formula to C2.

How do you check if a column exists in multiple tables?

(i) Using INFORMATION_SCHEMA. The easiest and straightforward way to check for the column in a table is to use the information schema for column system view. Wright a select query for INFORMATION_SCHEMA. COLUMNS as shown below. If the query returns record, then the column is available in the table.

How do you check if a value already exists in a SQL table?

The SQL EXISTS Operator The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.


1 Answers

You can filter SQL groups with the HAVING clause. For example, you can group your table by users and their activity, and then filter it to contain only those that have completed the tutorial:

SELECT user FROM tbl
GROUP BY user, activity
HAVING activity = 'completed_tutorial';

EDIT: After OP has edited their question, this is my new answer. Here, I assume that your table has a date field.

SELECT *, COALESCE(date >= (
    SELECT date FROM tbl WHERE activity = 'completed_tutorial'
    AND user = outertbl.user
), FALSE)
FROM tbl AS outertbl
ORDER BY date

Notice, that such query is essentially N² when unoptimised, so I would recommend instead to just get the data from the database and then process it in your program.

like image 78
Ainar-G Avatar answered Oct 29 '22 23:10

Ainar-G