Use And and or operator together in sql - sql

Is there any way that i could use and and like operator with or to get desired
Select * from table where column1 = 1 and column2 =10 and column3 like '%we%' or column3 like '%ws%'
Need to use or in just 3rd column but with and of other 3 Columns

Try this:
Select * from table where column1 = 1 and column2 =10 and (column3 like '%we%' or column3 like '%ws%');
That's how you can get your desired output.

Related

How to use CASE statement in SQL to change an empty field in one column based on another column

I'm just starting with SQL with no training but the job suddenly requires it. So thank you in advance for any help.
Let's say I have a query that returns 3 columns. Some of the cells in column 3 are empty and I would like to fill them in with values based on column1.
example:
CASE column1 = 'Individual' then Column3 should show 'Individual' not empty, but if column1 = 'group' them column3 needs to show "group" else no change.
SELECT column1, column2, column3,
CASE
WHEN column1 = 'Individual' THEN Column3 = 'Individual'
WHEN column1 = "Group' THEN comlumn3 = 'Group'
END
FROM tablename
If you only want to select data and not change the table values you can use case as you tried:
SELECT column1, column2,
CASE
WHEN column3 IS NULL THEN Column1
ELSE column3
END as column3
FROM tablename

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.

Merging two columns but only unique combinations

I have two columns, each with identification numbers that have been brought in from different datasheets.
I want to combine this into one column with both identification numbers if they are different, but only one of the identification numbers if they are the same.
I'm using SELECT DISTINCT CONCAT(column 1, column 2) AS column 3 to combine the columns, but can not filter out UNIQUE combinations.
When I try WHERE column 1 <> column 2, I get an error message.
Any suggestions?
You can use CASE WHEN to test for conditions:
SELECT DISTINCT CASE WHEN column1 = column2 THEN column1
ELSE CONCAT(column1, column2)
END AS column3
FROM table1
try this using IIF or CASE and CONCAT
select
distinct
iif(col1<>col2,concat(col1,col2),col1) [myid]
from mytable
or
select
distinct
case when col1<>col2 then
concat(col1,col2)
else col1 end [myid]
from mytable
You should do something like:
SELECT DISTINCT CASE WHEN column1 = column2 THEN column1
ELSE column1 + '|' + column2
END AS combinedColumn
FROM table1
Consider the following chart:
column1 column2 column1+column2 column1+'|'+column2
12 34 1234 12|34
123 4 1234 123|4
1234 1234 1234 1234
Also, column1+column2 loses some information - what the original parts were.

Getting "ORA-00923: FROM keyword not found where expected" in a Sybase-working query

I have built the following query running fine on Sybase engines :
select *,
(select COUNT(*) from myTable where column1 != '' and column2!=column3) as myField1,
(select COUNT(*) from myTable where column1 != '' and column2=column3) as myField2
from
(select column1,column2,column3,column4,column5
from myTable
where column1 != '' and column2 != column3) as SUB_VIRTUAL_TABLE
However, running the same query on Oracle engines will raise the following exception:
ORA-00923: FROM keyword not found where expected
Could please anyone help to fix this syntax?
Thanks a lot.
P.s.: tables and columns exist, the query is indeed running fine when the engine is Sybase.
The expected result would look like this:
column1 | column2 | column3 | column4 | column5 | myField 1 | myField2
-----------------------------------------------------------------------
A B C D E 12 15
E F G H I 12 15
...
You don't use the as keyword in Oracle to alias subqueries. You just specify the alias. And you probably need to alias the * in your select
select sub_virtual_table.*,
(select COUNT(*)
from myTable
where column1 != ''
and column2!=column3) as myField1,
(select COUNT(*)
from myTable
where column1 != ''
and column2=column3) as myField2
from (select column1,column2,column3,column4,column5
from myTable
where column1 != ''
and column2 != column3) SUB_VIRTUAL_TABLE
Since a varchar2 column in Oracle can never be equal to nor unequal to the empty string, the four column1 predicates aren't doing anything. You probably want to either remove them or do the proper IS NULL/ IS NOT NULL checks. See this discussion on why Oracle treats the empty string as NULL.
In Oracle '' is treated like null hence the condition != '' can be written like is not null.
Try this:
select SUB_VIRTUAL_TABLE.*,
(select COUNT(*) from myTable where column1 is not null and column2!=column3) as myField1,
(select COUNT(*) from myTable where column1 is not null and column2=column3) as myField2
from (
select column1,column2,column3,column4,column5
from myTable
where column1 is not null and column2 != column3
) SUB_VIRTUAL_TABLE

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)