instance_id
int64 0
199
| selected_database
stringclasses 12
values | query
stringlengths 92
2.94k
| error_sql
sequencelengths 1
4
| sol_sql
sequencelengths 0
0
| preprocess_sql
sequencelengths 0
10
| clean_up_sql
sequencelengths 0
5
| test_cases
sequencelengths 0
0
| external_data
stringclasses 6
values | efficiency
bool 1
class |
---|---|---|---|---|---|---|---|---|---|
100 | superhero | In the superhero database, is it possible to return a value from a DELETE statement in PostgreSQL when no rows were deleted? For example, if we attempt to delete a superhero with an ID that does not exist, we want to return a default value indicating that no rows were deleted. We tried using the RETURNING clause with a constant value, but it returned NULL instead of the desired default value. | [
"delete from superhero where id = 999 returning 1"
] | [] | [] | [] | [] | null | null |
101 | superhero | A data analyst is working with the superhero database and needs to perform a forward fill operation on the 'height_cm' and 'weight_kg' columns of the 'superhero' table. The analyst wants to create a PL/pgSQL function that mimics the behavior of the pandas 'ffill' function, which fills missing values with the last known non-null value. Here we use a table example(row_num int, id int, str text, val int) to test the functionality. We need to get the result of ffill(column val). The analyst has attempted to create a function but encountered syntax errors and incorrect results. The analyst needs a function that can handle forward filling for any specified column in the 'superhero' table, ordered by the 'id' column and partitioned by the 'publisher_id'. | [
"CREATE OR REPLACE FUNCTION GapFillInternal(s anyelement, v anyelement) RETURNS anyelement AS $$ DECLARE temp alias for $0; BEGIN RAISE NOTICE 's= %, v= %', s, v; IF v IS NULL AND s NOTNULL THEN temp := s; ELSIF s IS NULL AND v NOTNULL THEN temp := v; ELSIF s NOTNULL AND v NOTNULL THEN temp := v; ELSE temp := NULL; END IF; RAISE NOTICE 'temp= %', temp; RETURN temp; END; $$ LANGUAGE PLPGSQL;",
"CREATE AGGREGATE GapFill(anyelement) (SFUNC=GapFillInternal, STYPE=anyelement);",
"SELECT id, str, val, GapFill(val) OVER (ORDER BY id) AS valx FROM example;"
] | [] | [
"CREATE TEMPORARY TABLE example(id int, str text, val integer);",
"INSERT INTO example VALUES (1, 'a', NULL), (1, NULL, 1), (2, 'b', 2), (2, NULL, NULL)"
] | [
"DROP TABLE IF EXISTS example;",
"DROP FUNCTION IF EXISTS GapFillInternal(anyelement, anyelement);",
"DROP AGGREGATE IF EXISTS GapFill(anyelement);"
] | [] | null | null |
102 | california_schools | In the context of the 'california_schools' database, we need to update the 'table_A' table to deactivate certain items based on their associated records in table 'table_B'. Specifically, we want to find all items in table_A whose associated record in table_B has the 'deleted' set to 'true'. From this set of results, we need to get the 'parent_id' of these items. Then, for any item in the 'table_A' table whose 'id' is part of the 'parent_id' column from the previous result set, we need to check if their 'is_active' is 'true' and if so, make it 'false'. This operation is intended to deactivate items that are part of deleted status but the query gets stuck loading endlessly. | [
"UPDATE table_A A SET is_active = false FROM table_A WHERE A.parent_id IS NULL AND A.is_active = true AND A.id = ANY (SELECT (B.parent_id) FROM table_A B INNER JOIN table_B ON table_A.foreign_id = table_B.id WHERE table_B.deleted = true) RETURNING *;"
] | [] | [
"create table table_B (id int primary key, deleted boolean);",
"create table table_A (id serial primary key, parent_id int, is_active boolean default true, foreign_id int, foreign key (foreign_id) references table_B(id));",
"insert into table_B (id, deleted) values (1, false), (2, true), (5, true), (3, false), (4, false)",
"insert into table_A (parent_id, foreign_id) values (null, 1), (1, 2), (1, 5), (null, 3), (3, 4)"
] | [
"DROP TABLE table_A;",
"DROP TABLE table_B;"
] | [] | null | true |
103 | toxicology | We are analyzing the sales data of a chemical supply company stored in the 'transactions' table. The columns are id (customer id), amount (amount spent by customer), and timestamp (time of purchase). Assume that today is '2022-01-27'. We need to query:\n- Yesterday's revenue: sum of amount.\n- Percent difference from 8 days ago's revenue to yesterday's revenue.\n- Month-to-date (MTD) revenue.\n- Percent difference from last month's MTD to this month's MTD.\nWhen calculating the percentage, round the result to two decimal places.
Given the sample data:\n| id | amount | timestamp |\n| -- | ------ | -------- |\n| 1 | 50 | 2021-12-01|\n| 2 | 60 | 2021-12-02|\n| 3 | 70 | 2021-11-05|\n| 4 | 80 | 2022-01-26|\n| 5 | 90 | 2022-01-25|\n| 6 | 20 | 2022-01-26|\n| 7 | 80 | 2022-01-19|\nThe expected output is:\n| yesterday_revenue | pct_change_week_ago | mtd | pct_change_month_prior|\n| -------- | -------------- | --- | --- |\n| 100 | 0.25 | 270 | 0.50|\nHowever, the user's query resulted in incorrect percent change columns. Here is the problematic SQL statement: | [
"SELECT SUM(CASE WHEN timestamp::date = '2022-01-27'::date - 1 THEN amount ELSE NULL END) AS yesterday_revenue, ROUND((SUM(CASE WHEN timestamp::date > '2022-01-27'::date - 1 THEN amount ELSE NULL END) - SUM(CASE WHEN timestamp::date = '2022-01-27'::date - 8 THEN amount ELSE NULL END)) / SUM(CASE WHEN timestamp::date = '2022-01-27'::date - 8 THEN amount ELSE NULL END), 2) AS pct_change_week_ago, SUM(CASE WHEN date_trunc('month', timestamp) = date_trunc('month', '2022-01-27'::date - 1) THEN amount ELSE NULL END) AS mtd, ROUND((SUM(CASE WHEN date_trunc('month', timestamp) = date_trunc('month', '2022-01-27'::date - 1) THEN amount ELSE NULL END) - SUM(CASE WHEN date_trunc('month', timestamp) = date_trunc('month', '2022-01-27'::date - 1) - interval '1 month' AND date_part('day', timestamp) <= date_part('day', '2022-01-27'::date - 1) THEN amount ELSE NULL END)) / SUM(CASE WHEN date_trunc('month', timestamp) = date_trunc('month', '2022-01-27'::date - 1) - interval '1 month' AND date_part('day', timestamp) <= date_part('day', '2022-01-27'::date - 1) THEN amount ELSE NULL END), 2) AS pct_change_month_prior FROM transactions;"
] | [] | [
"CREATE TABLE transactions (id int, amount numeric, timestamp date);",
"INSERT INTO transactions (id, amount, timestamp) VALUES (1, 50, '2021-12-01'), (2, 60, '2021-12-02'), (3, 70, '2021-11-05'), (4, 80, '2022-01-26'), (5, 90, '2022-01-25'), (6, 20, '2022-01-26'), (7, 80, '2022-01-19');"
] | [
"DROP TABLE transactions;"
] | [] | null | null |
104 | card_games | I am analyzing the average converted mana cost of cards over a rolling window of 8 previous cards for each card in the 'cards' table. I need to round the nine_day_avg to two decimal places. However, I am having trouble placing the ROUND function correctly in the query. The query below does not produce the desired result. Can you help me correct it? | [
"SELECT name, convertedManaCost, avg(convertedManaCost) OVER(ORDER BY id ROWS BETWEEN 8 PRECEDING AND CURRENT ROW) AS nine_card_avg FROM cards WHERE name LIKE 'A%' ORDER BY id DESC"
] | [] | [] | [] | [] | null | null |
105 | superhero | In the superhero database, each superhero has a set of attributes stored in a B column named 'attributes' within the 'hero_attribute' table. Each attribute object contains an 'ss_id' and an 'approved' status indicating whether the attribute is officially recognized by the superhero community. For example, a single record might look like this:\n{\ | [
"SELECT hero_id, attribute_id, jsonb_array_length(a.ss) AS ss_cnt, jsonb_array_length(CASE WHEN a.ss -> 'approved' = 'true' THEN a.ss END) AS approved_cnt FROM hero_attribute a WHERE a.hero_id IN (1, 2);"
] | [] | [
"ALTER TABLE hero_attribute ADD COLUMN ss JSONB;",
"UPDATE hero_attribute SET ss = '[{\"ss_id\": 1, \"approved\": true}, {\"ss_id\": 2, \"approved\": false}]' WHERE hero_id = 1;",
"UPDATE hero_attribute SET ss = '[{\"ss_id\": 1, \"approved\": true}, {\"ss_id\": 2, \"approved\": true}]' WHERE hero_id = 2;"
] | [
"ALTER TABLE hero_attribute DROP COLUMN ss;"
] | [] | null | null |
106 | financial | I am trying to speed up a PostgreSQL query to find previous transactions on the same day of the year from the 'trans' table. My current query is as follows:\nsql \nselect * from trans \nwhere date_part('month', date) = date_part('month', now()) \nand date_part('day', date) = date_part('day', now()) \norder by date desc; \n\nThis query works but is running much slower than desired. Is there a better approach for comparing the current month and day?\nThe data is time-series in nature, and I am using PostgreSQL as the database. | [
"select * from trans where date_part('month', \"date\") = date_part('month', now()) and date_part('day', \"date\") = date_part('day', now()) order by \"date\" desc;"
] | [] | [
"CREATE INDEX ix1 ON trans (EXTRACT(MONTH FROM date), EXTRACT(DAY FROM date));"
] | [
"drop index if exists ix1;"
] | [] | null | true |
107 | financial | Is there an efficient way to aggregate data from a jsonb column in postgresql? Given the table userdata(id STRING, data JSONB) and the data in the table named "test".As you notice, I want unique values of keys (for e.g. "dis") across all records indexed by the id.I tried getting values using jsonb_agg and jsonb_array_elements. I could not aggregate all keys and distinct values. I couldn't figure out how to use jsonb_each to get all keys. What I tried is something like this to get one key. Any help with the query is appreciated. | [
"\nselect id,\n (select jsonb_agg(t->>'dis') from jsonb_array_elements(data::jsonb) as x(t) where t->>'dis' is not null) as sdata\nfrom test where id='123.abc'\n"
] | [] | [
"\nCREATE TABLE test (\n id TEXT NOT NULL,\n data JSONB NOT NULL\n);\nINSERT INTO test (id, data)\nVALUES\n ('123.abc', '{\"dis\": [\"close\"]}'),\n ('123.abc', '{\"purpose\": {\"score\": 0.1, \"text\": \"hi\"}, \"dis\": [\"hello\", \"close\"]}'),\n ('123.abc', '{\"dis\": [\"bye\"], \"dir\": 1}'),\n ('123.abc', '{}'),\n ('567.bhy', '{\"dis\": [\"close\"]}');\n"
] | [
"DROP TABLE test;"
] | [] | ('123.abc', '{"dis": ["close"]}'), ('123.abc', '{"purpose": {"score": 0.1, "text": "hi"}, "dis": ["hello", "close"]}'), ('123.abc', '{"dis": ["bye"], "dir": 1}'), ('123.abc', '{}'), ('567.bhy', '{"dis": ["close"]}') | null |
108 | european_football_2 |
In the context of managing a football database, I am trying to update the 'overall_rating' of players based on their 'player_api_id' and the 'date' of their attributes. I have arrays of 'player_api_id's, 'date's, and 'overall_rating's that I want to use to update the 'Player_Attributes' table. My initial approach was to use a loop to iterate through the arrays and update each player's 'overall_rating' individually, but this method incorrectly updates the 'overall_rating' to the last value in the array for all selected players. To fix this issue, I need to ensure that each 'overall_rating' is correctly matched with the corresponding 'player_api_id' and 'date'. One solution is to use the 'unnest' function in SQL to pair the arrays together and update each player's 'overall_rating' individually. Here's the corrected SQL query I plan to use:
| [
"DO $$ DECLARE i integer; BEGIN \n FOREACH i IN ARRAY[11, 20]::integer[] LOOP \n RAISE NOTICE 'Value: %', i; \n UPDATE Player_Attributes SET overall_rating = i \n WHERE player_api_id = ANY (ARRAY[505942, 155782]::integer[]) \n AND date = ANY (ARRAY['2016-02-18 00:00:00', '2015-10-16 00:00:00']::text[]);\n END LOOP; \n END $$;"
] | [] | [] | [] | [] | null | null |
109 | formula_1 | In the context of the Formula 1 database, I am trying to perform a full-text search on a specific B field within the 'results' table. The B field contains race result details, and I am particularly interested in searching within the 'fastestLapTime' attribute. My initial attempt to perform this search using the `to_tsvector` and `to_tsquery` functions did not yield the expected results. Here is the problematic SQL statement I used: | [
"\nSELECT resultId FROM results WHERE to_tsvector(results.fastestLapTime) @@ to_tsquery('1:35.405');\n"
] | [] | [
""
] | [
""
] | [] | null | null |
110 | formula_1 | In the context of Formula 1 racing data, I have a table that records the results of races, including the race ID, driver ID, constructor ID, and the position each driver finished in. I want to count the occurrences of each finishing position by race and insert the number of occurrences into another table as a B object. The positions are not limited to a predefined set and can vary widely. Here's an example of what I'm trying to achieve: For each race, I want to count how many drivers finished in each position and store this information in a B column, where the key is the position and the value is the count of drivers who finished in that position. | [
"\nSELECT raceId, position, COUNT(*) as cnt FROM results GROUP BY raceId, position\n"
] | [] | [
""
] | [
""
] | [] | null | null |
111 | european_football_2 | In the context of the european_football_2 database, I am trying to understand 'keyset pagination' using the `Match` table which includes `id`, `date`, and `match_api_id` columns. My base query for the first page works perfectly, but I have a few use-cases which I don't understand how does it work if I want to order by `match_api_id DESC`. | [
"\nSELECT * FROM Match WHERE (match_api_id, date, id) > (492473, '2008-08-17 00:00:00', 1) ORDER BY date, id ASC LIMIT 3\n"
] | [] | [
""
] | [
""
] | [] | null | null |
112 | california_schools | In the context of analyzing school data within the 'california_schools' database, I need to identify all schools that are located in both Alameda and Contra Costa counties. This is similar to finding products whose companies include both A and B in the original problem. I attempted to write a query that would return schools located in more than one county, but I'm struggling to refine this query to specifically include only those schools that are present in both Alameda and Contra Costa counties. | [
"\nSELECT School FROM schools GROUP BY School HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC;\n"
] | [] | [
""
] | [
""
] | [] | null | null |
113 | formula_1 |
I'm new to SQL & I'm trying to get the raceid and name for each name with the latest date.
| [
"SELECT MAX(date), raceid, name FROM races GROUP BY name, raceid HAVING MAX(date) = date;"
] | [] | [] | [] | [] | null | null |
114 | european_football_2 |
I need to generate a report that lists all possible combinations of match outcomes (win, lose, draw) for each team in the 'european_football_2' database. I tried the following query but it does not work as expected; it only returns a limited set of combinations instead of all possible combinations for each team.
| [
"SELECT t.team_name, o.outcome FROM (VALUES('Team A'),('Team B')) AS t(team_name) JOIN (VALUES('win'),('lose'),('draw')) AS o(outcome);"
] | [] | [] | [] | [] | null | null |
115 | california_schools |
I am working with the california_schools database and need to analyze the frpm table to find the second highest enrollment (K-12) for each County Code, ignoring rows with NULL values in the County Code and Enrollment (K-12) fields. The goal is: Partition the data by County Code. Within each partition, sort the records by Enrollment (K-12) in descending order, with NULL values appearing last. Select the second highest enrollment record (rn = 2) from each partition. Exclude any counties where there's only one valid record.
| [
"\nSELECT DISTINCT ON (\"County Code\") * FROM frpm ORDER BY \"County Code\", \"Enrollment (K-12)\" DESC;\n"
] | [] | [] | [] | [] | null | null |
116 | formula_1 | In the context of Formula 1 racing data, I have a table that contains information about the results of races, including the points scored by drivers and their finishing positions. I want to find out the maximum number of points that can be accumulated by a driver in races where the total points scored by all drivers in those races is less than or equal to 100. The expected output is the maximum points a single driver can score under this condition, considering the distribution of points across races. | [
"SELECT SUM(quantity) FROM race_materials WHERE price_per_unit * quantity <= 100;"
] | [] | [
"CREATE TABLE IF NOT EXISTS race_materials (name text, price_per_unit int, quantity int); INSERT INTO race_materials (name, price_per_unit, quantity) VALUES ('A', 3, 30), ('B', 5, 3), ('C', 5, 3), ('D', 6, 20);"
] | [
"DROP TABLE race_materials;"
] | [] | null | null |
117 | formula_1 | In the context of Formula 1 racing data, I have two tables: `drivers` and `results`. The `drivers` table contains information about each driver, including their unique `driverId` and `driverRef`. The `results` table records the outcomes of races, including the `driverId` of the participant, their `position` in the race, and the `points` they scored. I want to filter `drivers` by their `position` and `points` in the `results` table. Typically, I filter between 2-5 `position` and `points` values. There are approximately 6-10 `results` entries per driver. Given the large size of the database, I aim to optimize this query for performance, possibly by eliminating the `HAVING` clause. Here's the query I'm currently using:```sql
SELECT
drivers.forename,
drivers.surname,
jsonb_agg(jsonb_strip_nulls(jsonb_build_object('laps', results.laps, 'position', results.position))) AS race_results
FROM
drivers
JOIN
results
ON
drivers.driverid = results.driverid
GROUP BY
drivers.forename,
drivers.surname
HAVING
jsonb_agg(jsonb_build_object('laps', results.laps, 'position', results.position)) @? '$[*] ? (@.laps == 56) ? (@.position == 1)';
``` | [
"SELECT \n drivers.forename, \n drivers.surname, \n jsonb_agg(jsonb_strip_nulls(jsonb_build_object('laps', results.laps, 'position', results.position))) AS race_results\nFROM \n drivers\nJOIN \n results\nON \n drivers.driverid = results.driverid\nGROUP BY \n drivers.forename, \n drivers.surname\nHAVING \n jsonb_agg(jsonb_build_object('laps', results.laps, 'position', results.position)) @? '$[*] ? (@.laps == 56) ? (@.position == 1)';"
] | [] | [
""
] | [
""
] | [] | null | true |
118 | european_football_2 |
How can I generate a table that groups data from a player_attributes table by player_fifa_api_id and player_api_id and, for each group, concatenates the overall_rating values into a field?
| [
"INSERT INTO historical_rating (player_fifa_api_id, player_api_id, grouped_rating) SELECT player_fifa_api_id, player_api_id AS grouped_rating FROM player_attributes GROUP BY player_fifa_api_id, player_api_id;"
] | [] | [
"CREATE TABLE historical_rating (player_fifa_api_id INT, player_api_id INT, grouped_rating TEXT);"
] | [
"DROP TABLE historical_rating"
] | [] | null | null |
119 | codebase_community |
In a database that contains a table named 'posts', each post can reference a parent post through the 'acceptedanswerid' column. The goal is to retrieve posts based on the following conditions: (1) Include the parent post if the parent's 'score' is greater than or equal to 20, and (2) Include the child post if the parent's 'score' is less than 20 but the child's 'score' is greater than or equal to 20. Both parent and child posts should not be included simultaneously if the parent satisfies the condition. How can I write a query to achieve this?
| [
"SELECT DISTINCT id, acceptedanswerid, posttypeid, score FROM posts WHERE score >= 20 OR acceptedanswerid IS NOT NULL AND score >= 20 GROUP BY id, acceptedanswerid;"
] | [] | [
""
] | [
""
] | [] | null | null |
120 | superhero |
In the superhero database, we want to identify a list of superheroes who have only the superpowers of 'Flight' and 'Telepathy' and do not possess any other superpowers. The following query seems to work, but I suspect there might be a more efficient way to achieve this result. | [
"SELECT DISTINCT s.id \nFROM superhero s \nWHERE EXISTS (\n SELECT 1 \n FROM hero_power hp \n JOIN superpower sp ON hp.power_id = sp.id \n WHERE s.id = hp.hero_id AND sp.power_name = 'Flight'\n) \nAND EXISTS (\n SELECT 1 \n FROM hero_power hp \n JOIN superpower sp ON hp.power_id = sp.id \n WHERE s.id = hp.hero_id AND sp.power_name = 'Telepathy'\n) \nAND NOT EXISTS (\n SELECT 1 \n FROM hero_power hp \n JOIN superpower sp ON hp.power_id = sp.id \n WHERE s.id = hp.hero_id AND sp.power_name NOT IN ('Flight', 'Telepathy')\n);"
] | [] | [
""
] | [
""
] | [] | null | true |
121 | card_games | We have a table with card collection data which includes the start and end dates of when cards were added to and removed from a collection. Not all cards have an end date as they are still in the collection. We need to calculate the number of new cards added per month, cards removed per month, and existing cards per month. We have already completed counts of new and removed cards per month, but we are facing trouble in calculating the existing cards. The data starts from January 2023. | [
"WITH card_activity AS ( SELECT to_date(fe.start_date_key::text, 'YYYYMMDD') AS start_date, to_date(fe.end_date_key::text, 'YYYYMMDD') AS end_date, dp.set_name, dp.set_code FROM fact_collection fe INNER JOIN dim_set dp ON fe.set_key = dp.set_key ) SELECT date_trunc('month', month_series) AS month, COUNT(*) AS existing_cards, sa.set_name FROM ( SELECT generate_series( (SELECT MIN(to_date(start_date_key::text, 'YYYYMMDD')) FROM fact_collection), '2100-12-31', INTERVAL '1 month') AS month_series ) AS months LEFT JOIN card_activity sa ON sa.start_date < month_series AND (sa.end_date IS NULL OR sa.end_date >= month_series) GROUP BY month, sa.set_name;"
] | [] | [
"CREATE TABLE dim_set ( set_key int4 GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE) NOT NULL, set_name varchar NULL, set_code varchar NULL ); CREATE TABLE fact_collection ( card_key int4 NULL, start_date_key int4 NULL, end_date_key int4 NULL, set_key int4 NULL ); INSERT INTO dim_set (set_name, set_code) VALUES ('Core Set', '10E'); INSERT INTO fact_collection (card_key, start_date_key, end_date_key, set_key) VALUES (1, 20230105, 20230130, 1), (2, 20230106, 20230120, 1), (3, 20230405, 20230420, 1); INSERT INTO fact_collection (card_key, start_date_key, set_key) VALUES (4, 20230110, 1), (5, 20230120, 1), (6, 20230220, 1), (7, 20230202, 1), (8, 20230228, 1), (9, 20230206, 1), (10, 20230406, 1);"
] | [
"DROP TABLE IF EXISTS fact_collection; DROP TABLE IF EXISTS dim_set;"
] | [] | null | null |
122 | superhero | We have a dataset representing time spans during which superheroes have been active in various missions. Each record includes a superhero's ID, the start time, and the end time of their mission. We need to combine multiple rows into a single row where the missions are continuous (i.e., the end time of one mission is the start time of the next mission for the same superhero). The goal is to find the earliest start time and the latest end time for each continuous span of missions for each superhero. | [
"WITH mission_spans AS ( SELECT hero_id, mission_start, mission_end FROM superhero_missions ORDER BY hero_id, mission_start, mission_end ) SELECT hero_id, MIN(mission_start) OVER (PARTITION BY hero_id), MAX(mission_end) OVER (PARTITION BY hero_id) FROM mission_spans ORDER BY 1, 2, 3"
] | [] | [
"CREATE TABLE superhero_missions ( hero_id bigint, mission_start timestamp, mission_end timestamp );",
"INSERT INTO superhero_missions (hero_id, mission_start, mission_end) VALUES (1, '2023-01-01 09:00:00', '2023-01-01 10:00:00'), (1, '2023-01-01 10:00:00', '2023-01-01 11:00:00'), (1, '2023-01-01 11:00:00', '2023-01-01 12:00:00'), (1, '2023-01-01 13:00:00', '2023-01-01 14:00:00'), (1, '2023-01-01 14:00:00', '2023-01-01 15:00:00'), (1, '2023-01-01 15:00:00', '2023-01-01 16:00:00'), (2, '2023-01-01 10:00:00', '2023-01-01 11:00:00'), (2, '2023-01-01 11:00:00', '2023-01-01 12:00:00'), (2, '2023-01-01 13:00:00', '2023-01-01 14:00:00'), (3, '2023-01-01 10:00:00', '2023-01-01 11:00:00');"
] | [
"DROP TABLE IF EXISTS superhero_missions;"
] | [] | null | null |
123 | card_games | I am trying to find the median release date of all card sets in the 'sets' table of the card_games database. The goal is to obtain the date that is in the middle of all the release dates. I attempted to use the percentile_cont function directly on the date column, but encountered an error. Here is the SQL statement I used: | [
"SELECT percentile_cont(0.5) within group (ORDER by releasedate) FROM sets"
] | [] | [] | [] | [] | null | null |
124 | formula_1 | I am creating a table to track race incidents and I need a check constraint to validate the possible values given a string value. I am creating this table: \\ | [
"CREATE TABLE race_incidents ( incident_type VARCHAR(30) NOT NULL CHECK(incident_type = 'Engine failure' OR incident_type = 'Collision'), incident_description VARCHAR(30) NOT NULL);"
] | [] | [] | [] | [] | null | null |
125 | financial | In the financial database, we have two tables: `trans` and `account`. The `trans` table tracks all transactions made on each account, with multiple rows per account. The `account` table contains only one row per account, representing the most recent transaction details. We need to update the `account` table with the details of the most recent transaction (highest `trans_id`) for each account. The tables have many columns, so we would like to use a method that includes all fields in the update without explicitly listing them. | [
"select * from trans t1 where (account_id, trans_id) in (select account_id, max(trans_id) from trans t1 group by account_id);"
] | [] | [
"CREATE TABLE latest_trans AS SELECT DISTINCT account_id, 0 AS amount, 0 AS trans_id FROM trans;"
] | [
"drop table if exists latest_trans;"
] | [] | null | null |
126 | european_football_2 | I am trying to run a recursive query in PostgreSQL to find all teams that belong under a specific league. The query is the following: I need to find all teams that are part of a league and any sub-leagues they might belong to. However, I am encountering issues with the recursive CTE. The error seems to be related to the structure of the CTE and the use of UNION instead of UNION ALL. Additionally, I am unsure how to properly reference the initial CTE within the recursive part of the query. | [
"WITH TAB AS (SELECT id as league_id, name FROM League UNION SELECT id, name FROM League) , RECURSIVE recuree AS ( SELECT league_id, name FROM TAB UNION SELECT E.league_id, E.name FROM TAB E JOIN recuree S ON E.id = S.league_id) SELECT * FROM recuree"
] | [] | [
""
] | [
""
] | [] | null | null |
127 | superhero | In our superhero database, we have multiple tables that store various attributes and details about superheroes. I need to retrieve all records from the `superhero` table across all column names that end with '_id' in josn. The column names are dynamic, with new ones being added and old ones being removed frequently, so I cannot hardcode the column names in my query. How can I achieve this? | [
"SELECT * \nFROM superhero.\"%_id\";"
] | [] | [
""
] | [
""
] | [] | null | null |
128 | card_games | In the context of managing a database for a card game, I need to ensure that when inserting data into a table, if a null value is provided for certain enum fields, the default value specified during the table creation is used instead. Currently, when I insert data with null values for these enum fields, the null values are being inserted rather than the default values. Here's an example of the issue I'm encountering:
sql
INSERT INTO cards(availability, borderColor) VALUES (NULL, 'black');
This results in the following data being inserted:
|availability|borderColor|
|---|---|
|NULL|black|
However, I need the data to be inserted with the default value for 'availability' when NULL is provided, like so:
|availability|borderColor|
|---|---|
|mtgo,paper|black| | [
"INSERT INTO cards(availability, borderColor) VALUES (NULL, 'black');"
] | [] | [
""
] | [
""
] | [] | null | null |
129 | formula_1 | In the context of Formula 1 racing data, I have a table named `races` with a column `date` of type `date`. I need each of the values in `date` to be unique. Given a `date` input, `input_date`, I need to find the **minimum** `date` value that satisfies the following criteria: the result must be >= `input_date` and the result must not already be in `date`. I cannot merely add one day to the greatest value in `date`, because I need the minimum value that satisfies the above criteria. Is there a concise way to compute this as part of an insert or update to the `races` table? | [
"INSERT INTO races (raceid, year, round, circuitid, name, date, time, url)\nVALUES (\n 999,\n 2023,\n 1,\n 1,\n 'Test Grand Prix',\n '2023-04-01',\n '12:00:00',\n 'http://example.com'\n)"
] | [] | [
""
] | [
""
] | [] | null | null |
130 | formula_1 | In the context of Formula 1 racing data, we have a table that records the lap times of drivers across different races. Each driver has multiple lap times recorded for each race they participate in. The goal is to select a specific driver's lap times across all races they've participated in, aggregating these times by race. Given `driverId=1` and `name=Lewis Hamilton`, we want to retrieve his lap times for each race, aggregated by race, to analyze his performance across different circuits. The desired output should list each race with the corresponding aggregated lap times for the specified driver. | [
"SELECT\n sub.raceId,\n sub.t[1] AS \"Lap_Time_Race1\",\n sub.t[2] AS \"Lap_Time_Race2\",\n sub.t[3] AS \"Lap_Time_Race3\"\nFROM (\n SELECT\n driverId,\n raceId,\n ARRAY_AGG(milliseconds ORDER BY lap) AS t\n FROM lapTimes\n WHERE driverId = 1\n GROUP BY driverId\n) AS sub\nORDER BY sub.raceId;"
] | [] | [
""
] | [
""
] | [] | null | null |
131 | thrombosis_prediction | In the context of a medical database tracking patient examinations and laboratory tests, I have a table called `Examination` that records various test results for patients, including a `Thrombosis` score indicating the severity of thrombosis. Another table, `Laboratory`, logs detailed laboratory test results over time for each patient. I need to retrieve the last two laboratory test results for each patient's `Thrombosis` score, but instead of getting two rows for each patient, I want to have two new columns with the previous `Thrombosis` scores, labeled as `Thrombosis_1` and `Thrombosis_2`. The desired result should look like this: ID | Thrombosis_1 | Thrombosis_2, where each row represents a patient with their last two `Thrombosis` scores. | [
"SELECT * FROM Examination AS e LEFT JOIN LATERAL (SELECT * FROM Laboratory WHERE ID = e.ID ORDER BY Date DESC LIMIT 2) AS lab ON true"
] | [] | [
""
] | [
""
] | [] | null | null |
132 | toxicology | In the context of a toxicology database, we have a scenario where we need to identify the atoms that are most frequently connected to other atoms within molecules, essentially finding the atoms with the most 'friends' (connections) and the number of these connections. Given the schema and data provided, we aim to find the atom(s) with the highest number of connections and the count of these connections. | [
"select a.f as \"id\", count(a.f) as \"num\" from ( select atom_id as f from connected union all select atom_id2 as f from connected ) a group by a.f order by count(a.f) desc limit 1;"
] | [] | [
""
] | [
""
] | [] | null | null |
133 | european_football_2 | I'm trying to create a trigger that will add a new row processed entry to a destination table each time a new row is created in the source table. The source table contains detailed information about match events in a football league, including timestamps, match IDs, player IDs, event types, and additional data about the event context. The destination table should store processed information about these events, including the timestamp, match ID, player ID, event type, a simplified URL from the event context, and the user ID who recorded the event. The trigger function is not working as expected, and no new entries are being recorded in the destination table despite new rows being inserted into the source table. | [
"CREATE OR REPLACE FUNCTION triger_function() RETURNS TRIGGER AS $BODY$ BEGIN INSERT INTO public.destination_table ( created_at, match_id, player_id, event_type, url, user_id) SELECT created_at, match_id, player_id, event_type, split_part(url::text, '?', 1) AS url, ((((data >> '{}')::jsonb >> '{}')::jsonb -> 'local_storage'::text) -> 'data'::text) >> '{}' -> 'user_id'::varchar FROM source_table; RETURN new; END; $BODY$ language plpgsql;",
"CREATE TRIGGER after_insert_source_error\nAFTER INSERT ON public.source_table\nFOR EACH ROW\nEXECUTE PROCEDURE trigger_function_error();",
"INSERT INTO public.source_table (\n created_at,\n match_id,\n player_id,\n event_type,\n url,\n data\n)\nVALUES (\n NOW(), -- created_at\n 101, -- match_id\n 202, -- player_id\n 'goal', -- event_type\n 'http://example.com?foo=bar', -- url\n '{\n \"local_storage\": {\n \"data\": {\n \"user_id\": \"u12345\"\n }\n }\n }'::jsonb -- data\n);"
] | [] | [
"CREATE TABLE public.source_table (\n id serial PRIMARY KEY,\n created_at TIMESTAMP NOT NULL,\n match_id INTEGER NOT NULL,\n player_id INTEGER NOT NULL,\n event_type VARCHAR NOT NULL,\n url VARCHAR NOT NULL,\n data JSONB NOT NULL\n);\n\nCREATE TABLE public.destination_table (\n id serial PRIMARY KEY,\n created_at TIMESTAMP NOT NULL,\n match_id INTEGER NOT NULL,\n player_id INTEGER NOT NULL,\n event_type VARCHAR NOT NULL,\n url VARCHAR NOT NULL,\n user_id VARCHAR\n);"
] | [
"DROP TABLE public.source_table; DROP TABLE public.destination_table;"
] | [] | null | null |
134 | superhero | I am given a task to optimize the following query (not originally written by me). The query calculates the success rate of superheroes requesting new powers, where each request can have up to 5 retries if it fails.
The query needs to return:
1. The total number of requests,
2. The success rate (ratio of successful to total requests),
3. Grouped by the power requested and the superhero's ID.
However, the current query is very slow on a large dataset (millions of rows). Below is the table-creation script, data-insertion script, and the problematic query. How can I optimize this further?
| [
"SELECT\n s.superhero_id AS \"superheroId\",\n p.power_name AS \"power\",\n COUNT(*) AS \"totalRequests\",\n SUM(\n CASE WHEN r.status = 'success' THEN 1 ELSE 0 END\n ) * 100.0 / COUNT(*) AS \"successRate\"\nFROM public.superhero_requests s\nJOIN public.superpower p\n ON s.superpower_id = p.id\nJOIN (\n SELECT\n id,\n superhero_id,\n superpower_id,\n status,\n retry_number,\n main_request_uuid,\n ROW_NUMBER() OVER (\n PARTITION BY main_request_uuid\n ORDER BY retry_number DESC, id DESC\n ) AS row_number\n FROM public.superhero_requests\n) r\n ON s.id = r.id\nWHERE s.created_at >= CURRENT_DATE - INTERVAL '30 days'\n AND s.created_at <= CURRENT_DATE\n AND r.row_number = 1\nGROUP BY\n s.superhero_id,\n p.power_name\nLIMIT 10;"
] | [] | [
"CREATE TABLE IF NOT EXISTS public.superhero_requests (\n id BIGINT NOT NULL,\n superhero_id BIGINT NOT NULL,\n superpower_id BIGINT NOT NULL,\n status TEXT NOT NULL,\n created_at TIMESTAMPTZ NOT NULL,\n retry_number SMALLINT NOT NULL,\n main_request_uuid VARCHAR NOT NULL,\n PRIMARY KEY (id)\n);",
"INSERT INTO public.superhero_requests (\n id,\n superhero_id,\n superpower_id,\n status,\n created_at,\n retry_number,\n main_request_uuid\n)\nSELECT\n generate_series(1, 1000000) AS id,\n (random() * 750)::int + 1 AS superhero_id, -- IDs between 1..750\n (random() * 5)::int + 1 AS superpower_id, -- referencing our 5 sample powers (1..5)\n CASE WHEN random() < 0.8 THEN 'success'\n ELSE 'failure'\n END AS status, \n NOW() - (random() * 30)::int * '1 day'::interval AS created_at,\n (random() * 5)::int AS retry_number, -- 0..5\n md5(random()::text) AS main_request_uuid -- random UUID-like string\n;"
] | [
"DROP TABLE public.superhero_requests;"
] | [] | null | true |
135 | formula_1 | In the Formula 1 database, I have a table named 'races' that contains information about each race, including the date of the race. I want to create a SELECT statement that not only returns the contents of the 'races' table but also includes an additional column that tells me how many races were held in the same year. For example, if there were 3 races in '2009', then for each race on that date, the 'same_year_races' column should be 3. I can create a separate statement using GROUP BY, but I'm looking for a way to make a single statement that includes the 'same_year_races' column in the results table. | [
"SELECT raceId, name, year, COUNT(*) AS same_year_races FROM races GROUP BY raceid, name, year"
] | [] | [] | [] | [] | null | null |
136 | card_games | In the card_games database, we have a table named 'card_prices' that records the price changes of cards over time. Each entry includes the timestamp of the price change, the card's UUID, and the new price. When a new price is recorded, we need to take the first price older than 30 days, along with prices from the last 30 days, and return the minimum. When new price came, we should not include in range: New Price Date: 2022/08/11; 30 days subtracted date: 2022/07/12. However, since price changes are not daily, we must also consider the price before the 30-day window to ensure we capture the correct minimum price. The current query is slow and sometimes returns incorrect results due to the way it handles the 30-day window and the price before it. We need to optimize the query to ensure it runs efficiently and accurately. | [
"SELECT min(price) FROM card_prices WHERE (timestamp >= (SELECT MAX(timestamp) FROM card_prices WHERE timestamp < '2022-07-12T15:30:00-00:00' AND uuid = '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c') OR timestamp >= '2022-07-12T15:30:00-00:00' AND timestamp < '2022-08-11T15:30:00-00:00') AND uuid = '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c' AND price > 0"
] | [] | [
"CREATE TABLE card_prices (timestamp timestamp with time zone not null, uuid varchar(200) not null, price numeric(12, 2));",
"INSERT INTO card_prices (timestamp, uuid, price) VALUES ('2022-07-09 18:18:39.000000 +00:00','5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 120.00), ('2022-07-10 15:45:56.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 125.00), ('2022-07-12 08:00:10.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 130.00), ('2022-07-14 13:15:55.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 135.00), ('2022-07-16 10:33:11.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 140.00), ('2022-07-18 20:18:48.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 145.00), ('2022-07-20 07:40:29.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 150.00), ('2022-07-22 14:11:59.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 155.00), ('2022-07-25 11:55:30.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 160.00), ('2022-07-28 16:05:07.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 165.00), ('2022-07-30 09:40:25.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 170.00), ('2022-08-02 18:30:13.000000 +00:00', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c', 175.00), ('2022-08-05 21:19:40.000000 +00:00','5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c',130.00), ('2022-08-10 11:20:39.000000 +00:00','5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c',140.00);"
] | [
"DROP TABLE card_prices;"
] | [] | null | true |
137 | superhero | Imagine the following data representing the attribute scores of superheroes over five consecutive years. We are interested in identifying superheroes whose attribute scores have gaps (null values) or have changed from one year to another, or both. The output should include only those superheroes who have gaps or scores different from their maximum score recorded over the years. For example, if a superhero's maximum score in an attribute is 100, and their scores over the years are 100, 90, 100, null, 100, this superhero should be included in the output because of the gap in year 4. Similarly, if a superhero's scores are 100, 90, 100, 100, 100, this superhero should also be included because their score in year 2 is different from their maximum score. | [
"with hero_attribute_data (hero_id, attribute_id, max_score, year_1, year_2, year_3, year_4, year_5) as (values (1, 1, 80, 80, 80, 80, null, 80), (2, 2, 90, 90, 85, 90, 88, 90), (3, 3, 75, 75, 70, null, 75, 75), (4, 4, 60, null, 60, 60, 60, null)), score_check as (select *, case when (max_score <> year_1 or max_score <> year_2 or max_score <> year_3 or max_score <> year_4 or max_score <> year_5) then false else true end as is_a_match from hero_attribute_data) select * from score_check where is_a_match is false"
] | [] | [] | [] | [] | null | null |
138 | superhero | In the superhero database, we have a table that represents the crafting recipes for special items used by superheroes. Each item can be bought directly from the store or crafted using other items and resources. The goal is to determine whether it is more cost-effective to buy an item directly or to craft it using the available resources and other items. The table includes the item ID, its retail price, the quantity needed, and the resources or items required to craft it along with their respective prices. We need to calculate both the partial craft price (cost of crafting using only resources) and the full craft price (cost of crafting using both resources and other items). The partial craft price is straightforward to calculate, but the full craft price requires a recursive approach to account for nested dependencies. The user attempted to use a recursive CTE to calculate the full craft price but encountered issues with the query logic. We need to correct the query to accurately compute the full craft price for each item. | [
"select item_id, item_price as retail_price, sum(coalesce(uses_item_price, 0) * quantity) + sum(coalesce(resource_price*quantity, 0)) as partial_craft_price FROM store GROUP BY item_id, retail_price;"
] | [] | [
"CREATE TABLE store ( item_id integer, item_price real, quantity integer, uses_item_id integer, uses_item_price real, resource_id integer, resource_price real );",
"INSERT INTO store (item_id, item_price, quantity, uses_item_id, uses_item_price, resource_id, resource_price) VALUES (1, 10000, 10, null, null, 5, 50 ), (1, 10000, 20, null, null, 6, 50 ), (2, 150, 1, 1, 10000, null, null), (2, 150, 5, null, null, 8, 50 ), (3, 5500, 3, null, null, 9, 50 ), (3, 5500, 50, 1, 10000, null, null ), (3, 5500, 1, 2, 150, null, null );"
] | [
"DROP TABLE store;"
] | [] | null | null |
139 | superhero | I want to UPDATE the superhero's full_name in the superhero table using a WHERE clause by team_id = 91 and using JOIN. The main table superhero structure looks like: | id | superhero_name | full_name | The two fields in two more connected tables with the values I need are in team_member and team_member_superhero tables. Table team_member_superhero structure looks like: | id | team_member_id | superhero_id | Table team_member: | id | team_id | superhero_id | | [
"UPDATE superhero SET full_name = 'Superman' JOIN team_member_superhero ON superhero.id = team_member_superhero.superhero_id JOIN team_member ON team_member_superhero.team_member_id = team_member.id WHERE team_id = 91;"
] | [] | [
"CREATE TABLE team_member_superhero (id bigint NOT NULL, team_member_id bigint NULL, superhero_id bigint NULL, PRIMARY KEY (id));",
"CREATE TABLE team_member (id bigint NOT NULL, team_id bigint NULL, PRIMARY KEY (id));",
"INSERT INTO team_member_superhero (id, team_member_id, superhero_id) VALUES (1, 1, 1);",
"INSERT INTO team_member (id, team_id) VALUES (1, 91);"
] | [
"UPDATE superhero s SET full_name = 'Charles Chandler' FROM team_member_superhero tms JOIN team_member tm ON tms.team_member_id = tm.id WHERE s.id = tms.superhero_id AND tm.team_id = 91;",
"DROP TABLE team_member_superhero;",
"DROP TABLE team_member;"
] | [] | null | null |
140 | european_football_2 | The football league management system requires a report that shows the performance of each team in terms of the number of matches played, victories, defeats, draws, and the total score. The score is calculated by awarding 3 points for a victory, 1 point for a draw, and 0 points for a defeat. The data is stored in the 'team' and 'match' tables. The 'team' table contains the team details, and the 'match' table contains the match details including the goals scored by the home and away teams. The task is to generate a report that includes the team name, number of matches, victories, defeats, draws, and the total score for each team. The user has attempted to write a query but is seeking a more efficient and cleaner solution. | [
"SELECT t.team_long_name, count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id) as matches, count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id AND m.home_team_goal > m.away_team_goal) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id AND m.away_team_goal > m.home_team_goal) as victories, count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id AND m.home_team_goal < m.away_team_goal) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id AND m.away_team_goal < m.home_team_goal) as defeats, count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id AND m.home_team_goal = m.away_team_goal) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id AND m.away_team_goal = m.home_team_goal) as draws, ((count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id AND m.home_team_goal > m.away_team_goal) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id AND m.away_team_goal > m.home_team_goal)) * 3) + count(m.home_team_api_id) filter(WHERE t.team_api_id = m.home_team_api_id AND m.home_team_goal = m.away_team_goal) + count(m.away_team_api_id) filter(WHERE t.team_api_id = m.away_team_api_id AND m.away_team_goal = m.home_team_goal) as score FROM team t JOIN match m ON t.team_api_id IN (m.home_team_api_id, m.away_team_api_id) GROUP BY t.team_long_name ORDER BY victories DESC"
] | [] | [] | [] | [] | null | true |
141 | thrombosis_prediction | We have two tables in our thrombosis_prediction database: patient and examination. The patient table contains patient information, and the examination table contains examination results for each patient. We want to create a report that shows each patient's ID and whether they have had an examination with specific diagnoses (e.g., 'SLE', 'PSS', 'RA susp.') recorded. The result should be a table with patient IDs and columns for each diagnosis, indicating TRUE if the patient has had an examination with that diagnosis and FALSE otherwise. The user attempted to write a query but encountered issues with multiple rows for each patient and incorrect TRUE/FALSE values. | [
"SELECT p.id, CASE WHEN e.id = p.id AND e.diagnosis = 'SLE' THEN TRUE ELSE FALSE END AS SLE, CASE WHEN e.id = p.id AND e.diagnosis = 'PSS' THEN TRUE ELSE FALSE END AS PSS, CASE WHEN e.id = p.id AND e.diagnosis = 'RA susp.' THEN TRUE ELSE FALSE END AS RA_susp FROM patient p LEFT JOIN examination e ON p.id = e.id WHERE e.diagnosis IN ('SLE', 'PSS', 'RA susp.') ORDER BY p.id DESC"
] | [] | [] | [] | [] | null | null |
142 | superhero | We have two tables in our superhero database, 'hero_birth_info' and 'hero_birth_info_alt'. The 'hero_birth_info' table contains the birthdate of superheroes, while the 'hero_birth_info_alt' table contains an alternative date of birth for some superheroes. We need to combine these two tables to get a single birth date for each superhero, prioritizing the date from 'hero_birth_info_alt' if it exists. However, when we run the following query, we get incorrect results where the birthdate from 'hero_birth_info_alt' is not correctly combined with the birthdate from 'hero_birth_info'. | [
"SELECT hbi.id, hbi.hero_name, hbi.birthdate, hbialt.date_of_birth FROM hero_birth_info hbi LEFT JOIN hero_birth_info_alt hbialt ON hbi.id = hbialt.id"
] | [] | [
"CREATE TABLE hero_birth_info (id bigint NOT NULL, hero_name text NULL, birthdate date NULL, PRIMARY KEY (id));",
"INSERT INTO hero_birth_info (id, hero_name, birthdate) VALUES (1, 'Sylvie', '2016-06-01'), (2, 'Rolf', NULL), (3, 'Jose', '2004-02-16'), (4, 'Eugin', NULL), (5, 'Andrey', '1998-09-29'), (6, 'Ivan', '2000-05-12'), (7, 'Vasiys', '2001-07-17'), (8, 'Alexey', '1993-09-05');",
"CREATE TABLE hero_birth_info_alt (id bigint NOT NULL, hero_name text NULL, date_of_birth date NULL, PRIMARY KEY (id));",
"INSERT INTO hero_birth_info_alt (id, hero_name, date_of_birth) VALUES (1, 'Sylvie', NULL), (2, 'Rolf', '2015-12-06'), (3, 'Jose', NULL), (4, 'Eugin', '1995-04-01'), (5, 'Andrey', '1998-09-29');"
] | [
"DROP TABLE hero_birth_info;",
"DROP TABLE hero_birth_info_alt;"
] | [] | null | null |
143 | superhero | In the superhero database, there are instances where the hair_colour_ids of superheroes within the same publisher are not contiguous due to deletions. To address this, I want to create a function that will reindex these hair_colour_ids to make them contiguous again. The function should take a publisher ID as input and reindex the hair_colour_ids of superheroes belonging to that publisher. I attempted to create a function that uses a temporary sequence to achieve this, but encountered an error when trying to use the sequence within the function. The error message was 'relation "id_seq_temp" does not exist'. I tried creating the sequence outside the function, but I prefer to keep the sequence within the function for cleaner code. Here is my function, with some names anonymized for privacy reasons. | [
"CREATE OR REPLACE FUNCTION reindex_superhero_ids(IN BIGINT) RETURNS VOID LANGUAGE SQL AS $$ CREATE TEMPORARY SEQUENCE id_seq_temp MINVALUE 1 START WITH 1 INCREMENT BY 1; ALTER SEQUENCE id_seq_temp RESTART; UPDATE superhero SET hair_colour_id=hair_colour_id+2000 WHERE publisher_id=$1; UPDATE superhero SET hair_colour_id=nextval('id_seq_temp') WHERE publisher_id=$1; $$;"
] | [] | [] | [
"DROP FUNCTION IF EXISTS reindex_superhero_ids(BIGINT);",
"UPDATE superhero SET hair_colour_id = 1 WHERE publisher_id = 9;"
] | [] | null | null |
144 | card_games | In the card_games database, a table named card_versions was created to track different versions of a card with a unique sequence number, definition ID, attribute ID, and input data. The primary key is a composite key consisting of defn_id, attr_id, and seqnr. Records were inserted into the card_versions table with sequential seqnr values for a specific defn_id and attr_id. When attempting to update the seqnr values by incrementing them for all records with defn_id = 100 and attr_id = 100, an error occurred in PostgreSQL due to the immediate uniqueness constraint check on the primary key. The task is to reproduce this issue and provide a solution to update the seqnr values without violating the primary key constraint. | [
"UPDATE card_versions SET seqnr=seqnr+1 WHERE defn_id = 100 AND attr_id = 100 AND seqnr >= 1"
] | [] | [
"CREATE TABLE card_versions(seqnr smallint NOT NULL, defn_id int NOT NULL, attr_id int NOT NULL, input CHAR(50) NOT NULL, CONSTRAINT pk_card_versions PRIMARY KEY (defn_id, attr_id, seqnr));",
"INSERT INTO card_versions(seqnr,defn_id,attr_id,input) VALUES (1,100,100,'test1'), (2,100,100,'test2'), (3,100,100,'test3'), (4,100,100,'test4'), (5,100,100,'test5');"
] | [
"DROP TABLE card_versions;"
] | [] | null | null |
145 | european_football_2 | As part of an analysis for a football analytics project, I need to create a JSONB column in the 'team_attributes' table that consolidates several existing attributes into a single JSONB object. Initially, I attempted to use the following query to populate this JSONB column with values from other columns in the same table: | [
"UPDATE team_attributes SET attributes_jsonb = jsonb_insert('{}', '{buildupplayspeed}', buildupplayspeed) WHERE team_api_id = 1773;",
"SELECT id, attributes_jsonb FROM team_attributes WHERE team_api_id = 1773 ORDER BY id;"
] | [] | [
"ALTER TABLE team_attributes ADD COLUMN attributes_jsonb jsonb"
] | [
"ALTER TABLE team_attributes DROP COLUMN attributes_jsonb"
] | [] | null | null |
146 | toxicology | In the toxicology database, we need to identify all ancestor atoms of a given atom using the 'connection' table, which contains the relationships between atoms through bonds. Each atom is linked to its parent atom via the 'atom_id' and 'parent_id' fields in the 'connection' table. Additionally, each atom has an 'enabled' status in the table, which indicates whether the atom is active or not. If any ancestor atom of a given atom has its 'enabled' status set to false, the query should return no results for that atom. For example, if we are querying for all ancestor atoms of atom 'TR000_4', and any of its ancestor atoms have 'enabled' set to false, the query should return no results. | [
"WITH RECURSIVE atom_ancestors AS ( SELECT atom_id, parent_id, enabled FROM connection WHERE atom_id = 4 UNION ALL SELECT c.atom_id, c.parent_id, c.enabled FROM connection c INNER JOIN atom_ancestors aa ON c.atom_id = aa.parent_id) SELECT * FROM atom_ancestors"
] | [] | [
"CREATE TABLE connection (atom_id integer, parent_id integer, enabled boolean)",
"INSERT INTO connection (atom_id, parent_id, enabled) VALUES (1, null, true), (2, 1, false), (3, 1, true), (4, 2, true);"
] | [
"DROP TABLE connection;"
] | [] | null | null |
147 | card_games |
Is there an efficient way to aggregate data from a JSONB column in PostgreSQL? Given the table card_preference(customerid STRING, preference JSONB) and the data in the table. As you notice, I want unique values of keys (for example, "dis") across all records, grouped by customerid. I tried extracting values using jsonb_agg and jsonb_array_elements, but I couldn’t aggregate all keys and distinct values correctly. I also couldn't figure out how to use jsonb_each to get all keys. What I tried is something like this to get a single key. Any help with the query is appreciated.
| [
"\nselect customerid,\n (select jsonb_agg(t->>'dis') from jsonb_array_elements(preference::jsonb) as x(t) where t->>'dis' is not null) as sdata\nfrom card_preference where customerid='123.abc'\n"
] | [] | [
"\nCREATE TABLE card_preference (\n customerid TEXT NOT NULL,\n preference JSONB NOT NULL\n);\nINSERT INTO card_preference (customerid, preference)\nVALUES\n ('123.abc', '{\"dis\": [\"close\"]}'),\n ('123.abc', '{\"purpose\": {\"score\": 0.1, \"text\": \"hi\"}, \"dis\": [\"hello\", \"close\"]}'),\n ('123.abc', '{\"dis\": [\"bye\"], \"dir\": 1}'),\n ('123.abc', '{}'),\n ('567.bhy', '{\"dis\": [\"close\"]}');\n"
] | [
"drop table card_preference"
] | [] | ('123.abc', '{"dis": ["close"]}'), ('123.abc', '{"purpose": {"score": 0.1, "text": "hi"}, "dis": ["hello", "close"]}'), ('123.abc', '{"dis": ["bye"], "dir": 1}'), ('123.abc', '{}'), ('567.bhy', '{"dis": ["close"]}') | null |
148 | superhero | In the superhero database, we have a table named 'hero_attribute' that stores the attribute values for each superhero. Each superhero can have multiple attributes, and each attribute can have different values for different superheroes. The goal is to identify any differences in attribute values for the same superhero across different records. For example, if a superhero has different values for the same attribute in different records, we need to flag that attribute for that superhero. The outcome should be a list of superheroes with the attribute names that have differences. | [
"\nSELECT hero_id, 'attribute_value' AS Difference FROM hero_attribute \nGROUP BY hero_id HAVING COUNT(DISTINCT attribute_value) > 1 \nOR (COUNT(attribute_value) != COUNT(*) AND COUNT(DISTINCT attribute_value) > 0) \nUNION ALL SELECT hero_id, 'attribute_id' AS Difference FROM hero_attribute GROUP BY hero_id \nHAVING COUNT(DISTINCT attribute_id) > 1 OR (COUNT(attribute_id) != COUNT(*) \nAND COUNT(DISTINCT attribute_id) > 0)\n"
] | [] | [
""
] | [
""
] | [] | null | null |
149 | student_club | In the student_club database, there is a table named `member` with the following columns: `member_id` (primary key), `first_name`, `last_name`, `email`, `position`, `t_shirt_size`, `phone`, `zip`, and `link_to_major`. The `link_to_major` column has a `NOT NULL` constraint. The user attempted to insert a new member into the `member` table with the following query:
sql
INSERT INTO member(member_id, first_name, last_name)
VALUES ('new_member_id', 'John', 'Doe')
ON CONFLICT (member_id)
DO UPDATE SET first_name=excluded.first_name, last_name=excluded.last_name;
However, the query resulted in an error because the `link_to_major` column, which has a `NOT NULL` constraint, was not provided a value. The error message from PostgreSQL was:
> ERROR: null value in column "link_to_major" of relation "member" violates not-null constraint
The user expected the `ON CONFLICT` clause to handle the update without requiring a value for `link_to_major`, but this did not happen. The user is unsure why the `NOT NULL` constraint is being enforced during the `ON CONFLICT` update, even though the column has a value in the existing row. | [
"INSERT INTO member(member_id, first_name, last_name) VALUES ('new_member_id', 'John', 'Doe') ON CONFLICT (member_id) DO UPDATE SET first_name=excluded.first_name, last_name=excluded.last_name;"
] | [] | [] | [] | [] | null | null |
150 | student_club | I have a table called `event` that stores `event_id` and `event_date`. I want to grab the most recent 'streak' for an event with a given name. A 'streak' is the number of consecutive days that the event has occurred at least once. An event may occur more than once a day. Big gotcha: The streak should also take into account a given timezone. Given a query for 'April Meeting' in 'MST' timezone, I'd expect the streak to be: | Streak Count | Name | TimeZone | Start Date | End Date |. However, my current query is not working as expected and I'm not sure why. Here's the problematic SQL statement I used: | [
"SELECT COUNT(*) AS streak_count, 'April Meeting' AS event_name, 'MST' AS timezone, MIN(event_date) AS start_date, MAX(event_date) AS end_date FROM attendance WHERE event_name = 'April Meeting' GROUP BY DATE_TRUNC('day', event_date AT TIME ZONE 'UTC' AT TIME ZONE 'MST') ORDER BY end_date DESC LIMIT 1;"
] | [] | [] | [] | [] | null | null |
151 | formula_1 | We have a table named 'pitstops' that stores pit stop data for Formula 1 races, including a column 'transaction_timestamp' which records the timestamp of each pit stop in nanoseconds. We need to group and count the number of pit stops by day. We attempted to convert the nanosecond timestamp to milliseconds and then use DATE_TRUNC to group by day, but encountered errors in our SQL queries. | [
"SELECT DATE_TRUNC('day', to_timestamp(transaction_timestamp / 1000000000.0)), COUNT(*) FROM pitstops GROUP BY DATE_TRUNC('day', transaction_timestamp)"
] | [] | [
"ALTER TABLE pitstops ADD COLUMN transaction_timestamp bigint;",
"UPDATE pitstops SET transaction_timestamp = EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000000000 + (random() * 1000000000)::bigint;"
] | [
"ALTER TABLE pitstops DROP COLUMN transaction_timestamp;"
] | [] | null | null |
152 | thrombosis_prediction | I am trying to obtain the laboratory test results for each patient where the values of 'got' and 'gpt' at the minimum end_date are equal to the values of 'got' and 'gpt' at the maximum end_date, grouped by patient id. My current query only obtains the laboratory test results from the maximum end_date for each patient, but I need the row minimum end_date where 'got' and 'gpt 'match the 'got' and 'gpt' in the maximum end_date, grouped by patient id. | [
"select a.id, a.date, a.got, a.gpt from laboratory as a inner join ( select id, max(date) as date from laboratory group by id ) as b on a.id = b.id and a.date = b.date order by id, date"
] | [] | [] | [] | [] | null | null |
153 | formula_1 | In the Formula 1 database, we need to generate a report that lists the financial roles associated with each race based on the roles of the constructors in that race. Each race can have multiple constructors, and each constructor can have multiple roles such as 'AR (Customer Billing)', 'AP (Commander)', and 'AP (Agent)'. The roles are determined by the constructor's performance and participation in the race. The issue arises when a constructor has multiple roles in a race, and the query only considers the first true value, ignoring the others. We need to ensure that all roles for each constructor in a race are considered and listed in the report. | [
"WITH constructor_roles AS ( SELECT 18 race_id, 1 constructor_id, false customer_billing, true commander, true agent UNION ALL SELECT 18, 2, true, false, false ) SELECT n.race_id, string_agg(distinct CASE WHEN n.customer_billing = TRUE THEN 'AR (Customer Billing)' WHEN n.commander = TRUE THEN 'AP (Commander)' WHEN n.agent = TRUE THEN 'AP (Agent)' ELSE NULL END, ', ') AS finance FROM constructor_roles n WHERE n.race_id = 18 AND (n.customer_billing = TRUE or n.commander = TRUE or n.agent = TRUE) GROUP BY race_id"
] | [] | [] | [] | [] | null | null |
154 | california_schools | A school district is analyzing the academic performance of students across different years and classes. They have a database with tables that track students, the years they attended, and the classes they took. The district wants to generate a report that lists each student's first name along with the years they attended and the classes they took in each year. The current query is producing duplicate entries for years when a student takes multiple classes in the same year. The goal is to merge these entries so that each year appears only once with all the classes listed under it. | [
"SELECT s.firstName, jsonb_agg(DISTINCT jsonb_build_object('yearId', y.id, 'classes', (SELECT jsonb_agg(jsonb_build_object('classId', c.id)) FROM classes AS c WHERE y.id = cy.yearId AND c.id = cy.classId AND s.id = cys.studentId))) AS years FROM users3 AS s LEFT JOIN classYearStudents AS cys ON cys.studentId = s.id LEFT JOIN classYears AS cy ON cy.id = cys.classYearId LEFT JOIN years AS y ON y.id = cy.yearId GROUP BY s.id order by s.id"
] | [] | [
"CREATE TABLE users3 (id SERIAL PRIMARY KEY, firstName TEXT);",
"CREATE TABLE years (id UUID PRIMARY KEY);",
"CREATE TABLE classes (id UUID PRIMARY KEY);",
"CREATE TABLE classYears (id SERIAL PRIMARY KEY, yearId UUID, classId UUID);",
"CREATE TABLE classYearStudents (id SERIAL PRIMARY KEY, studentId INT, classYearId INT);",
"INSERT INTO users3 (firstName) VALUES ('Jarrell'), ('Kevon'), ('Antone');",
"INSERT INTO years (id) VALUES ('bd5b69ac-6638-4d3e-8a52-94c24ed9a039'), ('7f5789b5-999e-45e4-aba4-9f45b29a69ef');",
"INSERT INTO classes (id) VALUES ('2590b596-e894-4af5-8ac5-68d109eee995'), ('fe4a11f2-5f38-4f7a-bbce-609bc7ad8f99'), ('c8cda7d1-7321-443c-b0ad-6d18451613b5');",
"INSERT INTO classYears (yearId, classId) VALUES ('bd5b69ac-6638-4d3e-8a52-94c24ed9a039', '2590b596-e894-4af5-8ac5-68d109eee995'), ('bd5b69ac-6638-4d3e-8a52-94c24ed9a039', 'fe4a11f2-5f38-4f7a-bbce-609bc7ad8f99'), ('7f5789b5-999e-45e4-aba4-9f45b29a69ef', 'c8cda7d1-7321-443c-b0ad-6d18451613b5');",
"INSERT INTO classYearStudents (studentId, classYearId) VALUES (1, 1), (1, 2), (2, 3), (2, 1), (2, 2), (3, 3), (3, 1), (3, 2);"
] | [
"DROP TABLE users3;",
"DROP TABLE years;",
"DROP TABLE classes;",
"DROP TABLE classYears;",
"DROP TABLE classYearStudents;"
] | [] | null | null |
155 | thrombosis_prediction | In the thrombosis_prediction database, there is a need to analyze the hierarchy of medical staff within the hospital. Each staff member has an employeeid, a bossid, and a salary. The hierarchy is already established, and each staff member can have direct and indirect managers. The task is to find, for each staff member, the lowest-ranked indirect manager in the hierarchy who earns at least twice as much as the staff member. I has attempted to create a recursive CTE to establish the hierarchy but is struggling to find the correct indirect manager based on the salary condition. The user's query is provided below and is producing incorrect results. In the final result, I need the employeeId, employeeSalary, hierarchical_level, bossId, bossSalary where the boss is the first one whose salary is at least twice as much as the employee. | [
"WITH RECURSIVE EmpMgrCTE AS (SELECT id, bossid, salary, 0 as EmployeeLevel FROM staff WHERE bossid IS NULL UNION ALL SELECT emp.id, emp.bossid, emp.salary, mgr.EmployeeLevel + 1 as EmployeeLevel FROM staff emp INNER JOIN EmpMgrCTE mgr ON emp.bossid = mgr.id) SELECT * FROM EmpMgrCTE emp"
] | [] | [
"CREATE TABLE staff (id bigint PRIMARY KEY, bossid bigint, salary int);",
"INSERT INTO staff (id, bossid, salary) VALUES (20, NULL, 10000), (10, 20, 4500), (50, 10, 3000), (70, 10, 1500), (40, 50, 1500), (60, 70, 2000), (30, 50, 1501);"
] | [
"DROP TABLE staff;"
] | [] | null | null |
156 | card_games | In the card_games database, there is a table named 'cards' which contains various attributes of Magic: The Gathering cards. Each card has a unique identifier (id), a name, a converted mana cost (convertedmanacost), a rarity, and other attributes. For each rarity, I want to find the card with the highest converted mana cost, highest edhrecrank, and lowest multiverseid. For a single rarity, I can do it like this: sql select rarity, id from cards where rarity = 'uncommon' order by convertedmanacost desc nulls last, edhrecrank desc nulls last, multiverseid asc limit 1; but I haven't been able to make it for all rarities. So the output should be something like this: I am using PostgreSQL. Any idea how I should do this? | [
"select rarity, id from cards order by convertedmanacost desc nulls last, edhrecrank desc nulls last, multiverseid asc limit 1;"
] | [] | [] | [] | [] | null | null |
157 | financial | In the financial database, we have a scenario where each account must have at least one associated disposition (disp). However, when attempting to enforce this relationship, the following SQL query fails due to a chicken-egg problem. The user's attempt to create the tables and enforce the relationship is as follows: | [
"DROP TABLE IF EXISTS card, account, disp; CREATE TABLE account (account_id INT PRIMARY KEY NOT NULL, district_id INT NOT NULL, frequency TEXT NOT NULL, date DATE NOT NULL, disp_id INT NOT NULL, FOREIGN KEY (disp_id) REFERENCES disp(disp_id) ON UPDATE CASCADE ON DELETE CASCADE); CREATE TABLE disp (disp_id INT PRIMARY KEY NOT NULL, client_id INT NOT NULL, account_id INT NOT NULL, type TEXT NOT NULL, FOREIGN KEY (account_id) REFERENCES account(account_id) ON UPDATE CASCADE ON DELETE CASCADE);"
] | [] | [] | [] | [] | null | null |
158 | financial | In the financial database, I am trying to aggregate client information into a array, but I am encountering an issue where the array contains a null value as `[null]` instead of an empty array when there are no qualifying rows. I am using PostgreSQL and have tried using `COALESCE` to replace null values, but it doesn't seem to work as expected. Here is the problematic SQL query I used: | [
"SELECT COALESCE(json_agg(CASE WHEN client.client_id IS NULL THEN NULL ELSE json_build_object('client_id', client.client_id, 'gender', client.gender, 'birth_date', client.birth_date) END), '[]') AS AS clients FROM client;"
] | [] | [] | [] | [] | null | null |
159 | debit_card_specializing | In the debit_card_specializing database, we have a table named transactions_1k that records all transactions made by customers at various gas stations. Each transaction includes details such as the date, time, customer ID, card ID, gas station ID, product ID, amount, and price. We have noticed that there are duplicate transactions in the table, where the same transaction (based on card ID, gas station ID, product ID) appears multiple times with different transaction IDs. Specifically, we want to identify and delete the duplicate transactions where the product ID is 5 (Natural) and keep the ones where the product ID is 2 (Nafta). However, our current query is only selecting the transactions with product ID 5. | [
"\nselect * from transactions_1k e\nwhere exists\n ( select * from transactions_1k e2 \n where e.date=e2.date and e.time=e2.time \n and e.cardid=e2.cardid \n and e.gasstationid=e2.gasstationid \n and e.productid='2' and e2.productid='5') order by e.date asc;\n"
] | [] | [
""
] | [
""
] | [] | null | null |
160 | financial | I have a table named "transfer" under the database "financial". The customer id and their transfer amount columns are given, Cum.sum and Bucket columns we need to find using postgresql. To Find cumulative sum , i can write sum(amount) over (order by id rows between unbounded preceding and current row) . But ask is reset cum.sum if amount reach threshold amount. For example threshold amount is 20 and sum of the row 1,2 and 3 values are 23 which is greater than 20 . so we reset the window function. The next threshold value calculate from id 4 onwards. How to write Postgresql code to implement these functions? | [
"\nselect id, amount as cum_sum, 1 as bucket from transfer where id = 1 union all select transfer.id, (case when cum_sum + amount > 20 then amount else cum_sum + amount end), (case when cum_sum + amount > 20 then bucket + 1 else bucket end) from cte join transfer on transfer.id = cte.id + 1\n"
] | [] | [
"\nCREATE TABLE transfer (\n id INT,\n amount INT\n);\n\nINSERT INTO transfer (id, amount)\nVALUES\n(1, 11),\n(2, 6),\n(3, 6),\n(4, 6),\n(5, 13),\n(6, 6),\n(7, 15),\n(8, 6),\n(9, 19),\n(10, 10);\n"
] | [
"drop table if exists transfer;"
] | [] | null | null |
161 | formula_1 | In the Formula 1 database, we have a table named 'results' that contains various performance metrics for each driver in each race. We are interested in analyzing the modal value of certain performance metrics (laps) for each race. The modal value is the most frequent value in a data set, and if there are multiple equally frequent values, we want to return the one that occurs first in the data set. For example, if in a race, the laps completed by drivers are [10, 10, 20, 20, 30], the modal value should be 10 because it occurs first among the most frequent values. We need to calculate the modal value for each race based on the laps columns and return the result along with the raceid. | [
"SELECT raceid mode() within group(order by laps) as modal_laps FROM results;"
] | [] | [] | [] | [] | null | null |
162 | card_games | In the card_games database, I want to run two tests: 1. Identify tables in the 'public' schema that are not listed in the 'required_tables' table. 2. Identify tables listed in the 'required_tables' table that are not present in the 'cards_schema' schema. For the first test, I use the following query which works correctly:\nsql \\/* First Query *\\/\nselect t1.table_name as t1_table_name, t2.table_name as t2_extra_tables_in_schema \\/nfrom required_tables t1 \\/nright join information_schema.tables t2 \\/non t1.table_name = t2.table_name \\/nwhere t2.table_schema='public' \\/nand t1.table_name IS NULL \\/n\nHowever, for the second test, when I try the following query (the equivalent to the first query but with a left join this time):\nsql \\/* Second Query *\\/\nselect t1.table_name as t1_tables_missing_from_schema, t2.table_name from required_tables t1 left join information_schema.tables t2 on t1.table_name = t2.table_name where t2.table_schema='public' and t2.table_name IS NULL; \\/n\nI always get zero results, even though I know that there are some tables in required_tables which is not in the 'public' schema. How do I get around this issue? Can you provide me with a way to get both missing results in a single query (maybe somehow with a full outer join is my guess), the results should include two colums: 'kind' (WHEN required_tables.table_name IS NULL THEN 'extra' ELSE 'missing') and the corresponding 'table_name'? | [
"select t1.table_name as t1_table_name, t2.table_name as t2_extra_tables_in_schema from required_tables t1 right join information_schema.tables t2 on t1.table_name = t2.table_name where t2.table_schema='public' and t1.table_name IS NULL;",
"select t1.table_name as t1_tables_missing_from_schema, t2.table_name from required_tables t1 left join information_schema.tables t2 on t1.table_name = t2.table_name where t2.table_schema='public' and t2.table_name IS NULL;"
] | [] | [
"CREATE SCHEMA IF NOT EXISTS cards_schema; CREATE TABLE IF NOT EXISTS required_tables (table_name name PRIMARY KEY); INSERT INTO required_tables (table_name) VALUES ('cards'), ('foreign_data'), ('rulings'), ('sets'), ('price'), ('source');"
] | [
"DROP TABLE IF EXISTS required_tables CASCADE;"
] | [] | null | null |
163 | european_football_2 | In the context of the 'european_football_2' database, we are managing a graph-like structure to represent football matches and their dependencies. Each match is a node, and the dependencies between matches (such as follow-up matches or related matches) are represented as edges. The match table has id as the primary key and a unique constraint on (id, stage). The dependency table references both id and stage from the match table for match1 and match2.The business requirement is that whenever a match's stage changes, its dependencies need to be re-evaluated and possibly updated. To handle this, we have a 'match' table and a 'dependency' table. The 'match' table includes a stage number to track changes, and the 'dependency' table references the 'match' table using the match ID and stage number. However, when we try to update the version of a match, we encounter an error due to foreign key constraints ('dependency_match1_stage1_fkey' and 'dependency_match2_stage2_fkey'). We need to find a way to update the stage number (the stage number of match1 incresed by 1) and automatically handle the dependencies without starting a transaction manually. | [
"update match set stage = stage + 1 where id = 'match1';"
] | [] | [
"drop table if exists dependency;",
"drop table if exists match;",
"create table match (id varchar(32), stage int not null default 1, status varchar(32), primary key (id), unique (id, stage));",
"create table dependency (match1 varchar(32) not null, stage1 int not null, match2 varchar(32) not null, stage2 int not null, constraint normalize check (match1 < match2), foreign key (match1, stage1) references match (id, stage) on delete cascade, foreign key (match2, stage2) references match (id, stage) on delete cascade, primary key (match1, match2));",
"insert into match (id, stage, status) values ('match1', 1, 'Scheduled'), ('match2', 2, 'Scheduled');",
"insert into dependency (match1, stage1, match2, stage2) values ('match1', 1, 'match2', 2);"
] | [
"drop table if exists dependency;",
"drop table if exists match;"
] | [] | null | null |
164 | superhero | Suppose we have a superhero event schedule where each superhero is scheduled to perform at different events. We have a table `superhero_events` that records the start and end times of these events. We want to retrieve a list of events that: Started within the last 3 days (inclusive) and end before 5 hours into the future (exclusive). The results should be ordered so that events that started in the past appear first, followed by events that start in the future. We can do something like the following query, but it is slow and needs optimization. Is there a way to use indexes to make this faster? | [
"DROP INDEX IF EXISTS idx_a;",
"SELECT * from superhero_events WHERE start_time >= Now()::timestamp - INTERVAL '3 days' AND end_time < now()+'5 hours'::interval ORDER BY CASE WHEN now()+'5 hours'::interval > Now()::timestamp AND start_time < Now()::timestamp THEN 1 WHEN start_time < Now()::timestamp THEN 2 ELSE 3 END;"
] | [] | [
"CREATE TABLE superhero_events (hero_id bigint NOT NULL, start_time timestamp without time zone NOT NULL, end_time timestamp without time zone NOT NULL);",
"ALTER TABLE superhero_events ADD COLUMN event_id SERIAL PRIMARY KEY;",
"ALTER TABLE superhero_events ADD CONSTRAINT chk_time_order CHECK (start_time < end_time);",
"INSERT INTO superhero_events (hero_id, start_time, end_time) SELECT s.id, NOW() - INTERVAL '1 hour' * (random()+0.5), NOW() + INTERVAL '1 hour' * (random()+0.5) FROM superhero s ORDER BY random() LIMIT 250;",
"INSERT INTO superhero_events (hero_id, start_time, end_time) SELECT s.id, NOW() + INTERVAL '5 hour' * (random()+0.5), NOW() + INTERVAL '5 hour' * (random()+0.5) + INTERVAL '2 hour' * (random()+0.5) FROM superhero s ORDER BY random() LIMIT 500;",
"INSERT INTO superhero_events (hero_id, start_time, end_time) SELECT s.id, NOW() - INTERVAL '3 day' * (random()+0.5), (NOW() - INTERVAL '3 day' * (random()+0.5)) + INTERVAL '3 hour' * (random()+0.5) FROM superhero s ORDER BY random() LIMIT 1500;",
"INSERT INTO superhero_events (hero_id, start_time, end_time) SELECT s.id, NOW() - INTERVAL '15 day' * (random()+0.5) - INTERVAL '3 day', (NOW() - INTERVAL '15 day' * (random()+0.5) - INTERVAL '3 day') + INTERVAL '4 hour' * (random()+0.5) FROM superhero s ORDER BY random() LIMIT 2750;"
] | [
"DROP TABLE IF EXISTS superhero_events;"
] | [] | null | true |
165 | california_schools | In the context of analyzing superhero attributes, I have a table ordered by the `hero_id` column. I aim to perform an aggregation on `n` rows at a time but also wish to incorporate the previous `k` and the next `k'` rows as context. For instance, considering the `hero_attribute` table with data ordered by `hero_id`, I want to aggregate `n` rows with overlapping context from adjacent rows. An example query to get the sum of `attribute_value` for `n=2` would group the rows into `[1, 2]`, `[3, 4]`, `[5, 6]` based on `(hero_id-1) / 2`. However, achieving overlapping groups, say with `k = k' = 2`, to get groups like `[1, 2, 3, 4]`, `[1, 2, 3, 4, 5, 6]`, `[3, 4, 5, 6]` based on `attribute_value`, proves challenging. Is there a way to accomplish this in PostgreSQL using `group by` or window functions? The output should contain only one column 'sum', which stores the three aggregation values of group `[1, 2, 3, 4]`, `[1, 2, 3, 4, 5, 6]` and `[3, 4, 5, 6]`. | [
"with hero_attribute(hero_id, attribute_id, attribute_value) as ( values (1, 1, 80), (2, 1, 75), (3, 1, 95), (4, 1, 85), (5, 1, 90), (6, 1, 70) ) select sum(attribute_value) from hero_attribute group by (hero_id-1) / 2;"
] | [] | [] | [] | [] | null | null |
166 | european_football_2 | In the database 'european_football_2', there is a table named 'players' that stores information about football players, including their skills in different areas of the game. Each player has a unique ID, a name, and a array of skills where each skill has a unique skill ID and a description. The task is to retrieve a list of players along with their skills in a specific format, where each player only has a single row, and their skills are represented as an array of skill descriptions. For example, (1, Alice, ['Passing', 'Shooting']). If a player has no skills, their skill array should be empty. The user attempted to write a query to achieve this but encountered issues with duplicate rows and missing entries for players with no skills. | [
"select id, name, d ->> 'description' as skill from player_skills, json_array_elements(skills) as d;"
] | [] | [
"CREATE TABLE player_skills (id INT PRIMARY KEY, name TEXT, skills JSON);",
"INSERT INTO player_skills (id, name, skills) VALUES (1, 'Alice', '[{\"id\": 11, \"description\": \"Passing\"}, {\"id\": 13, \"description\": \"Shooting\"}]'::json), (2, 'Bob', '[{\"id\": 10, \"description\": \"Defending\"}, {\"id\": 9, \"description\": \"Tackling\"}]'::json), (3, 'Sam', '[]'::json);"
] | [
"DROP TABLE player_skills;"
] | [] | {"id": 11, "description": "Passing"}, {"id": 13, "description": "Shooting"}, {"id": 10, "description": "Defending"}, {"id": 9, "description": "Tackling"} | null |
167 | european_football_2 | The database contains a table named 'match_events' with a column 'events' that stores JSON arrays of event objects for each football match. Each event object includes an 'id' and 'description'. The task is to extract all event objects with 'id' equal to 2 from each match record. The user attempted to achieve this using a CASE statement but encountered issues when the number of events in a match exceeded the hardcoded indices in the query. Can you write a sql query without using 'WHERE' or 'HAVING' clause - only inside 'SELECT' without relying on indices? | [
"SELECT CASE when (t.events::json->0->'id')::varchar::int = 2 then (t.events::json->0)::varchar when (t.events::json->1->'id')::varchar::int = 2 then (t.events::json->1)::varchar when (t.events::json->2->'id')::varchar::int = 2 then (t.events::json->2)::varchar else null::varchar end as \"result\" FROM match_events as t;"
] | [] | [
"CREATE TABLE match_events (match_id SERIAL PRIMARY KEY, events JSON);",
"INSERT INTO match_events (events) VALUES ('[{\"id\": 1, \"description\": \"Goal\"}, {\"id\": 2, \"description\": \"Yellow Card\"}, {\"id\": 3, \"description\": \"Substitution\"}]'::json), ('[{\"id\": 1, \"description\": \"Goal\"}, {\"id\": 4, \"description\": \"Substitution\"}, {\"id\": 3, \"description\": \"Red Card\"}, {\"id\": 2, \"description\": \"Goal\"}]'::json);"
] | [
"DROP TABLE match_events;"
] | [] | {"id": 1, "description": "Goal"}, {"id": 2, "description": "Yellow Card"}, {"id": 3, "description": "Substitution"}, {"id": 1, "description": "Goal"}, {"id": 4, "description": "Substitution"}, {"id": 3, "description": "Red Card"}, {"id": 2, "description": "Goal"} | null |
168 | thrombosis_prediction | In the context of a medical database, we have a table `examination` that records various tests and diagnoses for patients over time. Each record includes a patient ID, the date of examination, and several test results including `aCL IgG`, `aCL IgM`, `ANA`, and `aCL IgA`. We are interested in finding out how many times each test result occurs in each year between 1993 and 1996. Specifically, we want to count the occurrences of each `ANA Pattern` in each year, and then find the pattern with the highest count. Here is a query that attempts to do this but fails to return only the rows with the highest count of `ANA Pattern` per year. How to get only rows with the highest count of `ANA Pattern` per year between 1993 and 1996? | [
"SELECT COUNT(\"ANA Pattern\") AS c, \"ANA Pattern\", EXTRACT(YEAR FROM \"Examination Date\") AS examination_year FROM examination WHERE EXTRACT(YEAR FROM \"Examination Date\") BETWEEN 1993 AND 1996 GROUP BY EXTRACT(YEAR FROM \"Examination Date\"), \"ANA Pattern\" ORDER BY examination_year, c DESC;"
] | [] | [] | [] | [] | null | null |
169 | european_football_2 | In the context of the 'european_football_2' database, a user is trying to generate a report that combines two parts: a list of players along with their birth year, ordered by the player's name, and a summary of the total number of players in each birth year, ordered by the count of players and the year. The user has two separate queries that work individually but when combined using UNION ALL, the order of the first part changes unexpectedly. The user wants to maintain the order of the first part when combined with the second part. | [
"WITH player_names AS (SELECT 1 AS source, ROW_NUMBER() OVER (ORDER BY player_name) AS row_number, CONCAT(player_name, '(', LEFT(birthday, 4), ')') AS dest FROM player_table), birth_year_summary AS (SELECT 2 AS source, ROW_NUMBER() OVER (ORDER BY COUNT(*), LEFT(birthday, 4)) AS row_number, CONCAT('There are a total of ', COUNT(*), ' player', CASE WHEN COUNT(*) > 1 THEN 's' ELSE '' END, ' born in ', LEFT(birthday, 4), '.') AS dest FROM player_table GROUP BY LEFT(birthday, 4)) SELECT dest FROM (SELECT * FROM player_names UNION ALL SELECT * FROM birth_year_summary) AS combined_results ORDER BY ROW_NUM;"
] | [] | [
"CREATE TABLE player_table AS SELECT * FROM player ORDER BY RANDOM();"
] | [
"DROP TABLE player_table;"
] | [] | null | null |
170 | formula_1 | In the Formula 1 database, I want to generate a report that lists each race along with the stops and ids of all constructors who participated in that race. However, when I use a query with two LEFT JOINs, I encounter duplicates in the constructor and stops. I need a solution to ensure that the constructor and stop are listed without duplicates while maintaining the correct aggregation of data. Particularly, I can't just add distinct to remove duplicates because duplicate name is allowed. | [
"SELECT rs.raceId AS race_id, string_agg(constructorId::TEXT, ',' ORDER BY res.resultId) AS constructor_ids, string_agg(p.stop::TEXT, ', ' ORDER BY p.raceId) AS stops FROM races rs LEFT JOIN results res ON res.raceId = rs.raceId LEFT JOIN pitstops p ON rs.raceId = p.raceId GROUP BY rs.raceId"
] | [] | [] | [] | [] | null | null |
171 | formula_1 | In the context of the 'formula_1' database, we have a scenario involving race routes and their events. The 'routes' table represents the race routes, and we have two additional tables, 'route_detail' and 'route_event', which track various events and their details for each route. The 'route_events' table contains records of events for each route, and the 'route_detail' table contains detailed information about each route. We need to find all routes with a specific status (e.g., status 5, which is stored in route_detail) but do not have an event of a certain type (e.g., type 3, which is stored in route_event). However, the provided SQL query does not correctly filter out the routes with the unwanted event type, leading to incorrect results. | [
"SELECT r.id, r.start_day, r.end_day, de.point_of_delivery_plant_name, de.point_of_delivery_plant_number, de.visit_status FROM route r JOIN route_detail de ON de.route_id = r.id WHERE NOT EXISTS (SELECT 1 FROM route ro JOIN route_detail rd ON rd.route_id = ro.id JOIN route_event ev ON ev.route_detail_id = rd.id WHERE rd.route_id = r.id AND ev.event_type !=3 AND rd.visit_status = '5' AND rd.id = de.id) AND de.visit_status = '5' GROUP BY 1,2,3,4,5,6 ORDER BY r.id;"
] | [] | [
"CREATE TABLE route(id INT, start_day DATE, end_day DATE);",
"INSERT INTO route VALUES (1, '2023/05/01', '2023/05/07'), (2, '2023/05/01', '2023/05/07'), (3, '2023/05/01', '2023/05/07'), (4, '2023/05/01', '2023/05/07'), (5, '2023/05/01', '2023/05/07');",
"CREATE TABLE route_detail(id INT, route_id INT, visit_status INT, point_of_delivery_plant_name VARCHAR(30), point_of_delivery_plant_number INT);",
"INSERT INTO route_detail VALUES (1, 1, 5, 'CROP SOLUTIONS S.A.', 563), (2, 1, 5, 'CROP SOLUTIONS S.A.', 563), (3, 1, 5, 'CROP SOLUTIONS S.A.', 563), (4, 2, 0, 'SAMA S.A.', 781), (5, 3, 0, 'WALTER SAMA HARMS', 732), (6, 4, 5, 'AGROSER S.A.', 242), (7, 4, 5, 'AGROSER S.A.', 242), (8, 5, 5, 'AGROFERTIL S.A.', 287), (9, 5, 5, 'AGROFERTIL S.A.', 287), (10, 5, 5, 'AGROFERTIL S.A.', 287);",
"CREATE TABLE route_event (id INT, route_detail_id INT, event_type INT, event_description VARCHAR(30));",
"INSERT INTO route_event VALUES (50, 1, 1, 'start visit'), (51, 2, 2, 'recurrent form'), (52, 3, 3, 'end visit'), (53, 4, 1, 'start visit'), (54, 5, 1, 'start visit'), (55, 6, 1, 'start visit'), (56, 7, 2, 'recurrent form'), (57, 8, 1, 'start visit'), (58, 9, 2, 'recurrent form'), (59, 10, 4, 'harvest advance')"
] | [
"DROP TABLE route_event;",
"DROP TABLE route;",
"DROP TABLE route_detail;"
] | [] | null | null |
172 | european_football_2 | We are managing a database for a football analytics platform where we track the attributes of teams over time. Each record in the 'team_attributes' table represents the attributes of a team on a specific date. We need to ensure that the 'date' field of each record is correctly associated with an 'eff_to' field, which indicates the date just before the next record for the same team. If there is no subsequent record for the team, 'eff_to' should be set to '5999-12-31'. We are trying to automate this process using a trigger function that updates the 'eff_to' field whenever a new record is inserted into the 'team_attributes' table. However, our current trigger function is incorrectly updating all 'eff_to' fields with the last 'date' value instead of calculating them individually for each team. We need to correct this issue to ensure accurate data representation. | [
"CREATE OR REPLACE FUNCTION update_team_attributes_eff_to() RETURNS TRIGGER AS $$ BEGIN UPDATE team_attributes SET eff_to = subquery.next_date FROM ( SELECT COALESCE( LEAD(TO_TIMESTAMP(date, 'YYYY-MM-DD HH24:MI:SS')::DATE, 1) OVER ( ORDER BY TO_TIMESTAMP(date, 'YYYY-MM-DD HH24:MI:SS')::DATE DESC), TO_DATE('6000-00-00', 'YYYY-MM-DD') ) - 1 AS next_date FROM team_attributes ) AS subquery; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE OR REPLACE TRIGGER after_insert_team_attributes AFTER INSERT ON team_attributes FOR EACH ROW EXECUTE PROCEDURE update_team_attributes_eff_to();"
] | [] | [
"ALTER TABLE team_attributes ADD COLUMN eff_to DATE;"
] | [
"ALTER TABLE team_attributes DROP COLUMN eff_to;"
] | [] | null | null |
173 | formula_1 | In the Formula 1 database, I need to analyze the results of a specific race to identify the drivers who finished in the same position across multiple races, excluding the driver with the highest driver ID in each group. For example, if drivers 5 and 9 both finished in 3rd place in different races, and drivers 8, 12, and 13 all finished in 2nd place in different races, I want to exclude the driver with the highest ID in each group and get the remaining driver IDs. The final result should be a list of driver IDs excluding the highest ID in each group of drivers who finished in the same position across multiple races. | [
"select position, array_agg(driverid) as driverids from results group by position having COUNT(*)>1 order by position"
] | [] | [] | [] | [] | null | null |
174 | superhero | In the superhero2 database, we have a representation of a (binary) tree where each superhero2 is a node, and each node has a parent column and a color column. The color column represents the color of the superhero2's costume. The leaf nodes (superheroes without children) have a color (their color column is not null and can be either green or red). The goal is to color the whole tree based on the following rules: If a parent has one child, then the parent's color is the child's color. If a parent has two children and both have the same color, then the parent color is its children's color. If a parent has two children and they have different colors, then the parent color is gray. The id and parent id are text and (their length - 1) is the their depth, for example, 'A' is the root node and 'AB' is the child and 'ABC' is the child of 'AB'. The id represents the path from the root to the node. If the leaf node is 'ABDE', then the path is 'A', 'AB', 'ABD', 'ABDE'. How can I write a recursive query in PostgreSQL for this algorithm? | [
"WITH RECURSIVE cte AS ( SELECT id, parent_id, color FROM superhero2 WHERE parent_id IS NULL UNION ALL SELECT s.id, s.parent_id, CASE WHEN c1.color IS NULL THEN c2.color WHEN c2.color IS NULL THEN c1.color WHEN c1.color = c2.color THEN c1.color ELSE 'gray' END AS color FROM superhero2 s LEFT JOIN cte c1 ON s.id = c1.parent_id LEFT JOIN cte c2 ON s.id = c2.parent_id ) UPDATE superhero2 SET color = cte.color FROM cte WHERE superhero2.id = cte.id"
] | [] | [
"CREATE TABLE superhero2 ( id text NOT NULL, parent_id text NULL, color text NULL, PRIMARY KEY (id) );",
"INSERT INTO superhero2 (parent_id, id, color) VALUES (null, 'A', null), ('A', 'AB', null), ('A', 'AC', null), ('AB', 'ABD', null), ('ABD', 'ABDE', 'green'), ('AC', 'ACF', null), ('AC', 'ACG', null), ('ACF', 'ACFH', 'red'), ('ACF', 'ACFI', 'green'), ('ACG', 'ACGJ', 'red'), ('ACG', 'ACGK', 'red');"
] | [
"DROP TABLE IF EXISTS superhero2;"
] | [] | null | null |
175 | superhero | We need to count the occurrences of specific superheroes in two different datasets: one dataset includes superheroes with a specific gender, and the other includes superheroes with a specific alignment. We want to return a single result set that includes the counts of these superheroes from both datasets. The challenge is to ensure that the query is optimized and that the database planner can effectively use indexes to speed up the query. The user encountered an issue where applying the filter condition after the join operation prevented the planner from using indexes, leading to a suboptimal execution plan. The user wants to avoid manually applying the filter condition to each joined table to maintain query simplicity and performance. | [
"SELECT * \nFROM (\n SELECT gender_id, COUNT(*) as cnt1 \n FROM superhero \n WHERE gender_id IN (1, 2) \n GROUP BY gender_id\n) AS c1\nFULL OUTER JOIN (\n SELECT alignment_id, COUNT(*) as cnt2 \n FROM superhero \n WHERE alignment_id IN (1, 2) \n GROUP BY alignment_id\n) AS c2\nON c1.gender_id = c2.alignment_id\nWHERE c1.gender_id IN (1,2) AND c2.alignment_id IN (1,2);"
] | [] | [
""
] | [
""
] | [] | null | true |
176 | card_games | In the context of a card games database, I have a table named 'cards' that tracks each card's details, including its artist, name, and set code. Each card can belong to multiple sets, and each set can contain multiple cards. I want to generate a list of artists with the most cards in the database. However, for a comprehensive view, I need to list all sets and the count of cards per set for each artist, alongside the set with the highest number of cards. I'm not interested in artists with only one card. The result should be ordered by the artist with the most cards first, followed by the rest of the sets for the same artist, and so on. Here's the SQL query I tried, but it doesn't fully meet my requirements: | [
"SELECT artist, setcode, COUNT(setcode) AS counter FROM cards GROUP BY setcode, artist HAVING COUNT(setcode) > 1 ORDER BY counter DESC;"
] | [] | [
""
] | [
""
] | [] | null | null |
177 | european_football_2 | In the context of the 'european_football_2' database, we have a table that tracks the lineup changes between consecutive matches for football teams. Each row in the table represents a match, and it includes the match ID, the player IDs in the current lineup, and the player IDs in the next lineup. We need to count the number of players who are scheduled to play in the next match and are already in the current lineup. For example, if a player is listed twice in the current lineup and once in the next lineup, they should be counted only once. The user attempted to write a query to achieve this but encountered issues with the logic and syntax. | [
"SELECT match_id, current_lineup, next_lineup, (SELECT COUNT(case when arr=matches then matches end) FROM unnest(current_lineup) arr CROSS JOIN unnest(next_lineup) matches WHERE arr IS NOT NULL AND matches IS NOT NULL ) FROM match_lineups;"
] | [] | [
"CREATE TABLE match_lineups (match_id Integer PRIMARY KEY, current_lineup Integer[], next_lineup Integer[]); INSERT INTO match_lineups(match_id, current_lineup, next_lineup) VALUES (101, '{1, 2, 3, 4}', '{2, 4, 5}'); INSERT INTO match_lineups(match_id, current_lineup, next_lineup) VALUES (102, '{1, 3, 5, 6}', '{3, 5, 5, 7}');"
] | [
"DROP TABLE match_lineups;"
] | [] | null | null |
178 | formula_1 | I have a table called circuit_json that contains details about Formula 1 circuits. The table has a column named circuit_id_name, which stores JSON data with information about circuit IDs and their corresponding names. I am trying to extract the circuit ID and name into separate columns for better readability. Here is my attempt and the query I used: | [
"SELECT * \nFROM jsonb_to_record(circuit_id_name) AS x(circuitid int, name text) \nFROM circuit_json;"
] | [] | [
"CREATE TABLE circuit_json AS SELECT json_build_object('circuitid', circuitid, 'name', name) as circuit_id_name FROM circuits;"
] | [
"DROP TABLE circuit_json;"
] | [] | null | null |
179 | superhero | We have a table `hero_attribute` that stores the attribute values of superheroes. Each superhero can have multiple attributes, such as Intelligence, Strength, and Speed. The data in `hero_attribute` includes the `hero_id`, `attribute_id`, and `attribute_value`. We want to insert this data into another table `hero_attribute_bifurcation` where each attribute value greater than 50 is split into multiple rows of 50 and the remaining amount (if any) is inserted as a separate row. The new table `hero_attribute_bifurcation` should include the columns `hero_id`, `attribute_id`, `attribute_value`, and `is_bifurcated` indicating whether the attribute value was split (1) or not (0). The inserted data set should reflect this bifurcation logic. | [
"INSERT INTO hero_attribute_bifurcation (hero_id, attribute_id, attribute_chunk, is_bifurcated)\nSELECT \n hero_id, \n attribute_id, \n attribute_value, \n CAST(attribute_value > 100 AS integer) AS is_bifurcated\nFROM hero_attribute;"
] | [] | [
"CREATE TABLE hero_attribute_bifurcation (hero_id bigint, attribute_id bigint, attribute_chunk bigint, is_bifurcated integer);"
] | [
"DROP TABLE hero_attribute_bifurcation;"
] | [] | null | null |
180 | financial | I need to get a list of all districts from the account table. For each district, I want two arrays of loan IDs from the loan table. The first array should include loan IDs where the status is either 'A' or 'B', and the second array should include loan IDs where the status is 'C'. The results should be grouped by district_id and ordered by district_id. | [
"SELECT y.district_id,\n CASE WHEN y.status IN ('A', 'B') THEN array_agg(loan_id) END AS type_A_B,\n CASE WHEN y.status = 'C' THEN array_agg(loan_id) END AS type_C\nFROM (\n SELECT x.district_id, l.loan_id, l.status\n FROM loan l\n JOIN account x ON l.account_id = x.account_id\n) y\nGROUP BY 1, y.status\nORDER BY 1;"
] | [] | [
""
] | [
""
] | [] | null | null |
181 | superhero | In the superhero database, we have a timeseries of superhero activities recorded at 5-minute intervals. Each activity is associated with a specific superhero and an activity code. We need to aggregate these activities so that consecutive activities (over several continuous intervals) by the same superhero with the same activity code are grouped together, and the interval is summed up. The goal is to produce a list of valid intervals for each superhero and activity code combination. | [
"SELECT ts, superhero_id, activity_code, LEAD(ts) OVER (PARTITION BY superhero_id, activity_code ORDER BY ts) as next_ts FROM superhero_activities"
] | [] | [
"CREATE TABLE superhero_activities ( ts timestamptz, activity_code bigint, superhero_id bigint );",
"INSERT INTO superhero_activities VALUES ('2023-03-01 12:00:00', 1, 1), ('2023-03-01 12:05:00', 1, 1), ('2023-03-01 12:10:00', 1, 1), ('2023-03-01 12:10:00', 2, 1), ('2023-03-01 12:25:00', 1, 1), ('2023-03-01 12:30:00', 1, 1), ('2023-03-01 12:00:00', 1, 2), ('2023-03-01 12:20:00', 1, 2), ('2023-03-01 12:20:00', 3, 2), ('2023-03-01 12:25:00', 3, 2);"
] | [
"DROP TABLE superhero_activities;"
] | [] | null | null |
182 | card_games | I am working on a project to analyze card game tournaments and their participants. I have three tables: 'card_players', 'card_matches', and 'tournaments'. The 'card_players' table contains information about the players, the 'card_matches' table contains information about the matches played, and the 'tournaments' table contains information about the tournaments. I want to count how many tournaments have had at least one match where a player of type 'Pro' has participated. However, if both players in a match are of type 'Pro', I only want to count one tournament. Here's a simplified SQL of my database schema and some seed data. The problematic SQL query I used is provided below, and it counts both players in a match as separate tournaments, which is incorrect. I need a corrected query that counts each tournament only once if there is at least one 'Pro' player in any match. | [
"SELECT count(*) FROM card_matches INNER JOIN card_players ON card_matches.player1_id = card_players.id OR card_matches.player2_id = card_players.id WHERE card_players.type = 'Pro'"
] | [] | [
"CREATE TABLE card_players (id bigint NOT NULL, name text NOT NULL, type text NOT NULL, PRIMARY KEY(id));",
"CREATE TABLE card_matches (id bigint NOT NULL, tournament_id bigint NOT NULL, player1_id bigint NOT NULL, player2_id bigint NOT NULL, PRIMARY KEY(id));",
"CREATE TABLE tournaments (id bigint NOT NULL, name text NOT NULL, PRIMARY KEY(id));",
"INSERT INTO card_players (id, name, type) VALUES (1, 'Alice', 'Pro'), (2, 'Bob', 'Pro'), (3, 'Charlie', 'Amateur'), (4, 'David', 'Amateur');",
"INSERT INTO tournaments (id, name) VALUES (1, 'Grand Prix'), (2, 'Pro Tour'), (3, 'Super Bowl');",
"INSERT INTO card_matches (id, tournament_id, player1_id, player2_id) VALUES (1, 1, 1, 3), (2, 1, 2, 4), (3, 2, 1, 2), (4, 3, 3, 4), (5, 2, 3, 4);"
] | [
"DROP TABLE card_players;",
"DROP TABLE card_matches;",
"DROP TABLE tournaments;"
] | [] | null | null |
183 | european_football_2 | In the context of the European Football database, I have a set of matches where each match has a related home team, away team, season, and date. Most matches have been played several times during a season. In some cases, several matches took place at the same day and in the same league. I'd like to have one group (a match series) for all matches that have been played together during one season in one specific league, something like this:
| League ID | Season | Match IDs(a list of match ids) | Dates(a list of match dates)
I've been doing the heavy lifting in Python, but the code is quite slow and involves lots of queries. I've been wondering if any of you has ideas to combine the matches in one (or a few) queries?\nI have had no success doing this in Postgres so far. | [
"WITH all_dates AS (SELECT league_id, season, ARRAY_AGG(DISTINCT id order by id) as match_ids, date AS match_date FROM match WHERE season IS NOT NULL AND league_id IS NOT NULL AND id is not null AND date IS NOT NULL GROUP BY league_id, season, date) SELECT DISTINCT league_id, season, match_ids, ARRAY_AGG(DISTINCT all_dates.match_date ORDER BY match_date) AS dates FROM all_dates GROUP BY season, league_id, match_ids order by league_id;"
] | [] | [] | [] | [] | null | null |
184 | card_games | We have a dataset of card rulings in the 'ruling' table, where each ruling is associated with a unique card UUID. We need to transform this dataset into a format where each row represents a unique ruling id, and each column represents a different card UUID. The values in the cells should be the ruling text for that card. For each unique id, group all rules by card_uuid (e.g., card1, card2, card3). Assign a row number starting from 1 for each card_uuid grouping, and place the corresponding rule in the appropriate column. If a card_uuid doesn't have a rule for an id, insert NULL. The final output should have one row per id, with columns for each card_uuid and the corresponding rules or NULLs. The expected output should have ruling ids as rows and card UUIDs as columns, with ruling texts as the cell values. There are 3 uuids:'5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c' as card1, '56f4935b-f6c5-59b9-88bf-9bcce20247ce' as card2, '6d268c95-c176-5766-9a46-c14f739aba1c' as card3.
The expected output is like | id | card1 ruling | card2 ruling | card3 ruling |
However, when attempting to use the crosstab function, we encountered an error. Here is the problematic query we tried: | [
"select * from crosstab ('select id, uuid, text from ruling order by 1,2') AS final_result(id int, uuid1 text, uuid2 text, uuid3 text);"
] | [] | [
"CREATE TABLE ruling ( id integer, text text NULL, uuid text NULL);",
"INSERT INTO ruling (id, text, uuid) VALUES (1, 'First ruling text', '5f8287b1-5bb6-5f4c-ad17-316a40d5bb0c'), (2, 'Second ruling text', '56f4935b-f6c5-59b9-88bf-9bcce20247ce'), (2, 'Second ruling text 2', '56f4935b-f6c5-59b9-88bf-9bcce20247ce'), (2, 'Second ruling text 3', '56f4935b-f6c5-59b9-88bf-9bcce20247ce'), (3, 'Third ruling text', '6d268c95-c176-5766-9a46-c14f739aba1c');"
] | [
"DROP TABLE ruling;"
] | [] | null | null |
185 | card_games |
In the card_games database, there are two tables: cards_info and card_choice. The cards_info table contains information about various Magic: The Gathering cards, including their IDs and names. The card_choice table contains information about the customers' selections of different card types, including the card's ID and a JSONB field that lists the IDs of corresponding non-valid cards. These non-valid cards are the ones that a customer has deemed unsuitable or irrelevant to their selection, represented as an array of card IDs. The goal is to query the cards_info table and return the cards that are not listed in the non_valid_cards array for each card choice, based on the matching card_id.
| [
"\nSELECT c.* \nFROM cards_info c \nWHERE NOT EXISTS (\n SELECT 1 \n FROM card_choice cc \n WHERE cc.card_id = c.id \n AND c.id = ANY (cc.non_valid_cards)\n);\n\n"
] | [] | [
"\nCREATE TABLE cards_info (\n id SERIAL PRIMARY KEY,\n name VARCHAR(255) NOT NULL\n);\nINSERT INTO cards_info (id, name)\nVALUES\n (1, 'Ancestor`s Chosen'),\n (2, 'Angel of Mercy'),\n (3, 'Aven Cloudchaser');\n \nCREATE TABLE card_choice (\n id SERIAL PRIMARY KEY,\n card_id INT REFERENCES cards_info(id),\n non_valid_cards JSONB\n);\nINSERT INTO card_choice (id, card_id, non_valid_cards)\nVALUES\n (1, 1, '[1,3]'),\n (2, 3, '[1]'),\n (3, 2, '[2,3]');\n"
] | [
"DROP TABLE IF EXISTS cards_info; DROP TABLE IF EXISTS card_choice;"
] | [] | (1, 1, '[1,3]'), (2, 3, '[1]'), (3, 2, '[2,3]'); | null |
186 | card_games |
I have a certain hierarchy of data in the card_type table, where each row represents a type of cards with a unique identifier (uuid), a type name (card_name), and a reference to its parent card through the parent_uuid. The data is structured in a way that cards can be grouped under parent cards, forming a tree-like hierarchy.
I initially managed to create a recursive query that fetches the data, but the result isn't in the format I desire. The query correctly returns each card along with a list of parent uuids. However, instead of having the list of parent uuids, I would prefer to have a structured output where each parent card includes a list of its child cards (i.e., uuid and card_name).
For example, I want to convert the result into a structure where each parent card lists all its direct child cards grouped together, forming a tree-like structure. This would help me better visualize the hierarchy and relationships between the cards, with each parent card having an array of its children's uuid values.
Can you guide me on how to achieve this transformation using SQL?
| [
"\nWITH RECURSIVE nodes AS (\n SELECT\n uuid,\n card_name AS name,\n ARRAY[]::uuid[] AS parents\n FROM card_type\n WHERE parent_uuid IS NULL\n\n UNION ALL\n\n SELECT\n c.uuid,\n c.card_name AS name,\n nodes.parents || c.uuid\n FROM card_type c\n JOIN nodes ON nodes.uuid = c.parent_uuid\n)\nSELECT * FROM nodes;\n"
] | [] | [
"\nCREATE TABLE card_type\n(\n uuid uuid NOT NULL PRIMARY KEY,\n card_name character varying(32),\n parent_uuid uuid REFERENCES card_type(uuid)\n);\n\nINSERT INTO card_type (uuid, card_name, parent_uuid) \nVALUES \n('8a70180b-3644-4b17-af5f-93cbe0090cce', 'Creature', null),\n('d9093660-241a-48f6-bf09-b6a8c6c7f12a', 'Human', '8a70180b-3644-4b17-af5f-93cbe0090cce'),\n('376ae1cb-425d-44d2-b19a-19b6f1e86314', 'Cleric', 'd9093660-241a-48f6-bf09-b6a8c6c7f12a'),\n('5d5f174a-5c8e-4d12-912f-8173e255e35a', 'Knight', 'd9093660-241a-48f6-bf09-b6a8c6c7f12a'),\n('f79f5fa0-6eaf-465b-9b14-e3b49c5ac9ef', 'Enchantment', null),\n('8aa95eda-7963-40ef-be44-076cdf06c5c1', 'Aura', 'f79f5fa0-6eaf-465b-9b14-e3b49c5ac9ef');\n"
] | [
"DROP TABLE IF EXISTS card_type;"
] | [] | null | null |
187 | student_club | In the context of the student_club database, we have two tables: `event` and `budget`. The `event` table contains information about various events, including their start dates and statuses. The `budget` table contains financial details related to these events, including the amount budgeted and the remaining budget. The user wants to know the average remaining budget and the number of events that are open or closed on a daily basis between '2020-01-01' and '2020-03-31'. The user attempted to write a query to achieve this, but it resulted in incorrect results or errors. Below is the problematic SQL statement the user used, followed by the corrected solution. | [
"SELECT d.the_day AS \"Date\", COUNT(e.event_id) AS \"Number of Events\", AVG(b.remaining) AS \"Avg Remaining Budget\" FROM (SELECT ts::date AS the_day FROM generate_series (timestamp '2020-01-01', timestamp '2020-03-31', interval '1 day'::interval) ts) d LEFT JOIN \"event\" e ON e.event_date::date = d.the_day LEFT JOIN budget b ON b.link_to_event = e.event_id GROUP BY d.the_day ORDER BY d.the_day;"
] | [] | [] | [] | [] | null | null |
188 | financial | I log the daily transactions of my bank accounts. Now I want to create a SQL statement to get the sum of transaction amounts for each month but separate columns for each year. I came up with the following SQL statement: sql SELECT LPAD(extract (month from trans.date)::text, 2, '0') as month, sum(trans.amount) as 1998 from trans WHERE trans.date >= '1998-01-01' and trans.date < '1999-01-01' group by month order by 1; This results in only getting the values from 1998: | month | a1998 | | -------- | -------------- | | 1 | 100 | | 2 | 358 | | 3 | 495 | How could I change the SQL statement to get new columns for each year? Is this even possible? | [
"SELECT LPAD(extract (month from trans.date)::text, 2, '0') as month, sum(trans.amount) as a1997 from trans WHERE trans.date >= '1997-01-01' and trans.date < '1998-01-01' group by month order by 1;"
] | [] | [] | [] | [] | null | null |
189 | formula_1 | I'm trying to run a query that will find circuits within a given distance of any of the selected locations. This is for a search result where users can select multiple locations to look around. My current approach is to use `ST_ClosestPoint` and pass in an array of `ST_Point` generated in PHP. Then I pass that string into `ST_Collect`. However, this doesn't work because it looks like `ST_ClosestPoint` doesn't like these mixed arguments. I have a `gist(coordinate::geography)` index on `circuits` which seems like it would be useful to use. What am I missing - is there a better way to do this, or is this a bad approach? Should I be performing the query each time with a different location? | [
"SELECT * FROM circuits WHERE ST_DWithin(ST_SetSRID(ST_MakePoint(lng, lat), 4326)::geography, ST_ClosestPoint((ST_MakePoint(lng, lat), 4326)::geography, ST_Collect(Array[ST_SetSRID(ST_MakePoint(2.76083, 101.73800), 4326)::geography, ST_SetSRID(ST_MakePoint(26.03250, 50.51060), 4326)::geography])), 1000000, FALSE);"
] | [] | [
"CREATE EXTENSION postgis;"
] | [] | [] | null | null |
190 | formula_1 | In the Formula 1 database, I have a results table currently containing around 400 million entries and the following query on it:
The query retrieves the top 10 result IDs for a specific driver or another driver, ordered by the result ID in descending order. The query works fine when filtering by a single driver ID, but it takes several minutes when filtering by two driver IDs, even if both drivers have a small number of results (e.g., 2 for the first driver and 57 for the second driver). The issue seems to be related to the combination of ORDER BY and LIMIT clauses. | [
"SELECT resultid FROM results WHERE driverid = 3 OR driverid = 4 ORDER BY resultid DESC LIMIT 10"
] | [] | [] | [] | [] | null | true |
191 | card_games | In a multiplayer card game platform, when a player is misbehaving, their 'muted' status is set to true. Chat messages from muted players should be hidden from all other players except the muted player themselves to prevent them from noticing their muted status and creating new accounts. The platform uses PostgreSQL 14.2 and has the following tables: 'words_users', 'words_social', 'words_games', and 'words_chat'. The user has prepared a test case with two users, one of whom is muted, and a game where both users have exchanged messages. The user's current SQL function to retrieve chat messages does not filter out messages from muted players for other players. The user seeks to modify the SQL function to hide messages from muted players for everyone except the muted player themselves. | [
"CREATE OR REPLACE FUNCTION words_get_chat(in_gid integer, in_social integer, in_sid text) RETURNS TABLE (out_mine integer, out_msg text) AS $func$ SELECT CASE WHEN c.uid = s.uid THEN 1 ELSE 0 END, c.msg FROM words_chat c JOIN words_games g USING (gid) JOIN words_social s ON s.uid IN (g.player1, g.player2) WHERE c.gid = in_gid AND s.social = in_social AND s.sid = in_sid ORDER BY c.CREATED ASC; $func$ LANGUAGE sql;",
"SELECT words_get_chat(10, 100, 'abc') AS nice_user;"
] | [] | [
"CREATE TABLE words_users (uid SERIAL PRIMARY KEY, muted BOOLEAN NOT NULL DEFAULT false);",
"CREATE TABLE words_social (sid text NOT NULL CHECK (sid ~ '\\S'), social integer NOT NULL CHECK (0 < social AND social <= 256), given text NOT NULL CHECK (given ~ '\\S'), uid integer NOT NULL REFERENCES words_users ON DELETE CASCADE, PRIMARY KEY(sid, social));",
"CREATE TABLE words_games (gid SERIAL PRIMARY KEY, player1 integer REFERENCES words_users(uid) ON DELETE CASCADE NOT NULL CHECK (player1 <> player2), player2 integer REFERENCES words_users(uid) ON DELETE CASCADE);",
"CREATE TABLE words_chat (cid BIGSERIAL PRIMARY KEY, created timestamptz NOT NULL, gid integer NOT NULL REFERENCES words_games ON DELETE CASCADE, uid integer NOT NULL REFERENCES words_users ON DELETE CASCADE, msg text NOT NULL);",
"INSERT INTO words_users (uid, muted) VALUES (1, false), (2, true);",
"INSERT INTO words_social (sid, social, given, uid) VALUES ('abc', 100, 'Nice user', 1), ('def', 200, 'Bad user', 2);",
"INSERT INTO words_games (gid, player1, player2) VALUES (10, 1, 2);",
"INSERT INTO words_chat (gid, uid, created, msg) VALUES (10, 1, CURRENT_TIMESTAMP + INTERVAL '1 min', 'Hi how are you doing?'), (10, 1, CURRENT_TIMESTAMP + INTERVAL '2 min', 'I am a nice user'), (10, 2, CURRENT_TIMESTAMP + INTERVAL '3 min', 'F*** ***!!'), (10, 2, CURRENT_TIMESTAMP + INTERVAL '4 min', 'I am a bad user'), (10, 1, CURRENT_TIMESTAMP + INTERVAL '5 min', 'Are you there??');"
] | [
"DROP TABLE words_users;",
"DROP TABLE words_social;",
"DROP TABLE words_games;",
"DROP TABLE words_chat;"
] | [] | null | null |
192 | formula_1 | A Formula 1 team is analyzing the performance and financial impact of their drivers in various races. They need to calculate the total earnings for each driver based on their points and a bonus system. The bonus is calculated as follows: if the total earnings exceed 5000 points, a 20% bonus is applied; if the total earnings exceed 3000 points, a 15% bonus is applied. The team wants to avoid repeating the calculation of total earnings multiple times in their query. | [
"SELECT driverid, points, (points * 100) as earnings, CASE WHEN (points * 100) > 5000 THEN (points * 100) * 0.2 WHEN (points * 100) > 3000 THEN (points * 100) * 0.15 ELSE null END AS bonus FROM driverstandings"
] | [] | [] | [] | [] | null | null |
193 | european_football_2 | The data returned as `dataset` in the CTE below represents the number of times team attributes were recorded for each date within a specific time frame. The dataset looks like this:
| date | rows_added |
How can I incorporate a count of the duplicate records, by date, in the following CTE? If I was going to only count the duplicate dates I would use the following but I can't incorporate it into the CTE above:
sql
SELECT date, COUNT(date)
FROM dataset
GROUP BY date
HAVING COUNT(date) >1
Desired output given the example above:
|total_days_in_result_set | total_days_w_distinct_record_counts | toal_days_w_duplicate_record_counts | duplicate_dates |
| [
"with dataset as (\n SELECT \n date,\n COUNT(*) as rows_added\n FROM\n team_attributes\n WHERE \n date between '2010-01-01 00:00:00'\n AND '2015-12-31 00:00:00'\n GROUP BY \n date\n )\nSELECT\n COUNT(*) as total_days_in_result_set,\n COUNT(DISTINCT rows_added) as total_days_w_distinct_record_counts,\n COUNT(*) - COUNT(DISTINCT rows_added) as toal_days_w_duplicate_record_counts\nFROM dataset"
] | [] | [] | [] | [] | null | null |
194 | card_games | I'm trying to handle an array of counters column in Postgres for a card collection tracking system. For example, let's say I have this table:
| card_name | counters | where counters is a list of numbers like [1,2,3,4] and now I'm adding 2 values [2,0,2,1] and [1,3,1,0]).
I expect the query to sum between the 2 counters vectors on conflict ([1,3,1,0] + [2,0,2,1] = [3,3,3,1]).
The expected counters are [3,3,3,1]. I had a try but it didn't seem to work, what am I missing? | [
"insert into card_counters (card_name, counters) values ('Lightning Bolt', array[2,0,2,1]) on conflict (card_name) do update set counters = array_agg(unnest(card_counters.counters) + unnest(array[2,0,2,1]))"
] | [] | [
"CREATE TABLE card_counters (card_name text PRIMARY KEY, counters integer[]);",
"INSERT INTO card_counters (card_name, counters) VALUES ('Lightning Bolt', array[1,3,1,0]);"
] | [
"DROP TABLE card_counters;"
] | [] | null | null |
195 | erolp | In the erolp database, we have a table named sales_transactions that records the transaction details of a financial application. Each transaction has an id, a transaction amount (trx), an event multiplier (event), and a desired result (result_good) which is the accumulation of the transaction amounts and previous results, multiplied by the event multiplier. The user is trying to calculate the 'result_good' column based on the recursive relationship between the rows. The user has attempted various methods including arrays, lateral views, recursive views, custom functions, and variables but has not been successful. The expected output is | id | result | and result is retained to 6 decimal places | [
"select id, round(prev + event * sum(prev) over(order by id range between unbounded preceding and 1 preceding)::numeric(10, 6), 6) as not_quite_my_tempo from (select *, event*sum(trx) over(order by id) as prev from sales_transactions) t order by id"
] | [] | [
"CREATE TABLE sales_transactions (id int, trx int, event numeric(10,5), result_good numeric(10,6));",
"INSERT INTO sales_transactions (id, trx, event, result_good) VALUES (1, 20, 0.1, 2.000000), (2,-10, 0.1, 1.200000), (3, 20,-0.1,-3.320000), (4,-10, 0.1, 1.988000), (5, 20, 0.1, 4.186800), (6,-10,-0.1,-3.605480), (7, 20, 0.1, 5.244932);"
] | [
"DROP TABLE IF EXISTS sales_transactions;"
] | [] | null | null |
196 | california_schools | We have a dataset of schools in California, and we are interested in analyzing the distribution of schools based on their funding type. We have created a Common Table Expression (CTE) named cte_funding_count that contains the count of schools for each funding type. The CTE looks like this:
| fundingtype | count |
From this CTE, we want to calculate the percentage of the count compared to the sum of the count as a new third column, and we want the percentage without decimals. However, when we do that, we get a sum of the percent column that is not exactly 100 due to rounding. How do we avoid this? | [
"WITH cte_funding_count AS (SELECT fundingtype, COUNT(*) AS count FROM schools GROUP BY fundingtype) SELECT fundingtype, count, ROUND(count*100/(SELECT SUM(count) FROM cte_funding_count),0) AS percent FROM cte_funding_count"
] | [] | [] | [] | [] | null | null |
197 | thrombosis_prediction | In the thrombosis_prediction database, I need to find the first laboratory test activity for each patient that occurred between the patient's first recorded data date and the date of their first hospital admission. The laboratory table contains the test dates, and the patient table contains the first recorded data date and the admission date. The patient's ID is the common key between the two tables. I want to retrieve the date of the first laboratory test activity and patient id for patients with sex F. | [
"SELECT lab.date AS firstActivity, pat.id FROM patient pat JOIN laboratory lab ON lab.id = pat.id AND lab.date <= pat.description AND lab.date > pat.\"First Date\" WHERE pat.sex='F' order by pat.id"
] | [] | [] | [] | [] | null | null |
198 | card_games | In the card_games database, I have a table (cards) that contains an id column (id) and another column (keywords) that contains an array of strings. I have a select query (SelectQuery) that gets me an id that matches cards.id, as well as an array of values (RemoveKeywords). I would like to now remove from the keywords array, any strings that are contained in the RemoveKeywords array that match on id. If the array is empty, the output return is []. For example, given cards (1, ['test']) and selectquery (1, ['test']), the output is (1, []) but not none | [
"select id, array_agg(elem) from cards, unnest(string_to_array(cards.keywords, ',')::text[]) elem where elem <> all(SELECT unnest(sq.RemoveKeywords) from SelectQuery sq) and id in (61, 65) group by id order by id;"
] | [] | [
"CREATE TABLE SelectQuery (id bigint, RemoveKeywords text[]);",
"INSERT INTO SelectQuery (id, RemoveKeywords) VALUES (65, ARRAY['Flying']), (61, ARRAY['Landwalk']);"
] | [
"DROP TABLE SelectQuery;"
] | [] | null | null |
199 | toxicology | We have a table named "Experiment" in the toxicology database that records the hourly measurements of various chemical reactions over a period of 2009 to the present. Each record includes a timestamp and the concentration levels of different chemicals such as Chlorine (CL) and Carbon (C). We need to aggregate (average) the concentration levels across different intervals from specific timepoints, for example, data from 2021-01-07T00:00:00.000Z for one year at 7 day intervals, or 3 months at 1 day interval or 7 days at 1h interval etc. The date_trunc() function partly solves this, but rounds the weeks to the nearest Monday, e.g. the following query returns the first time series interval as 2021-01-04 with an incorrect count: | [
"SELECT date_trunc('week', \"TIMESTAMP\") AS week, count(*), AVG(\"CL\") AS cl, AVG(\"C\") AS c FROM \"Experiment\" WHERE \"TIMESTAMP\" >= '2021-01-07T00:00:00.000Z' AND \"TIMESTAMP\" <= '2022-01-06T23:59:59.999Z' GROUP BY week ORDER BY week ASC"
] | [] | [
"CREATE TABLE \"Experiment\" (\"TIMESTAMP\" timestamp NOT NULL, \"CL\" numeric NOT NULL, \"C\" numeric NOT NULL);",
"INSERT INTO \"Experiment\" (\"TIMESTAMP\", \"CL\", \"C\") VALUES ('2021-01-07 00:00:00', 10.0, 5.0), ('2021-01-07 01:00:00', 11.0, 6.0), ('2021-01-14 00:00:00', 9.0, 4.0), ('2021-01-14 01:00:00', 10.0, 5.0), ('2021-01-21 00:00:00', 8.0, 3.0), ('2021-01-21 01:00:00', 9.0, 4.0);"
] | [
"DROP TABLE \"Experiment\";"
] | [] | null | null |