Skip to content

Tag: sql

Grouping by multiple columns in a range in postgres

I have a couple tables (pega.race exists too we just don’t need any of the data for this) What I want is to get an average result for each combination (permutation?) of speed/strength/wind/lightning/water/fire in a range. So from 0-2.25, 2.25-4.5, 4.5-6.75, and 6.75-9. They have to be within 0-9. No val…

SQL: use CASE with AS

I’m working on a long SQL request and I want to use CASE inside but I don’t know how to write it. My request looks like : I want to add something like : I tried to add this at the end but it didn’t work : My goal is to edit key along its value Answer You can do

SQL JOIN tables and and sum columns with the same PK

Here are my tables: Players Name DoB Ranking Ben 01/01/1999 1 Tom 02/01/1999 1 Sam 03/01/1999 1 Sam 03/01/1999 2 Ranking Ranking Points 1 100 2 50 I want to join the tables and add the total points for each player then output this table. Output Name DoB Points Ben 01/01/1999 100 Tom 02/01/1999 100 Sam 03/01/1…

Optimizing select union select oracle

I had an interview recently and the recruiter told me to make a query on a table USERS with two fields (name, age) which should return a list with two columns | NAME | MAJOR OR MINOR | My response was this : Then, he told me that is correct, but we can do better! So, my question is: How

Postgres: Where value equal decimal value

My table looks like this: When the WHERE clause is equal to value with decimal point I get no valid output like: Output: transponder_id = None When the query is for integer value, query works correctly: Output: transponder_id = 1009 Also using BETWEEN query returns expected values Output: transponder_id = 101…

Using the function SPLIT_PART twice in PostgreSQL?

I have a TEXT column where each text is formatted as such: /customers/{customer_id}/views/{id1}~{id2}/ I am trying to fetch the id2 only. My idea is how to split the string by the / character first, where I will have: customers, {customer_id}, views, {id1}~{id2}. And then, get the last position: {id1}~{id2}. …