SQL Join on multiple columns of a row - sql

I have the following table IDDetails:
ID1 ID2 ID3
1 2 3
1 5 7 and so on
I want to link each ID of the row with a name from the Names table:
ID Name
1 A
2 B
3 C
4 D
5 E
7 G
The output should have 6 columns like :
ID1 Name1 ID2 Name2 ID3 Name3
1 A 2 B 3 C
1 A 5 E 7 G
The operation should have minimum joins with limited cost.

This query will give you the results you want. Note that you have to JOIN the Names table 3 times to get the three different names for each row.
SELECT i.ID1, n1.Name AS Name1,
i.ID2, n2.Name AS Name2,
i.ID3, n3.Name AS Name3
FROM IDDetails i
JOIN Names n1 on n1.ID = i.ID1
JOIN Names n2 on n2.ID = i.ID2
JOIN Names n3 on n3.ID = i.ID3
Output:
ID1 Name1 ID2 Name2 ID3 Name3
1 A 2 B 3 C
1 A 5 E 7 G
Update
Here is a query without JOINs, as requested in the edit after the original post:
SELECT i.ID1, (SELECT Name FROM Names n WHERE n.ID = i.ID1) AS Name1,
i.ID2, (SELECT Name FROM Names n WHERE n.ID = i.ID2) AS Name2,
i.ID3, (SELECT Name FROM Names n WHERE n.ID = i.ID3) AS Name3
FROM IDDetails i
Output:
ID1 Name1 ID2 Name2 ID3 Name3
1 A 2 B 3 C
1 A 5 E 7 G
SQLFiddle Demo of both queries

Related

combining similar rows with two same values

I have three tables: users, products and reviews. I'm trying to get form a table that would show products that have gotten the same review from different users, the users who reviewed it and what review it got.
Here are the tables and the output I'm looking for:
Users
uid uname
1 name1
2 name2
3 name3
4 name4
Products
pid pname
1 A
2 B
3 C
4 D
Reviews
pid uid grade
1 1 3
1 2 2
1 3 3
2 1 4
3 2 1
2 2 4
4 3 1
Desired output:
uname uname2 pname grade
name1 name3 A 3
name3 name1 A 3
name1 name2 B 4
name2 name1 B 4
There are some overly complicated answers here.
Its pretty simple using a self join like this:
select u1.uname, u2.uname, p.pname, r1.grade
from review r1
join review r2 on r2.pid=r1.pid and r2.grade=r1.grade and r2.uid<>r1.uid
join products p on p.pid=r1.pid
join users u1 on u1.uid=r1.uid
join users u2 on u2.uid=r2.uid
order by pname, r1.grade, u1.uname, u2.uname
Result:
uname uname1 pname grade
name1 name3 A 3
name3 name1 A 3
name1 name2 B 4
name2 name1 B 4
select users.uname, reviews.grade, products.pname from products
join reviews on products.id = reviews.pid
join users on users.id = reviews.id
This can be done using a self-join like this -
with combined as
(select (select uname from users where uid = r.uid),
(select pname from products where pid = r.pid),
r.grade,
r.uid
from reviews r)
select c1.uname as name1, c2.uname as name2, c1.pname, c1.grade
from combined c1, combined c2
where c1.grade = c2.grade
and c1.pname = c2.pname
and c1.uid <> c2.uid;

Select particular result randomly from a table for a certain partition

I want to select record corresponding to 'B' whenever there are duplicates for a name. If there's no duplicate I want to display the record. Refer to the sample table [TableInfo]. Please help me with the SQL query.
TableInfo
Name Type Value
------------------------
Name1 A 5
Name1 B 10
Name1 C 11
Name5 B 88
Name5 C 98
Name6 A 24
Name6 B 21
Name2 B 21
Name3 C 55
Name4 A 74
The expected result:
Name Type Value
------------------------
Name1 B 10
Name5 B 88
Name6 B 21
Name2 B 21
Name3 C 55
Name4 A 74
I think you want this:
select i.*
from info i
where type = 'B'
union all
select i.*
from info i
where not exists (select 1 from info i2 where i2.name = i.name and i2.type = 'B');

Selecting a Column based on Child and Parent Category

I Have a SQL server 2008 Database. I have a table with the following structure
CatID ParentCatID NAME Level
1 NULL A 1
2 1 B 2
3 2 C 3
4 NULL D 1
5 4 E 2
6 5 F 3
7 NULL G 1
8 7 H 2
I want to Select the Name column in Heirarchical format like below:
Level1 Level2 Level3
A B C
D E F
In my Table all level1 categories have level2 children. Similarly all level2 children have level3 children. So how can i get the data in my desired format.
It's a simple query like this:
select
C1.NAME as Level1,
C2.NAME as Level2,
C3.NAME as Level3
from Categories C1
inner join Categories C2
on C2.ParentCatId = C1.CatId
inner join Categories C3
on C3.ParentCatId = C2.CatId
where C1.Level = 1 and C2.Level = 2 and C3.Level = 3 and

SQL Returning Results based on an indirect criteria

I am very new to sql so I hope this explanation makes sense.
I would like to return sales pries. Find all ID1's based on ID3 when given ID2
My tables looks like this
Table 1 Table 2
ID1 Sales ID1 ID2 ID3
A 10 A 0 #
B 20 B 1 #
C 30 C 2 #
D 40 D 3 *
E 50 E 4 $
F 60 F 5 $
G 70 G 6 #
H 80 H 7 %
I 90 I 8 %
Result when looking for ID2 = 0 would be
Results ID2 = 0
ID1 Sales
A 10
B 20
C 30
G 70
select ID1,Sales
from table1
where ID1 in (select ID1 from table2 where ID3 in ( select ID3 from table2 where ID2 = 0))
Thanks Everyone for your help. I managed to get my answer by using the following. Your help is much appreciated, I learned a lot
select ID1,Sales from table1 where ID1 in(select ID1 from table2 where ID3 in (select ID3 from table where ID2=0))
So you need three pieces of information.
The first is what id3 value are you looking for?
The answer is '#'.
Then you want to get all of the id1 values that have that id3 value.
Finally you want to return a result set of id1 and sales.
select -- step 3
table1.id1
,table1.sales
from
table1
inner join -- step 2
table2
on table1.id1 = table2.id1
inner join (
select
id3
from
table2
where
id2 = 0 ) as subquery -- step 1
on table2.id3 = subquery.id3

How to get value instead of fieldname in Unpivot query

This is my query
Declare #SampleUNPivot Table(ID int ,Name varchar(50),a int,b int,c int,d int)
insert into #SampleUNPivot values(1,'name1',1,2,3,4)
insert into #SampleUNPivot values(2,'name2',10,20,30,40)
insert into #SampleUNPivot values(3,'name3',11,21,31,41)
insert into #SampleUNPivot values(4,'name4',14,24,34,44)
Select ID,Name,[SampleValue]
From (
Select ID,name,a,b,c,d from #SampleUNPivot) orig
UNPIVOT
( quantity for [SampleValue] in (a,b,c,d)) as UNPT
------------Current Result-------------
ID Name SampleValue
1 name1 a
1 name1 b
1 name1 c
1 name1 d
2 name2 a
2 name2 b
2 name2 c
2 name2 d
3 name3 a
3 name3 b
3 name3 c
3 name3 d
4 name4 a
4 name4 b
4 name4 c
4 name4 d
Please correct the query above to give
Results like
ID Name SampleValue
1 name1 1
1 name1 2
1 name1 3
1 name1 4
and so on..................
get value instead of fieldname in Unpivot query
That's your quantity field.
SELECT ID,Name,[SampleValue], quantity
FROM ( SELECT ID,name,a,b,c,d from #SampleUNPivot) orig
UNPIVOT ( quantity for [SampleValue] in (a,b,c,d)) as UNPT