Linked Questions
93 questions linked to/from Aggregate columns with additional (distinct) filters
77 votes
2 answers
85k views
Conditional SQL count
What is the best way to create columns which count the number of occurrences of data in a table? The table needs to be grouped by one column? My database is PostgreSQL. I have seen: SELECT sum(...
26 votes
3 answers
81k views
error : subquery must return only one column
I am getting the error subquery must return only one column when I try to run the following query: SELECT mat.mat as mat1, sum(stx.total) as sumtotal1, ( SELECT mat.mat as mat, sum(stx.total) ...
11 votes
4 answers
16k views
Select a row of first non-null values in a sparse table
Using the following table: A | B | C | ts --+------+------+------------------ 1 | null | null | 2016-06-15 10:00 4 | null | null | 2016-06-15 11:00 4 | 9 | null | 2016-06-15 12:00 5 | 1 |...
7 votes
2 answers
9k views
Sum by month and put months as columns
Background I have time series data on a monthly basis and I would like to sum values for each ID, grouped by month and then have the month names as columns rather than as rows. Example +----+-------...
9 votes
3 answers
23k views
Conditional lead/lag function PostgreSQL?
I have a table like this: Name activity time user1 A1 12:00 user1 E3 12:01 user1 A2 12:02 user2 A1 10:05 user2 A2 10:06 user2 A3 10:07 user2 M6 ...
5 votes
3 answers
12k views
Pivot table without crosstab/tablefunc
I have a table like this INPUT id author size file_ext -------------------------------- 1 a 13661 python 1 a 13513 cpp 1 a 1211 non-code 2 b ...
6 votes
3 answers
8k views
Scope of a column alias in a SELECT with GROUP BY
I have been asked to solve this question: Say that a big city is defined as a place of type city with a population of at least 100,000. Write an SQL query that returns the scheme (state_name,...
1 vote
1 answer
5k views
Optimize slow query when multiple JOIN LATERAL are present
This is a follow-up optimization on Adding a SUM group by into a COUNT DISTINCT query (albeit with some optimizations and joins simplifications). I wonder if it's possible to optimize the following ...
5 votes
3 answers
1k views
How to form groups of consecutive dates allowing for a given maximum gap?
Given a table like: person_id contact_day days_last_contact dash_group 1 2015-02-09 1 1 2015-05-01 81 2 1 2015-05-02 1 2 1 2015-05-03 1 2 1 2015-06-01 29 3 1 2015-08-01 61 4 1 2015-08-04 3 4 1 2015-...
4 votes
2 answers
3k views
Postgres COUNT number of column values with INNER JOIN
I am creating a report in Postgres 9.3. This is my SQL Fiddle. Basically I have two tables, responses and questions, the structure is: responses ->id ->question_id ->response questions ->...
2 votes
2 answers
3k views
Optimizing SQL query with multiple joins and grouping (Postgres 9.3)
I've browsed around some other posts and managed to make my queries run a bit faster. However, I've come to a loss as to how to further optimize this query. I'm going to be using it on a website where ...
2 votes
2 answers
1k views
Convert MySQL view to Postgres
I've inherited the need to convert a production MySQL DB over to Postgres. This has mostly been handled without issue using simple SQL statements for table/function creation (using Navicat to generate ...
0 votes
2 answers
6k views
Convert numeric result to percentage with decimal digits
I have this query: Select count(incidentnumber) as average from incidents Where IncidentStationGround <> firstpumparriving_deployedfromstation; I got a result, it's something like 20,000. But ...
1 vote
2 answers
1k views
Select most recent non-null value for multiple columns
I've been stumped trying to optimize this query and was hoping some of you database wizards might have some insight. Here is the setup. Using TimescaleDB as my database, I have a wide table containing ...
1 vote
3 answers
4k views
Postgres JSONB select json objects as columns
The table has an id and step_data columns. id | step_data -------------- a1 | {...} a2 | {...} a3 | {...} Where step_data is nested structure as follows, where the actual keys of the metadata can be ...