Joining two table - sql

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

Related

How to join 2 tables without creating duplicated rows of records in SQL

I have 2 tables (Table 1 and Table 2) which I would like to join and get the records of ALL dates..
I tried left join on Table1.productid = Table2.productid but it does not give me the expected output that I want..
Does anyone have experience in solving this type of join in SQL?
Use UNION ALL
SELECT
t1.Date_id,
t1.Product_id,
t1.Clicks,
0 AS Sales
FROM table1 t1
UNION ALL
SELECT
t2.Date_id,
t2.Product_id,
0 AS Clicks,
t2.Sales
FROM table2 t2
Yes you can do a full outer join to get the output as follows
select case when a.date_id is null then b.date_id end as date_id
,case when a.product_id is null then b.product_id else a.product_id end as product_id
,case when a.clicks is null then 0 else a.clicks end as clicks
,case when b.sales is null then 0 else b.sales end as sales
from table1 a
full outer join table2 b
on a.product_id=b.product_id
and a.date_id=b.date_id
Something like this may work:
INSERT INTO Your_new_table3(DateId, ProductId, Clicks, Sales)
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2;
You can use Outer Apply . Read Microsoft doc about this.
SELECT
t1.Date_id,
t1.Product_id,
t1.Click,
t2.Sales
FROM
table1 t1
OUTER APPLY
(
SELECT TOP 1 *
FROM table2 t2
WHERE t2.productid = t1.productid
) t2
OR use Group by statement :
SELECT DISTINCT
String_agg(t1.Date_id,'-'),
t1.Product_id,
t1.Click,
t2.Sales
FROM table1 t1
LEFT JOIN table2 t2 ON t2.productid = t1.productid
Group by t1.Product_id,
t1.Click,
t2.Sales
OR sue Distinct statement :
SELECT DISTINCT
t1.Date_id,
t1.Product_id,
t1.Click,
t2.Sales
FROM table1 t1
LEFT JOIN table2 t2 ON t2.productid = t1.productid

SQL LEFT JOIN with WHERE class

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 ) );

Select table rows when value from other table is null

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.

How can I replace `SELECT ... WHERE SELECT ...` with joins?

I have read that SELECT ... WHERE SELECT ... is slow, and that I should use joins instead.
But I don't know how to replace this code
SELECT Id
FROM Table1
Where
(
Data1 IS NULL
OR
(
Data2=1
AND
(SELECT 1 FROM Table2 WHERE Table2.Id=Table1.Id) IS NULL
)
)
AND
(SELECT 1 FROM Table3 WHERE Table3.Id=Table1.Id) IS NULL
with joins.
The tables have the following structure:
Table1:
Id: INTEGER PRIMARY KEY
Data1: XML
Data2: INTEGER
Table2:
Id: INTEGER
Table3:
Id: INTEGER PRIMARY KEY
select Id from Table1 where
Id not in (select Id from Table3) and
(Data1 is null or
(Data2 = 1 and Id not in (select Id from Table2)));
or, if you really want joins:
select Id from Table1 left join Table2 on (Table1.Id = Table2.Id)
left join Table3 on (Table1.Id = Table3.Id)
where Table3.Id is null and
(Data1 is null or
(Data2 = 1 and Table2.Id is null));
I don't expect much difference in performance between these two. The query would likely benefit from an index on Table2.Id (you have one on Table3.Id by virtue of it being a primary key).
There are two key parts to moving subqueries from in to the from clause. The first is to use left outer join, so no rows from the first table inadvertently drop out. The second is to use select distinct for each subquery, to avoid unwanted duplicates.
Applied to your query, the result is:
SELECT t1.Id
FROM Table1 t1 left outer join
(select distinct id
from Table2
) t2
on t1.id = t2.id left outer join
(select distinct id
from Table3
) t3
on t1.id = t3.id
Where(t1.Data1 IS NULL OR
(t1.Data2=1 and t2.id is null)
) and
t3.id is null;

Table join comparing and creating a new column using that compare

I have 2 tables... table1 and table2.
I want to display all serial numbers from table1
table2 has also has serial numbers in it
I would like to compare the serial numbers in table1 with table2
I would then like to display all the serial numbers in table1 and have a second column with a yes if the serial number was in table1 or a no if it wasn't
Is this possible to do with a sql statement or do I have to build a seperate table? i'm running sql-server.
If serial numbers in each table are unique then you could use:
SELECT Table1.SerialNumber,
CASE WHEN Table2.SerialNumber IS NULL THEN 'No' ELSE 'Yes' END AS [IsInTable2]
FROM Table1.SerialNumber
LEFT JOIN Table2
ON Table2.SerialNumber = Table1.SerialNumber
If there are Duplicates in one or both tables then either of the following will work:
SELECT DISTINCT
Table1.SerialNumber,
COALESCE([IsInTable2], 'No') [IsInTable2]
FROM Table1.SerialNumber
OUTER APPLY
( SELECT TOP 1 'Yes' [IsInTable2]
FROM Table2
WHERE Table2.SerialNumber = Table1.SerialNumber
) Table2
SELECT DISTINCT
Table1.SerialNumber,
CASE WHEN Table2.SerialNumber IS NULL THEN 'No' ELSE 'Yes' END [IsInTable2]
FROM Table1.SerialNumber
LEFT JOIN
( SELECT DISTINCT SerialNumber
FROM Table2
) Table2
ON Table2.SerialNumber = Table1.SerialNumber
If we assume that the serial numbers are unique in each table then you can do an outer join. Using a LEFT OUTER JOIN will grab you all rows from the left side and optionally grab you any matching rows on the right side. Then you can do a comparison to see if a matching row was found in table2.
SELECT t1.serial, CASE WHEN t2.serial IS NULL THEN 'No' ELSE 'Yes' END
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t1.serial = t2.serial;
Try this
SELECT t1.serialnumber as serialnumber, Case
WHEN t1.serialnumber = t2.serialnumber then 'YES' else 'NO' END
FROM table1 t1
LEFT JOIN table2 t2 with (nolock) on t1.serialnumber = t2.serialnumber;
Hopefully that should work