Which condition satisfies among multiple conditions in SQL query - sql

This is my SQL query :
select *
from table1
where Column1 less than Condition1
|| Column2 less than Condition2
|| Column3 Less than Condition3
Now I am trying to find which condition out of above 3 conditions satisfies for a particular record in a table.

Are you sure it's SQL SERVER? If yes, then correcting the syntax, and adding your logic.
Like Karras mentioned, you already have * in the select which was already making you sure which column was satisfying the WHERE clause, but this code may help you further.
SELECT *,
CASE
WHEN Column1 < Condition1
THEN 'Column 1 satisfies'
WHEN Column2 < Condition2
THEN 'Column 2 satisfies'
WHEN Column3 < Condition3
THEN 'Column 3 satisfies'
END as ConditionFlag
FROM table1
WHERE Column1 < Condition1 OR Column2 < Condition2 OR Column3 < Condition3;

As long as you use * to return the full result set, you should be able to see the values for Column1, Column2, Column3 and understand which of the conditions evaluated to true.

Related

PostgreSQL count multiple columns of the same table

I want to count some columns from a table in PostgreSQL.
For each column count I have some conditions and I want to have all in one query. My problem is the fact that I don't get the expected results in counting because I tried to apply all the conditions for the entire data set.
The table:
column1
column2
column3
UUID10
UUID20
UUID30
NULL
UUID21
NULL
NULL
UUID22
UUID31
UUID11
UUID20
UUID30
This is what I tried so far:
SELECT
COUNT(DISTINCT column1) AS column1_count,
COUNT(DISTINCT column2) AS column2_count,
COUNT(DISTINCT column3) AS column3_count
FROM TABLE
WHERE
column2 IN ('UUID20', 'UUID21', 'UUID22')
AND column1 = 'UUID10' -> this condition should be removed from this where clause
OR column3 IN ('UUID30', 'UUID31')
Result:
column1_count
column2_count
coumn3_count
2
3
2
The result in not correct because I should have column1_count = 1. I mean, this is what the query does, but is not what I intended. So I thought to have some constrains for column2 and column3 in a subquery, and having a another condition just for column1.
A second try:
SELECT *
FROM
(
SELECT
column1
column2,
column3
FROM TABLE
WHERE
column2 IN ('UUID20', 'UUID21', 'UUID22')
OR column3 IN ('UUID30', 'UUID31')
) x
WHERE
column1 = 'UUID10'
Result:
column1_count
column2_count
coumn3_count
1
1
1
Because the last condition on column1 is restricting my result, I end up having 1 for all the counts.
How can I apply different conditions for counting each column?
I would try not to use UNION if is possible. Maybe there can be made some subqueries in another way than what I tried so far. I just have to find a way for the constraint for the column1, to not be on the same WHEN clause as for the column2 and column3.
I think you want conditional aggregation:
SELECT COUNT(DISTINCT CASE WHEN column1 = 'UUID10' THEN column1 END) AS column1_count,
COUNT(DISTINCT column2) AS column2_count,
COUNT(DISTINCT column3) AS coumn3_count
FROM TABLE
WHERE column2 IN ('UUID20', 'UUID21', 'UUID22') OR
column3 IN ('UUID30', 'UUID31');
I assume that you are aware that COUNT(DISTINCT CASE WHEN column1 = 'UUID10' THEN column1 END) is not particularly useful code. It returns 1 or 0 depending on whether the value is present. I assume your code is actually more interesting.

How do I update multiple tables with multiple where clauses in one query

Is there a way to do multiple updates with one sql query?
Something like:
UPDATE Table1
SET Column1 = Expression1 WHERE Conditions 1
SET Column2 = Expression2 WHERE Conditions 2
SET Column3 = Expression3 WHERE Conditions 3
...;
Thank you!
Yes, it looks like an anti pattern, but, syntactically, you can do it using Case expression:
UPDATE Table1
SET
Column1 = ( case when condition1 then Expression1 else Column1 end ),
Column2 = ( case when condition2 then Expression2 else Column2 end )
By the way, I guess you mixed "multiple tables" by "multiple columns".

ORACLE UPDATE SAME TABLE

I am getting error while updating one set of data by applying condition (LESS THAN SYMBOL) with other set of data on same table. could someone please help me.
Below is my oracle query -
UPDATE TABLE
SET COLUMN1 = 1
WHERE COLUMN2 = 'Y'
AND COLUMN3 = 'N'
AND TRUNC(COLUMN4) <
(SELECT TRUNC(COLUMN4)
FROM TABLE
WHERE COLUMN3 = 'Y' AND COLUMN4 = 'Y')
ERROR AS BELOW -
SQL Error:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
First some homework; please do not call your table TABLE (Oracle will complain). Additionally your subquery doesn't make sense:
(SELECT TRUNC(COLUMN4)
FROM TABLE
WHERE COLUMN3 = 'Y' AND COLUMN4 = 'Y')
If COLUMN4 = 'Y what does TRUNC(COLUMN4) mean?
But I guess what you mean is this (sample data added)
create table TAB as
select 1 COLUMN1, 'Y' COLUMN2, 'N' COLUMN3, sysdate-1 COLUMN4 from dual union all
select 2 COLUMN1, 'Y' COLUMN2, 'Y' COLUMN3, sysdate COLUMN4 from dual union all
select 2 COLUMN1, 'Y' COLUMN2, 'Y' COLUMN3, sysdate COLUMN4 from dual;
UPDATE TAB
SET COLUMN1 = 1
WHERE COLUMN2 = 'Y'
AND COLUMN3 = 'N'
AND TRUNC(COLUMN4) <
(SELECT TRUNC(COLUMN4)
FROM TAB
WHERE COLUMN2 = 'Y' AND COLUMN3 = 'Y');
Which leads to
ORA-01427: single-row subquery returns more than one row
The problem is, with < you can compare only two numbers, if you want to compare a number with a set of numbers (= result of a subquery with more rows), you must use the Group Comparison Conditions. Two choices are available:
< ALL - the predicate is valid for ALL values returned by the subquery
< ANY / < SOME the predicate is valid for SOME (at least one) value returned by the subquery .
So what you can do this for example
UPDATE TAB
SET COLUMN1 = 1
WHERE COLUMN2 = 'Y'
AND COLUMN3 = 'N'
AND TRUNC(COLUMN4) < ALL
(SELECT TRUNC(COLUMN4)
FROM TAB
WHERE COLUMN2 = 'Y' AND COLUMN3 = 'Y');
Update will be done in rows with TRUNC(COLUMN4) less than **ALL* values returned by the subquery.
I think the message is pretty clear. Perhaps you should use an aggregation function:
UPDATE TABLE
SET COLUMN1 = 1
WHERE COLUMN2 = 'Y' AND
COLUMN3 = 'N' AND
TRUNC(COLUMN4) < (SELECT MIN(TRUNC(COLUMN4))
FROM TABLE
WHERE COLUMN3 = 'Y' AND COLUMN4 = 'Y'
);
EDIT:
Given the columns you've specified, this could be simplified to:
UPDATE TABLE
SET COLUMN1 = 1
WHERE COLUMN2 = 'Y' AND
COLUMN3 = 'N' AND
TRUNC(COLUMN4) < 'Y';
You might need an additional condition, if the purpose of the subquery was to see if any rows exist.

Select statement subquery, multiple conditions

I am trying to create a query to select a certain condition then within that condition select two other conditions.
Breaking it down.
SELECT condition 1 FROM column 2, if this condition is not met return nothing.
SELECT condition 2 FROM column 3, SELECT condition 3 FROM column 4, if either of these two conditions are met return the respective column value from that rows value.
My feeble attempt which gives an obvious syntax error,
SELECT Column_1
FROM Data_TBL
WHERE Column_2 = 'Condition_1'
GROUP BY(WHERE Column_3 = 'Condition_2' OR Column_4 = 'Condition_3')
ORDER BY Column_1 ASC
Still very new to SQL statements and I am struggling with the syntax.
I think you just need a where clause. For the filtering:
select t.*
from data_tbl t
where (column2 = 'Condition_1') and
(column3 = 'Condition_2' or column4 = 'Condition_3);
I'm not sure what you want to return when both column3 and column4 meet the respective conditions, but I think this is what you want:
select (case when column3 = 'Condition_2' then column3 else column4 end)
from data_tbl t
where (column2 = 'Condition_1') and
(column3 = 'Condition_2' or column4 = 'Condition_3);

Select BETWEEN column values

I'm trying to use the BETWEEN with column names instead of direct values, something like this:
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3;
This is returning something like 17 rows, but if i write:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
i get around 600 rows..
In both cases i only get rows where column1 value is actually the middle value, but 2nd method gives me much more results, so 1st method has something wrong with it.
I suspect the problem might be on using BETWEEN clause with column names, instead of pure values, and somehow SQL is converting the column names to actual values..its strange, but can someone enlighten me please?
Thanks
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3; # gives 17 rows
is same as
SELECT * FROM table WHERE (column1 >= column2 AND column1 <= column3) # gives 17 rows
Because of your addition check of
(column1 <= column2 AND column1 >= column3)
which is ORed, you get additional rows.
Between A And B assumes that A<B, i.e., that the first expression in the Between, (A), is less than the second expression, (B) it does not check or execute with the opposite option.
e.g., if you put Where 3 Between 4 And 2 no rows will be returned:
or, if you write
Select Case When 3 Between 4 and 2 then 'true' else 'false' end
it will return false
Your logic for the two statements is not the same:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
Has two clauses. Remove the first and you should have the same results as your between statement.
SELECT * FROM table WHERE (column1 >= column2 AND column1 <= column3)