Linked Questions
93 questions linked to/from Aggregate columns with additional (distinct) filters
1 vote
3 answers
1k views
Sum of two counts from one table with additional data from another table
I have two tables as follows: TABLE A | id | col_a | col_b | user_id | -------------------------------- | 1 | false | true | 1 | | 2 | false | true | 2 | | 3 | true | true | 2 ...
1 vote
4 answers
871 views
Get max value from a window of rows as new column for all rows
| col 1 | col 2 | col 3 | |-------|-------|-------| | 67458 | ADM | 1008 | | 67458 | ADM | 1009 | | 67458 | SKI | 1009 | | 67458 | LIS | 1010 | | 67458 | TOU | 1121 | How to get max ...
1 vote
2 answers
995 views
Get start and end date time based on sequence of rows
I have a table below with sample data. Date&Time Message 13/07/2022 8:59:09 Normal 13/07/2022 8:59:10 Normal 13/07/2022 8:59:11 Normal 13/07/2022 8:59:12 Warning 13/07/2022 8:59:13 Warning 13/07/...
2 votes
3 answers
264 views
Grouping based on sequence of rows
I have a table of orders with a column denoting whether it's a buy or a sell, with the rows typically ordered by timestamp. What I'd like to do is operate on groups of consecutive buys, plus their ...
1 vote
1 answer
1k views
How to copy the previous row value of the same column?
I have several tracked specimens that can move between different areas throughout the day (and its periods). In the example below the calculated field mov has to restart counting whenever there is a ...
2 votes
1 answer
1k views
Aggregate the same column in multiple different ways
I am trying to get an array of categories associated with each product and then also get the top-level parent category of each product in another column, which by my logic is finding the same values ...
0 votes
1 answer
728 views
Get total count and filtered count from single subquery
product table product_id name 1 t-shirt 2 shirt users table user_id name 1 sphinx favorite table with a UNIQUE constraint on (user_id, product_id): user_id product_id 1 2 What is best way to query ...
0 votes
1 answer
924 views
How to get a date interval with condition
How to get a continuous date interval from rows fulfilling specific condition? I have a table of employees states with 2 types of user_position. The interval is continuous if the next higher ...
1 vote
1 answer
688 views
Create view with aggregated columns from three levels of nested tables
I'm using Postgres 9.4 and I have 3 tables: truck , container and container_metadata. A truck can have many container and a container many container_metadata. I'll add more description later with the ...
1 vote
2 answers
587 views
How to improve SQL query performance containing partially common subqueries
I have a simple table tableA in PostgreSQL 13 that contains a time series of event counts. In stylized form it looks something like this: event_count sys_timestamp 100 167877672772 ...
4 votes
3 answers
309 views
Select people with latest balance for one credit card being greater than for another
In a PostgreSQL 9.5.3 DB, I have a credit_card_balances table referencing a persons table which tracks the balances of various credit cards associated to a particular person: CREATE TABLE persons ( ...
1 vote
1 answer
627 views
Sustract two queries from same table
Let's say I have 2 tables: table device is a set of measuring devices, table data is a set of values of different types measured by the devices. Table data = (no, id,value_type,value, dayhour) no is ...
1 vote
2 answers
582 views
Find all rows in between a set in PostgreSQL
I have a table named tc_fuel that is receiving all the fuel related data from GPS vehicles, and I want to get the last "Tank Usage" to calculate MPG for a whole tank, but the readings when the tank is ...
0 votes
1 answer
505 views
Count devices per day in a given date range
I have a table which has devices with 3 statuses, Pass, Fail and Warning. Device Status Date Device1 Pass 12/1/2020 Device2 Fail 12/1/2020 Device3 Warning 12/1/2020 Device1 Fail 12/2/2020 Device2 ...
0 votes
1 answer
509 views
Get conditional count and conditional DISTINCT count in a single SELECT
I have 3 tables: TABLE: session_log user_id | device | logged_on ---------|--------|--------------------- 1 | web | 2022-01-01 12:43:25 1 | web | 2022-01-01 13:33:32 2 | ...