I have two tables (Temp1 & finalTemp) [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have two tables (Temp1 & finalTemp)
temp1
AccId Name address city GuarantorId
1 abc xyz a 11
1 qwe asd a 115
2 kk aa t 21
3 t u p 96
now I want my finalTemp like
AccId Name1 address1 city1 Name2 address2 city2 Name3 address3 city3
1 abc xyz a qwe asd a null null null
2 kk aa t null null null null null null
3 t u p

Self LEFT JOIN?
select t1.*,t2.*
from temp t1
left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name
Or, extended, double self left join:
select t1.*,t2.*,t3.*
from temp t1
left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name
left join temp t3 on t2.AccId = t3.AccId and t2.name < t3.name

Related

How to find the two combinations of elements that are present the most in all the groups? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
For example in this simple table:
Group
Element
xxx
a
xxx
b
xxx
c
xxx
d
xxx
e
yyy
a
yyy
b
yyy
f
yyy
g
zzz
a
zzz
b
zzz
c
zzz
g
'a' and 'b' are the combination of elements present the most in all groups
You can do it with a triangle join
with c1 as (
select t1.grp, t1.Element e1, t2.Element e2
from tbl t1
join tbl t2 on t1.grp = t2.grp and t1.Element < t2.Element
)
select e1,e2
from c1
group by e1,e2
order by count(*) desc
limit 1

how to Compare two tables and retrieve matched and unmatched data in sql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have two table with different column name and most of the values are same . I want the matched and unmatched data from the other table.
***
src-table
--------------------------------------------------------------------------------
eid | ename | email | country
--------------------------------------------------------------------------------
1 as as#gmail.com india
2 bs bs#gmail.com usa
3 cs cs#gmail.com usa
4 ds ds#gmail.com india
--------------------------------------------------------------------------------
tgt_table
--------------------------------------------------------------------------------
eid | ename | email | country
--------------------------------------------------------------------------------
1 as as#gmail.com india
2 bs b#gmail.com india
3 cs cs#gmail.com usa
4 ds d#gmail.com india
--------------------------------------------------------------------------------
expected output for matched_data
--------------------------------------------------------------------------------
src_coloumnname | src_data | tgt_colomnname | tgt_data
--------------------------------------------------------------------------------
eid 1 eid 1
eid 2 eid 2
eid 3 eid 3
eid 4 eid 4
ename as ename as
ename bs ename bs
ename cs ename cs
ename ds ename ds
email as#gmail.com email as#gmail.com
email cs#gmail.com email cs#gmail.com
country india country india
country usa country usa
country india country india
----------------------------------------------------------------------------
***
similarly unmatched records
how can i achieve that? can some one plz help me?
It could be done using one column at a time:
SELECT 'eid' AS match_column, l.eid AS src_value, r.eid AS tgt_value, CASE WHEN l.eid = r.eid THEN 'match' ELSE 'no match' END AS result
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'ename', l.ename, r.ename, CASE WHEN l.ename = r.ename THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'email', l.email, r.email, CASE WHEN l.email = r.email THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'country', l.country, r.country, CASE WHEN l.country = r.country THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
It matches columns where the rows match. It does not check rows that are missing from either table.
you can do a simple join, i think you can do this
SELECT * FROM T1 FULL OUTER JOIN T2 ON TRUE
or
SELECT * FROM T1 , T2
for everyting
SELECT * FROM T1 FULL OUTER JOIN T2 ON T1.C1 <> T2.T1_C1
for the unmatched
SELECT * FROM T1 FULL OUTER JOIN T2 ON T1.C1 = T2.T1_C1
for matched
you can also change type of join for matching
here the join documentation
https://www.w3schools.com/sql/sql_join.asp
you can also
SELECT T1.C1, T2.C1,T1.C2, ..., CASE WHEN T1.C1=T2.T1_C1 THEN 'MATCH' ELSE 'NOT MATCH' END FROM T1,T2
for have you result with the evidence of matching
this is the documenation of case
https://www.w3schools.com/sql/sql_case.asp

How to append one table column records into second table in SQL Server? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a two tables:
Employee:
EmployeeID First Name Last Name
------------------------------------
1 ABC XYZ
2 DEF LMN
3 GHI OPQ
Conversation:
ConversationId FromUser ToUser
-----------------------------------
11 1 2
22 1 3
33 2 1
44 3 2
I want output as follows
ConversationId FromUserId ToUserId FromUser ToUser
---------------------------------------------------------------
11 1 2 ABC DEF
22 1 3 ABC GHI
33 2 1 DEF ABC
44 3 2 GHI DEF
JOIN the Employee table twice, once for from user, and once for to user.
select c.*, fu.firstname, tu.firstname
from conversation c
join Employee fu on c.FromUser = fu.EmployeeID
join Employee tu on c.ToUser = tu.EmployeeID
SELECT A.* , B.FIRSTNAME FromUser , C.FIRSTNAME ToUser FROM
conversation A LEFT JOIN EMPLOYEE B ON
A.FromUser = B.EmployeeID
LEFT JOIN
EMPLOYEE C ON A.ToUser = B.EmployeeID
Try like this,
SELECT c.conversationId
,c.Fromuser AS FromUserId
,c.ToUser AS ToUserId
,frm.FirstName AS FromUser
,t.FirstName AS ToUser
FROM conversation c
INNER JOIN employee frm ON c.fromuser = frm.EmployeeId
INNER JOIN employee t ON c.touser = t.EmployeeId

Order by data depending on a specific value of column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am using Sql server 2008, i need to order by the following data:
Id PId Name
1 1 A
1 2 G --value to consider
1 3 C
2 1 A
2 2 B --value to consider
2 3 C
3 1 A
3 2 D --value to consider
3 3 C
result should look something like:
Id PId Name
2 1 A
2 2 B
2 3 C
3 1 A
3 2 D
3 3 C
1 1 A
1 2 G
1 3 C
i have tried different combinations in order by clause but of no use like:
order by Name, PId
order by PId, Name, Id
select T1.*
from Table1 as t1
left outer join Table1 as T2 on T2.id = T1.id and T2.Pid = 2
order by T2.Name, T1.Pid
sql fiddle demo
Your question isn't clear about the ordering you want, but if you want to order by the Name associated with the same Id for PId=2, then by Pid, try
select Id, PId, Name
from t
order by (
select Name from t as t2
where t2.Id = t.Id
and t2.PId = 2
), PId

Issue with SQL Query with Group By [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Table 1
Id(Key) Name
1 XYZ
2 ABC
Table 2
ID(Key) Bank
1 SC
2 CP
Table 3
Id(no key) month year noofduty
1 03 12 20
2 04 12 22
1 03 12 25
2 04 12 15
Required Result
ID Name Bank TotalDuty
1 XYZ SC 42
2 ABC CP 40
I am confused with GroupBy in multiple tables, a help is required. using SQL 2000 Server
Here is a SQL Fiddle with a Demo
SELECT t1.id
, t1.name
, t2.bank
, sum(t3.noofduty) as totalduty
FROM t1
INNER JOIN t3
on t1.id = t3.id
INNER JOIN t2
ON t2.id = t3.id
GROUP BY t1.id, t1.name, t2.bank