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…
Tag: sql
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…
SQL best practice/performance when inserting into a table. To use a temp table or not
I have a select query that’s come about from me trying to remove while loops from an existing query that was far too slow. As it stands I first select into a temp table. Then from that temp table I insert into the final table using the values from the temp table. Below is a simplified example of the flo…
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…
PowerShell code to script out all SQL Server Agent jobs into a single file
I was trying to script out all the SQL Server Agent jobs for category ‘Data Warehouse’ into a single file I was able to do it using PowerShell, where every single job creates a single file. But I need one file for all the SQL Server Agent jobs under category ID = 100 (or Category : = ‘Data W…
Google BigQuery SQL: How to fill in gaps in a table with dates?
Data Table: Dates Customer Subscription 20/02/2020 A free 21/02/2020 A free 22/02/2020 A free 23/02/2020 B free 23/03/2020 A full 01/03/2020 B full 01/03/2020 A full 02/03/2020 A full Need to fill gaps in dates by the value in the previous date Output: Dates Customer Last Subscription 20/02/2020 A free 21/02/…