if conditions inside where sql - sql

I'm trying this logic not sure what am missing in this case
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where t1.column1 between 1 and 10 if t2.column2='value1'
and t1.column1 between 11 and 20 if t2.column2='value2'

You could use a case when aproach
CREATE tABLE table2(column1 int, column2 varchar(10))
INSERT INTO table2 VALUEs(1,'value1'),(2,'value1'),(13,'value2'),(44,'value2')
CREATE tABLE table1(column1 int)
INSERT INTO table1 VALUES (1),(2),(13),(44)
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where
CASE WHEN t1.column1 between 1 and 10 AND t2.column2 like 'value1' THEN TRUE
WHEN t1.column1 between 11 and 20 AND t2.column2 like 'value2' THEN TRUE
ELSE FALSE END
column1 | column1 | column2
------: | ------: | :------
1 | 1 | value1
2 | 2 | value1
13 | 13 | value2
db<>fiddle here

I assume you've left out key details, like the ON for the JOIN and the wildcard for the LIKE, in the interest of simplicity.
select *
from table1 t1
join table2 t2
where (t1.column1 between 1 and 10 and t2.column2 like 'value1')
or (t1.column1 between 11 and 20 and t2.column2 like 'value2')

Related

Matching ID if more than one in column then nothing

I have table1 and table2
table1
nameID column1 column2 column3
Joe 10 10 99
table2
nameID column1 cost
Joe 99 100
Joe 10 100
Joe 30 200
My goal to have column3 from table1 match with column1 from table2.
IF table2 have 2 or more records even if table1 column3 match with table2 column1 (99) and expect nothing output.
IF table 2 have only one record on column1 (99) match with table1 column3 (99) then expect result
nameID column3 cost
Joe 99 100
I have tried
select t1.name, t1.column3
from table1 t1
join table2 t1 on t1.nameID = t2.nameID
where
t1.column3 = t2.column1
and t1.column1 <> t2.column1
and t1.column2 <> t2.column1
Not sure how to make it works. Thank you.
WITH CTE_X
AS (
SELECT count(*) AS countX
FROM table2 t2
)
SELECT table1.nameId
,table1.column3
FROM table1
INNER JOIN table2 ON table1.column3 = table2.column1
JOIN CTE_X ON 1 = 1
WHERE CTE_X.countX = 1
Please see this

SQL Query Syntax Error using select & <> with 2 columns

I got a table with 2 columns here.
| column1 | column2 |
| A | 1 |
| B | 1 |
| C | 2 |
| D | 1 |
I'm trying to execute a SELECT query that:
Selects a value FROM Column 2 WHILE Column 2 doesn't have the same value in a row which has Column 1 = "A"
SELECT column2 from mytable and column2 <> (SELECT * FROM mytable where column1 = 'A');
Basically I'm trying to execute a query that returns column1 values only when column2 is valued at "2" here.
But the project I'm making will be having column2 values random so I should use only columns names
Sorry if that's too confusing!
The subquery of your query returns multiple values. So you have to use NOT IN instead of <>:
SELECT column2
FROM mytable
WHERE column2 NOT IN (SELECT column2 FROM mytable WHERE column1 = 'A');
use corelated subquery
select t1.* from mytable t1
where not exists ( select 1 from mytable t2 where t2.column2=t1.column2 and column1='A')
or use not in
select t1.* from table_name t1 where t1.column2 not in ( select column2 from table_name where column1='A')
You could also use a join, for example:
select t1.column2
from mytable t1 left join
(select distinct t2.column2 from mytable t2 where t2.column1='A') t3 on t1.column2=t3.column2
where t3.column2 is null

SQL Join with 2 columns when one of them is NULL/Blank

Let's say I have 2 tables like this:
I want to join both #tmp1 & #tmp2 for Columns : Col2 & Col3 . But if the value of either of these columns is blank or Null, I want to ignore that column and just look at one.
So for example here, when I join where Col2 is ABC, I should get both DEF & Blank for Col3.
I hope I'm making some sense here. Apologize if it's not clear enough.
I can't tell from your example what columns you want to join but to solve the "null" problem you do it like this
SELECT *
FROM T1
JOIN T2 ON COALESCE(T1.COL1,T2.COL1) = T2.COL1
AND COALESCE(T1.COL2,T2.COL2) = T2.COL2
If T1.COL1 or T1.COL2 are null it will use the value of the table it is joining to. This allows null to be a "wildcard".
or if T2 is the table with nulls
SELECT *
FROM T1
JOIN T2 ON T1.COL1 = COALESCE(T2.COL1,T1.COL1)
AND T1.COL2 = COALESCE(T2.COL2,T1.COL2)
You can use union for this checking if at least 1 of the 2 columns is the same using exists:
select col1, col2, col3
from tmp1 t1
where exists (select 1
from tmp2 t2
where t1.col2 = t2.col2 or t1.col3 = t2.col3)
union
select col1, col2, col3
from tmp2 t2
where exists (select 1
from tmp1 t1
where t1.col2 = t2.col2 or t1.col3 = t2.col3)
Fiddle Demo
Results:
| col1 | col2 | col3 |
|-------|------|------|
| test1 | abc | def |
| test2 | aaa | bbb |
| test1 | abc | |
| test2 | ccc | bbb |

SQL left outer join to a min value of right table

I have two tables, lets say table1 and table2.
table1 || table2
--------||-------------
col1 || col1 | col2
--------||------|------
a || a | 4
b || a | 2
c || a | 5
d || b | 1
|| b | 3
|| d | 6
With SELECT table1.col1, table2.col2 FROM table1 LEFT OUTER JOIN table2 ON table1.col1 = table2.col1 I get following:
table1.col1 | table2.col2
-------------|-------------
a | 4
a | 2
a | 5
b | 1
b | 3
c | NULL
d | 6
How is it possible to achieve this (only get the minimum of table2.col2 so that there's no entry of table1.col1 more than once):
table1.col1 | table2.col2
-------------|-------------
a | 2
b | 1
c | NULL
d | 6
Or is it a wrong approach?
You need to use MIN:
SELECT
t1.col1,
MIN(t2.col2) AS col2
FROM table1 t1
LEFT JOIN table2 t2
ON t2.col1 = t1.col1
GROUP BY t1.col1
SQL Fiddle
Alternative solution, use a correlated sub-query:
select col1, (select min(col2) from table2 t2 where t2.col1 = t1.col1)
from table1 t1
If there are more columns in table2 you may want to use APPLY operator:
SELECT * FROM table1
OUTER APPLY(SELECT TOP 1 * FROM table2
WHERE table1.col1 = table2.col1 ORDER BY table2.col2)oa

Resolved: Query that returns the output where one column matches all the values in another column

Using oracle developer, I've run a query that results in the following table. But I only want the results where column1 matches all the values columns 2 (3,4,8). So the output would be 2, 3, but not 4. I'm sure there is a way to bring this result about without hard coding it? I'm thinking its some sort of self-join?
select column1, column2
from table1
where column1 in (
select column1
from table2
where depth >= 100)
order by column2;
Output:
column1 column2
3 2
8 2
4 2
3 3
4 3
8 3
4 4
Table2
Column1 Area_Name Depth
1 Lake 40
2 River 50
3 Ocean 150
4 Cliff 150
5 Mountain 90
6 Construction 60
7 Building 50
8 Random 100
9 Also Random 50
10 Another one 80
Needed output:
column2
2
3
Ok, this is what I was looking for:
SELECT table1.column1
FROM table1
INNER JOIN table2
ON table1.column2 = table2.column2
WHERE table2.depth >= 100
GROUP BY boat_id
HAVING COUNT(*) >= (
select count(*)
from table2
where depth >= 100);
UPDATED
WITH qry AS (
SELECT column1, column2
FROM table1
WHERE column1 IN (
SELECT column1
FROM table2
WHERE depth >= 100)
)
SELECT t1.column2
FROM qry t1 LEFT JOIN qry t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
GROUP BY t1.column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM qry)
ORDER BY t1.column2
Output:
| COLUMN2 |
-----------
| 2 |
| 3 |
SQLFiddle