Skip to content

Tag: sql

How to re-use result from a SELECT statement?

My goal is to re-use the result from a SELECT statement to be used in SQL EXISTS statement. The general idea looks like this: The actual SQL statement I am trying to reduce: Answer This query: Doesn’t really make sense. It is saying to return all rows if col = 1 is in the table — but then it filte…

Sort full join based on two columns on two different tables

I have two tables in my PostgreSQL database (Table A and Table B). Both of these tables have a createdAt column. I want to do a full join on these two tables and then sort the result based on createdAt values on both A and B tables. Below is an example of what I want to be my query result.

SQL Server – grab part of string after a value sequence

I have a table called Note with a column named Notes. It has font info in the beginning which I don’t need. I’ve created a new column name final_notes and would like to grab everything after the “fs” plus two characters. The final result would be Answer We use PATINDEX to find the firs…

How to fix SELECT statement to return duplicates

Currently I am trying to return a three table join to find duplicate track titles that are in my a “track” table, and also return the track number | cd title from the other joined tables. My select statement is currently returning all the information from the joined tables but its not showing only…

Problem in SQL query #1066 – Not unique table/alias

I have a problem with this query: I get this error: 1066 – Not unique table/alias: ‘code’ Answer The error seems pretty clear. You have mentioned code three times in the from clause. What does code refer to? In your case, I think the solution is to remove all the references. The first should…

How to skip NULL (or 0) when using Row_Number?

I need to return the sequential number of a row based on values in ColValue. I need to do it until NULL occurs in a row, then start to count from the beginning. My original table is in the left picture, what I need is on the right (column RowNr): [ I tried various combinations of ROW_NUMBER but in vain.

insert extra rows in query result sql

Given a table with entries at irregular time stamps, “breaks” must be inserted at regular 5 min intervals ( the data associated can / will be NULL ). I was thinking of getting the start time, making a subquery that has a window function and adds 5 min intervals to the start time – but I only…