Merge two tables by two variables in SQL Server - sql

I have two tables that I want to merge by the two common variables.
So I have
Table1
Var1 Var2 Dates
---------- ---------- ----------------------
111111 AA 1990-06-20
111111 AA 2000-06-20
222222 BB 1963-06-20
222222 BB 2005-06-20
333333 CC 2006-06-30
333333 CC 2016-06-26
and Table2
Var2 Dates
---------- ----------------------
AA 1990-06-20
BB 1963-06-20
I want the output to be
Var1 Var2 Dates
---------- ---------- ----------------------
111111 AA 1990-06-20
222222 BB 1963-06-20
I have tried inner join two times using "Var2" and "Dates" in two tables. But the results gave me more rows (duplicate entries) than I need.

Well, the most common way would be to use an INNER JOIN:
SELECT T1.*
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.Var1 = T2.Var1
AND T1.Dates = T2.Dates
But this makes more sense when you also want to use some columns from the second table. In your case, you could do just:
SELECT *
FROM Table1 T1
WHERE EXISTS(SELECT 1 FROM Table2
WHERE Var1 = T1.Var1
AND Dates = T1.Dates)

I think in your case you need only one JOIN with multiple conditions
SELECT t1.*
FROM Table1 t1
JOIN Table2 t2 ON t1.Var2 = t2.Var2 AND t1.Dates = t2.Dates

Related

Join two tables with switch case in order to avoid one to many join

I have two tables, t1 and t2.
Table t1:
Name address id
---- ------- --
rob 32 cgr 12
mary 31 lmo 42
tom axel St 2
Table t2:
ID Flag expense
-- ---- --------
12 Shop 1200
12 Educ 14000
42 educ 4000
Now I will have to create a table which will have attributes from t1 plus two more attributes that is expense in shop and expense in educ
Table t3
Name address id Shop_ex Educ_ex
---- ------- -- ------- -------
rob 32 cgr 12 1200 14000
mary 31 lmo 42 NULL 4000
tom axel st 2 NULL NULL
How to accomplish this?
I tried doing a left join t2 with switch case but it gives me multiple record as the join is becoming one to many.
select
t1.name, t1.address, t1.id,
case
when t2.flag = "shop" then t2.expense
else null
end as shop_ex
case
when t2.flag = "educ" then t2.expense
else null
end as educ_ex
from
t1
left join
t2 on (t1.id = t2.id)
It seems I will have to convert t2 table first before joining, to have a single record on the basis of flag. But I am not sure how to do that.
Please mind the tables are huge and optimized query will be nice.
Please suggest.
You only need to join the first table to the second one, twice:
SELECT t1.Name, t1.address, t1.id, t2a.expense AS Shop_ex, t2b.expense AS Educ_ex
FROM table1 t1
LEFT JOIN table2 t2a
ON t2a.ID = t1.id AND t2a.Flag = 'Shop'
LEFT JOIN table2 t2b
ON t2b.ID = t1.id AND t2b.Flag = 'Educ'
Demo

SQL Server : select from multiple tables and calculate percentages

I have four tables and I would need to extract data from them to calculate the percentage
Table1
ID FK1 FK2
------------------
1 1 1
2 2 2
3 3 3
Table2
ID Name
------------------
1 L1
2 A1
3 B
Table3
ID FK3
------------------
1 1
2 2
3 3
Table4
ID Name
------------------
1 BA
2 N
3 CE
Now I need to get a Name from table4, which will be displayed as individual rows and then a Name from table2, which will be listed as individual columns and the value will then be a percentage of the record from table4:
Name L1 A1 B
---------------------------
BA 20(%) 40(%) 40(%)
N 30(%) 20(%) 30(%)
CE 15(%) 15(%) 70(%)
Because there are links, I'll give an example of what question I have now
select t3.Name
from table1 t1 (nolock)
join table2 t2 (nolock) on t1.FK1 = t2.ID
join table3 t3 (nolock) on t1.FK2 = t3.ID
join table4 t4 (nolock) on t2.FK3 = t4.ID
Does anyone have any idea how to do this? Thank you very much

Joining table get the first data on first table if duplicate

Hi i have table1 and table2.
table1 is the logtime table of employees and table2 is the groupcode of the employee.
On table1 some employees has duplicate time in because they time in multiple time to just to secure their time in.
Table1
ID EMPID Time_IN
1 001 7:01 AM
2 004 7:04 AM
3 034 7:10 AM
4 034 7:11 AM
5 019 7:11 AM
6 019 7:12 AM
Table2
ID empID GroupName
1 001 AA
2 004 AB
3 034 AA
4 019 AA
result
GroupName CNT
AA 5
AB 1
Expected result
GroupName CNT
AA 3
AB 1
current query
Select b.GroupName, count(*) as cnt
from table1 a
inner join table2 b
on a.EMPID = b.empID
Group by b.GroupName
How can i achive as expected result above?
Thankyou in advance.
you can use distinct count as follows:
select t2.groupname, count(distinct empid) as cnt
from table1 t1 join table2 t2
on t1.empid = t2.empid
group by t2.groupname
The join is superfluous for the question you have asked:
select t2.GroupName, count(*) as cnt
from table2 t2
group by t2.GroupName;
This is much more efficient than joining and using count(distinct). You probably really have a different question, which should be asked as a new question.

SQL How to join two query's with different columns and joins

I have 2 queries both use joins, select various columns from multiple tables which produce the below result sets:
companyID workType contractnumber employeenumber Value2
1 1C 9999999 111111 2547.21
1 1C 9999999 222222 863.67
1 1C 9999999 333333 2962.15
1 1C 9999999 444444 1971.61
1 1C 9999999 555555 152.41
1 1C 9999999 666666 155.90
1 1C 9999999 777777 657.20
companyID normalWorkType employeeNumber value1
1 1C 11111 1016.08
1 1C 22222 3118.05
1 1C 33333 2628.81
1 61 44444 2547.21
I'm looking to join these to produce the below result.. can anyone explain what the correct syntax would be?
companyID normalWorkType contractnumber employeeNumber value1 Value2
1 1C 9999999 11111 1016.08 2547.21
1 1C 9999999 22222 3118.05 863.67
1 1C 9999999 33333 2628.81 2962.15
1 61 9999999 44444 2547.21 1971.61
1 1C 9999999 55555 0 152.41
1 1C 9999999 66666 0 155.90
1 1C 9999999 77777 0 657.20
Grouping above is on companyID & employeeNumber
Try LEFT OUTER JOIN:
SELECT t1.companyID,
t2.normalWorkType,
t1.contractnumber,
t1.employeeNumber,
coalesce(t2.value1,0) as t2.value1,
t1.value2
FROM DatabaseName.t1 AS t1 LEFT OUTER JOIN
DatabaseName.t2 AS t2 ON t1.employeenumber = t2.employeenumber
AND t1.companyID = t2.companyID
I'm assuming here that the contract numbers in the proposed result set are actually the contract numbers from your first query, and that the difference is just a typo. If that's true, this will get you what you want.
SELECT
q1.companyID
COALESCE(q2.normalWorkType,q1.workType) AS normalWorkType
q1.contractnumber
q1.employeeNumber
COALESCE(q2.value1 ,0) as value1
q1.Value2
FROM
(
<Your first query>
) as q1
LEFT JOIN
(
<Your second query>
) as q2
ON
q1.companyID = q2.companyID
AND
q1.employeeNumber = q2.employeeNumber;
Try below Query
select Table1.companyId,Table2.normalworktype,Table1.contractnumber,
Table1.employeenumber,Table2.value1,Table1.value2
from Table1
inner join Table2 on Table1.companyid = Table2.companyid
and Table1.employeeNumber=Table2.employeeNumber
Thanks for the responses everyone,
After trying Husam Ebish's suggestion It wasn't quite what I needed but lead me to this thread. (up-voted your answer)
How to combine two query results into one and I have different column name
What I really found useful was this bit of information highlighted below from the linked thread. After trying the different "left" joins suggested the comment's highlighted lead me to try "full outer join" which has given me the correct result-set.
If you only have one row from each query, it's just a CROSS JOIN
SELECT
*
FROM
(query1) AS q1
CROSS JOIN
(query2) AS q2
If you have more than one row from each query, you need an INNER JOIN
or maybe a FULL OUTER JOIN and some relationship between the two sets
of data
SELECT
*
FROM
(query1) AS q1
FULL OUTER JOIN
(query2) AS q2
ON q2.id2 = q1.id1
Note:
UNION appends data vertically
JOIN appends data horizontally
SELECT
*
FROM
(select top 1 VL1,VL2,VL3 from current_voltage AS q1
where deviceimei ='233'
order by devicetimestamp) AS q1
CROSS JOIN
(select top 1 OTI,WTI,ATI from overview AS q2
where deviceimei ='233'
order by devicetimestamp ) AS q2
You can put your search result to 2 temp table and then join this 2 temp table or use alliance for your each select and then join them.
like :
SELECT
*
FROM (
SELECT
companyID
,workType
,contractnumber
,employeenumber
,Value2
FROM table1
) t1
INNER JOIN (
SELECT
companyID
,normalWorkType
,contractnumber
,employeeNumber
,value1
,Value2
FROM table2
) t2
ON t1.companyID = t2.companyid
or put your result 2 different temp table and then join them
SELECT
companyID
,workType
,contractnumber
,employeenumber
,Value2
INTO #tt1
FROM table1
SELECT
companyID
,normalWorkType
,contractnumber
,employeeNumber
,value1
,Value2
INTO #tt2
FROM table2
SELECT *
FROM #tt1 t1
INNER JOIN #tt2 t2 ON t1.companyid = t2.companyID

Select single row from two table related using mapping table SQL Server

I have three tables: Table1, Table2, Table1Table2Mapping.
Each tabel1 data having multiple data in tabel1, this table1 relation table2 is done through the mapping table the Primary key of the table1 and table2 is put in mapping table.
Table1:
Table1_ID Name ,Other columns
--------- ---- ------
1 Name1
2 Name2
3 Name3
Table2:
Table2_ID Title
--------- -----
101 Title1
102 Title2
103 Title3
104 Title4
105 Title5
Table1Table2Mapping:
Table1_ID Table2_ID
--------- ------------
1 101
1 102
2 103
3 104
3 105
I am getting all the related rows from table1 and its relation in table1 through mappping table
I need to select single row for each Table1 row.
Like this
Table1_ID Name Title
--------- ------- -----
1 Name1 Title1
2 Name2 Title3
3 Name3 Title4
The CROSS APPLY will do the trick:
SELECT
T1.Table1_ID
, T1.Name
, TMP.Title
FROM
Table1 T1
CROSS APPLY (
SELECT TOP(1)
T2.Title
FROM
Table2 T2
INNER JOIN Table1Table2Mapping TTM
ON T2.Table2_ID = TTM.Table2_ID
WHERE
TTM.Table1_ID = T1.Table1_ID
ORDER BY
TTM.TAble2_ID ASC -- You can change this order to what you want
) TMP
You can change the order of the subquery.
SQL Fiddle
Using CROSS APPLY in TechNet
EDIT
IF it is enough, you can use aggregation too:
SELECT
T1.Table1_ID
, T1.Name
, MIN(T2.Title) -- You can use MAX
FROM
Table1 T1
INNER JOIN Table1Table2Mapping TTM
ON TTM.Table1_ID = T1.Table1_ID
INNER JOIN Table2 T2
ON T2.Table2_ID = TTM.Table2_ID
GROUP BY
T1.Table1_ID
, T1.Name
(This gives the same result for the provided dataset, but in real life, the result of this and the previous solution mostly different!
use outer apply in sql server 2005 and later
Select Table1.*, ttt.Title From Table1 Outer Apply
(Select Top 1 Table2.* From Table2 Inner Join Table1Table2Mapping
On Table2.Table2_Id = Table1Table2Mapping.Table2_Id
Where Table1.Table1_Id = Table1Table2Mapping.Table1_Id) ttt