How to find if item is first in a column in SQLite?

Richard Plester

If I have a table:

Person:  Action:   Location:
-------  --------  ---------
Alan     Pondered  Garden
Bob      Hesitated Stairs 
Carol    ACTED     Kitchen
Dave     ACTED     Kitchen
Eric     Slept     Kitchen
Alan     ACTED     Cellar
Bob      Slept     Cellar
Fred     ACTED     Cellar

..and want to find the first person who "ACTED" first in each room, how do I do this?

I'd like to do this within an existing query:

SELECT Person,
       Location,
       <CLEVER-CODE-HERE> as flg_First --BOOLEAN was first to ACT
FROM my_table
GROUP BY Person,Location
ORDER BY Person

Desired result would be:

Person: Location: flg_First:
------- --------- ----------
Alan    Cellar    TRUE
Alan    Garden    False
Bob     Cellar    False
Bob     Stairs    False
Carol   Kitchen   TRUE
Dave    Kitchen   False
Eric    Bedroom   False
Fred    Cellar    False

I'm hoping this maybe achievable (with a window function?) but how?

forpas

With NOT EXISTS:

select m.person, m.location,
  case 
    when m.action = 'ACTED' and not exists (
      select 1 from my_table
      where action = m.action and location = m.location and rowid < m.rowid
    ) then 'TRUE'
    else 'False'
  end flg_First 
from my_table m
order by m.person, m.location

See the demo.
You can do it also with ROW_NUMBER() with a composite sorting involving action and rowid:

select person, location,
  case 
    when action = 'ACTED' 
      and row_number() over (partition by location order by action <> 'ACTED', rowid) = 1 then 'TRUE'
    else 'False'
  end flg_First
from my_table
order by person, location

See the demo.
Results:

| Person | Location | flg_First |
| ------ | -------- | --------- |
| Alan   | Cellar   | TRUE      |
| Alan   | Garden   | False     |
| Bob    | Cellar   | False     |
| Bob    | Stairs   | False     |
| Carol  | Kitchen  | TRUE      |
| Dave   | Kitchen  | False     |
| Eric   | Kitchen  | False     |
| Fred   | Cellar   | False     |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find first occurrence for each id based on datetime column with pandas?

How to parse CSV file and search by item in first column

R dataframe: how to find item in one column but not another column (two column contain similar lists)

How to Find First Valid Row in SQL Based on Difference of Column Values

How to add control item to first column of datatable

How to find index of the first match in the column using Google sheets script?

How to find Duplicates - by referencing to the first column?

How to find the first consecutive value in a Pandas dataframe column and delete that row?

How to find the first value in a column larger than the previous value in the column

How to find the first row with min value of a column in dataframe

How to split the column by identifier (|) and get the first item and group by?

how to find first duplicate item in array?

How to find the first observation of a column that matches a condition

SQLite: Add column or rows first?

How to find first date after given date in a sorted column

How to find first specific item and then take 3 following?

Select first item of pivot column

Not able to find column in sqlite

How to find column position for first matching

How to find the column name of first empty cell in a row?

How do you delete a column of values in a csv file but not the first item?

How to find rows in a matrix with only the minimum element in the first column?

How to find row and column of an item in a Dataframe

LINQ: How to find most popular item from column

How to find column with first of value in R using dplyr?

Excel / LibreOffice Calc: find item in column with latest first occurrence?

How to find first and last value of a column in SQL?

How to find the absolute first match within a column in VBA using Find?

How to find the position of the first list item outside a container?