is there a way to select rows using values from another table?
I want to select data on table 1 using table2
Output will be
I have 2 instances in selecting values in table1 that's why I'm trying to use case on this one, I'm not just sure if it is correct.
select
case when table2.s_num is Null and table2.s_div is Null then
(select * from table1 where table1.item_num = table2.i_num)
from table1, table2
Here are my instances:
1. if t2.s_num is null and t2.s_div is null then select * from t1 where t1.item_num = t2.i_num
2. if t2.s_num is null and t2.s_div is not null then select * from t1 where t1.item_num = t2.i_num and t1.store_div = t2.s_div
I'm not that good in sql, any ideas? thanks!
is there a way to select rows using values from another table?
You should use a join.
First, look at the output of this query:
select *
from table1 t1
join table2 t2 on t2.i_num = t1.item_num
See how the join works? It matches i_num to item_num and returns only rows where there was a match (that's an inner join, the default kind).
You actually have a more complicated join condition for your two "instances", something like this should express it:
select *
from table1 t1
join table2 t2 on t2.i_num = t1.item_num and (t2.s_div is null or t2.s_div = t1.store_div)
You also want to filter to rows where s_num is null, so just add a where:
select *
from table1 t1
join table2 t2 on t2.i_num = t1.item_num and (t2.s_div is null or t2.s_div = t1.store_div)
where t2.s_num is null
just join is ok
select t1.*
from
table1 t1
join table2 t2
on t1.item_num = t2.i_num
where t2.s_num is null and t2.s_div is null
use table2 LEFT JOIN to table1
SELECT store_num = coalesce(t2.s_num, t1.store_num),
item_num = t2.i_num,
store_div = colaesce(t2.s_div, t1.store_div),
t1.price
FROM table2 t2
LEFT JOIN table1 t1 ON t2.i_num = t1.item_num
You can try this. Change OR to AND in WHERE clause if both s_num and s_div should be null
SELECT *
FROM TABLE1 T1
INNER JOIN TABLE1 T2
ON T2.I_NUM = T1.ITEM_NUM
WHERE T2.S_NUM IS NULL OR T2.S_DIV IS NULL
SELECT A.*
FROM table1 AS A JOIN table2 AS B ON (B.i_num = A.item_num)
WHERE A.item_num IN (SELECT i_num FROM table1 WHERE s_num=null AND s_div=null);
You can use LEFT JOIN:
select
from table1
left join table2
on table2.s_num is null
and (table2.s_div = table1.store_div or table2.s_div is null)
and table1.item_num = table2.i_num
As you said, if table2.s_num is not null, you will not join table2, and if table2.s_num is null matches, then will do table2.s_div = table1.store_div or table1.store_div is null, if this matches, it means table1.item_num = table2.i_num will be computed.
Related
select *
from Table1
full join Table2 on Table2.Common = Table1.Common
In the above query, I want to add this where condition:
where (Table1.StatusId > 100 or Table1.StatusId is not null)
StatusId is of datatype numeric(18, 2).
The problem is when I use the where condition, rows in Table2 which don't have any connecting data in Table1 are not showing up anymore.
Is there any other way to add this where condition?
Try put it on a Subquery.
SELECT *
FROM ( SELECT *
FROM Table1
WHERE ( Table1.StatusId > 100
OR Table1.StatusId IS NOT NULL
)
) T
FULL JOIN Table2 ON Table2.Common = t.Common
Move the t1.StatusId is NOT NULL filter to ON condition
SELECT *
FROM Table1 t1
FULL JOIN Table2 t2
ON t1.Common = t1.Common
AND t1.StatusId is NOT NULL
The problem in your query is, in table1 status column for the non matching records NULL values will be present which will be filtered by applying the Where condition Where t1.StatusId is NOT NULL. So move it to ON clause which tells what are the records to joined instead of filtering the result
You can just add a AND (Table1.StatusId > 100 or Table1.StatusId is not null) after your ON-Statement
If you want to apply the where condition to the result of the join condition then
select *
from Table1
full join Table2 on Table2.Common = Table1.Common where Table1.StatusId > 100 or Table1.StatusId is not null
This result will probably not have the data from Table2 which has no connected data from Table1 because of your condition(Table1.StatusId is not null).
If you want the join to condition to be executed for only filtered records of Table1 then
select * from Table2 full join (select * from Table1 where StatusId > 100 or StatusId is not null ) T on Table2.Common = T.Common
select *
from Table2
Full join Table1 on Table2.Common = Table1.Common
where (Table2.StatusId > 100 or Table2.StatusId is not null)
select *
from table1 t1,
table2 t2,
table3 t3
where t2.parent_id = t1.row_id
and t2.xyz is not null
and (
select count(*)
from table3
where xyz = t2.row_id
) = 0;
Will it work?
I am using the alias t2 within my subquery.
My requirement is to check is to specify condition in where clause such that there is no record present in table3 where column xyz of table3 is stored as row_id of table2.
You can use NOT EXISTS to assert that there is no row returned from the subquery. Use modern explicit join syntax instead of comma based legacy syntax. No need to join table3 outside (you were making a cross join effectively).
select *
from table1 t1
join table2 t2 on t2.parent_id = t1.row_id
where t2.xyz is not null
and not exists (
select 1
from table3
where xyz = t2.row_id
);
there are two tables TABLE1 and TABLE2 in TABLE1 there are records which does not exist in TABLE2 with left join below i wanted to query all records which are in TABLE1 if the record does not exist in table2 however.
Note: about WHERE class in my code that is required this is because, there can be several records in the name of 'IN PROGRESS' in TABLE2 with one record in the name of 'GRADUATED' i wanted to distinct records based on table 1 ID that if there is any record in the name of 'GRADUATE' it should show only that else it should show inprogress.
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED'
I see some odds with your query:
exists part are not related with you main query. I think you need some relation
distinct in part not exists are not needed
You filter columns with the same conditions as filter main row set
As I understand you want to get all rows from table1 with state 'GRADUATED' int table2 and any row from table1 where rows in table2 are not exists or state not equal 'GRADUATED'
SELECT DISTINCT
t1.ID,
t2.TRAINING_STATUS_CHECK
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.ID = t2.FK_ID_CLASS
WHERE NOT EXISTS
(
SELECT NULL /*its not nesessary what you need*/
FROM TABLE1 sub_t1
JOIN TABLE2 sub_t2 ON sub_t1.ID = sub_t2.FK_ID_CLASS /* left join replaced to inner */
WHERE sub_t2.TRAINING_STATUS_CHECK = 'GRADUATED'
AND sub_t1.ID = t1.ID /*relation with outer query*/
)
OR t2.TRAINING_STATUS_CHECK = 'GRADUATED'
where the relatonship between tables does not exist - but only if the comparison involves rows in table that are not 'graduated' (I think)
SELECT DISTINCT
TABLE1.ID,
TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND TABLE2.TRAINING_STATUS_CHECK <> 'GRADUATED'
WHERE TABLE2.FK_ID_CLASS IS NULL
Not sure about your question but if you want all the records from table 1 who are not in table 2, you just have to do this :
SELECT TABLE1.ID
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FK_ID_CLASS
WHERE TABLE2.FK_ID_CLASS IS NULL
Try this:
SELECT DISTINCT TABLE1.ID, TABLE2.TRAINING_STATUS_CHECK
FROM TABLE1
LEFT JOIN TABLE2
ON TABLE1.ID = TABLE2.FK_ID_CLASS
AND (NOT EXISTS (SELECT 1
FROM TABLE2 t
WHERE TABLE1.ID = t.FK_ID_CLASS
AND t.TRAINING_STATUS_CHECK = 'GRADUATED')
OR TABLE2.TRAINING_STATUS_CHECK = 'GRADUATED')
For the record, conditions on the right table of a LEFT JOIN need to be placed inside the ON() clause or the join will transfer into an INNER JOIN due to NULL comparison.
It seems to me you have three distinct cases that can be "ORed together" using UNION; personally I find keeping all three separated like this makes things much easier to read and understand:
--- ID with GRADUATED exists in TABLE2
( SELECT ID, 'GRADUATED' AS TRAINING_STATUS_CHECK
FROM TABLE1
INTERSECT
SELECT FK_ID_CLASS, 'GRADUATED'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID without GRADUATED exists in TABLE2
( SELECT ID, 'IN PROGRESS'
FROM TABLE1
MINUS
SELECT FK_ID_CLASS, 'IN PROGRESS'
FROM TABLE2
WHERE TRAINING_STATUS_CHECK = 'GRADUATED' )
UNION
--- ID does not exist in TABLE2
( SELECT ID, '{{NONE}}'
FROM TABLE1
WHERE ID NOT IN ( SELECT FK_ID_CLASS FROM TABLE2 ) );
I have some set of records, but now i have to select only those records from this set which have theeir Id in either of the two tables.
Suppose I have table1 which contains
Id Name
----------
1 Name1
2 Name2
Now I need to select only those records from table one
which have either their id in table2 or in table3
I was trying to apply or operator witin inner join like:
select *
from table1
inner join table2 on table2.id = table1.id or
inner join table3 on table3.id = table1.id.
Is it possible? What is the best method to approach this? Actually I am also not able to use
if exist(select 1 from table2 where id=table1.id) then select from table1
Could someone help me to get over this?
Use left join and then check if at least one of the joins has found a relation
select t1.*
from table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
where t2.id is not null
or t3.is is not null
I would be inclined to use exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.id = t1.id) or
exists (select 1 from table3 t3 where t3.id = t1.id) ;
The advantage to using exists (or in) over a join involves duplicate rows. If table2 or table3 have multiple rows for a given id, then a version using join will produce multiple rows in the result set.
I think the most efficient way is to use UNION on table2 and table3 and join to it :
SELECT t1.*
FROM table1 t1
INNER JOIN(SELECT id FROM Table2
UNION
SELECT id FROM Table3) s
ON(t.id = s.id)
Alternatively, you can use below SQL as well:
SELECT *
FROM dbo.Table1
WHERE id Table1.IN ( SELECT table2.id
FROM dbo.table2 )
OR Table1.id IN ( SELECT table3.id
FROM Table3 )
In this Table1 and Table2 (AttendanceDate,EmployeeCode) are PRIMARY Key..
How can we replace table1 value with table2 where ever AttendanceDate and EmployeeCode will match..
Like Result table..
try this untested query:
select t1.AttendanceDate, t1.EmployeeCode, case when t2.duration is null then t1.duration else t2.duration end
from table1 t1 left outer join table t2 on t1.AttendanceDate= t2.AttendanceDate and t1.EmployeeCode = t2.EmployeeCode
This should work.
SELECT Table1.AtendanceDate, Table1.EmployeeCode,
CASE WHEN Table2.Duration IS NOT NULL THEN Table2.Duration ELSE Table1.Duration END AS Duration
FROM Table1
LEFT OUTER JOIN Table2 ON Table1.AtendanceDate = Table2.AtendanceDate
AND Table1.EmployeeCOde = Table2.EmployeeCode