October 22, 2024
Chicago 12, Melborne City, USA

SQL

Structured Query Language (SQL) is a language for querying databases. Questions should include code examples, table structure, sample dat

SQL

SQL “EXCEPT” vs “WHERE NOT IN”

I would expect the following two queries to yield identical results; however, I have several instances where the first is empty while the second returns several hundred rows. How is this possible? Working in Azure Synapse using T-SQL SELECT [value] FROM table_a WHERE [value] NOT IN (SELECT [value] FROM table_b) SELECT [value] FROM table_a EXCEPT

Read More
SQL

Big query multi-statement with a declare

I’m trying to UNION ALL requests (one request by quarter). I want to save this request as a view however I can’t because the declare of date array return a first result and so I’ve multi-statement (I only want to have the second result). I don’t understand why. Can you help me please ? DECLARE

Read More
SQL

How can I do complex calculations in MYSQL?

How can I find all rows in a table of data, which are relative to the result of some calculation between data from rows in another table? (As an example, for an eshop, I want to find all products (rows) in a table of products, which are having a price (column in another table) that

Read More
SQL

What is the best way to handles comments related to multiple tables in SQL?

I have a SQL structure that could be illustrated like so (simplified): Parts, Variants, Machines, Persons. Each have different relations, e.g.; 1 Part to many Variants Many Parts to Many Machines Many Persons to Many Parts I need to be able to add comments to each entry in either one of these tables. So one

Read More
SQL

Get all missing records with conditions on other two related tables

Suppose three tables: Table A has_many :table_b Table B belongs_to :table_a belongs_to :table_c Table C has_many :table_b Table B and Table C have each a boolean attribute has an boolean attribute called "active" How do I write a SQL statement or Active record expression where I get all the Table A records that don’t have

Read More
SQL

Computing 90th percentile manually vs function doesn't produce identical results, why?

So, I didn’t know originally that there was a percentile_cont function and I was trying to computer the 90th percentile of a number zeries by looking up the z-score (1.28155) and just doing the math. But I get ever more divergent numbers between my implementation and the built in function. Is this because of the

Read More
SQL

MERGE RETURNING into temp table without a CTE

Is something like this possible without having to use WITH? Can CREATE TEMP TABLE temp AS be used instead of creating the temp table manually? CREATE TEMP TABLE temp ( action text, winery_id int, brand text ); MERGE INTO wines w USING wines_updates u ON u.winery_id = w.winery_id WHEN MATCHED THEN UPDATE SET stock =

Read More
SQL

MERGE RETURNING into temp

Is something like this possible without having to use WITH? Can CREATE TEMP TABLE temp AS be used instead of creating the temp table manully? CREATE TEMP TABLE temp ( action text, winery_id int, brand text ); MERGE INTO wines w USING wines_updates u ON u.winery_id = w.winery_id WHEN MATCHED THEN UPDATE SET stock =

Read More
SQL

Check value between multiple rows in SQL

I need to write a query to output a table with 2 values – number & description. Table 1 has 2 columns – id and date id date 101 04-07-2018 102 15-11-2018 103 25-01-2019 104 28-06-2019 Table 2 has 3 columns – start date, end date and value start date end date value 29-11-2016 28-11-2017

Read More
SQL

(Spring Data Jpa) How to set a database to all repositories except one which I'd like to set other database

I have a large project, and I inserted a new entity with a Jpa Repository, I’d like this specific entity use a specific database unlike the one I was using for the others repositories, setting basePackages to primary database is not an option because the main repositories are randomly splitted along 10k of packages in

Read More