Say that you need to query some data and that there are three fields like the following (this is part of a larger query): Field1, Field2, Field3. So you select them like this: I need to compare these values and return the variable Result that is computed as follows: 0 if they are all the same 1/2 if two are
Tag: subquery
Selecting single row if it matches another
I have a scenario of this type: I want to select only one row in such a scenario. How can I achieve this? Answer You could use distinct on with least() and greatest(): An alternative is not exists: Finally: if all of the rows are duplicated, then a simple where condition is sufficient:
A question about a correlated subquery in SQL and AVG function
We need to query the DB for students whose points are greater than the average for the uni they go to. Now, in the handbook, it is used with GROUP BY clause, but I fail to understand why one is needed here at all if the universities are already grouped in the inner query’s WHERE condition. Could you someone please
Query Optimization in SQL in given case
These are the given tables: Question is: Write a optimized query to find out the name of all the customerIDs who ordered coffee that originated from ‘Costa Rica’ or ‘Indonesia’. My attempt: My teacher says it can be optimized more but I don’t see any way. Please Help me out. Answer Rather than these nested in subqueries, I would recommend
How to substract 2 columns from different table with using count function? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 1 year ago. Improve this question Currently, I am using count function to calculate the value for 2 columns and wanna substract the 2 columns and export to excel file. This
Select rows in left join which depend on sum of a field in the other table?
Im trying to write a SQL left outer join query where the left rows are selected based on the sum of a field in rows in the other (right) table. The other table has an id field that links back to the …
SQL Join that finds non matches
I have two tables “SellerDetails” and “ANL” and both have key fields of “Seller Ref No” and “Sales Year”. This giving me where there is a match on Seller Ref …
More than one row returned by a subquery when using STRING_AGG function
I get an error when trying to execute SELECT query in PostgreSQL 11 And authors_string should be fetched as a string value. It throwns an error ERROR: more than one row returned by a subquery used as an expression I guess it happens because of GROUP BY in subquery and can be handled with row_to_json function, but have no clue
Only include values from the latest timestamp into the query result
DB-Fiddle CREATE TABLE sales ( id int auto_increment primary key, time_stamp TIMESTAMP, product VARCHAR(255), sales_quantity INT ); INSERT INTO sales (time_stamp, product, …
Dynamic SQL :: Calculate percentage of NULLs per index
I have a query that help me list all index in a database and works pretty well: Unfortunately if I uncomment the 6th line the word t.name is underlined in red and if I run the query I receive the error: How can I make this subquery work? The goal is to have a percentage of NULLs on each column