Select particular result randomly from a table for a certain partition - sql

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

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;

SQL Join on multiple columns of a row

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

joining one table to one column in table2

I am still learning SQL and wanted to query a table and needed your help. I have two tables.
Table1
GR_ID US_ID
1 51
1 52
1 53
2 51
2 54
2 55
3 51
3 52
Table2
MEM_ID MEM_Name
1 Name1
2 Name2
3 Name3
51 Name51
52 Name52
53 Name53
54 Name54
55 Name55
Result expecting, to display table1 but with the names associated with ID from Table2.
Result
Group Users
Name1 Name51
Name1 Name52
Name1 Name53
Name2 Name51
Name2 Name54
Name2 Name55
Name3 Name51
Name3 Name52
This should work for both MySQL and SQL Server:
SELECT b.mem_name as groups,
c.mem_name as users
FROM Table1
JOIN Table2 as b
ON b.mem_id = gr_id
JOIN Table2 as c
ON c.mem_id = us_id
ORDER BY groups
Hope this helps!

SQL query to remove duplicate column values within distinct rows

I am using MS SQL, I have tables "Table1" , "Table2" , "Table3" as mentioned below(might look like a mess, I dont know how to make a table better here).
I want the "Expected OutPut" as mentioned ant the bottom.
Want a SQL script to perform this action.
Table 1
FamilyID FamilyName
1 One
2 Two
3 Three
Table2
FamilyID UserID UserName
1 1 Name1
1 2 Name2
1 3 Name3
2 4 Name4
2 5 Name5
2 6 Name6
2 7 Name7
2 8 Name8
3 9 Name9
3 10 Name10
Table3
FamilyID RoomID RoomType
1 1 RoomType1
1 2 RoomType2
2 1 RoomType1
2 2 RoomType2
2 3 RoomType3
2 2 RoomType2
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
2 Name4 RoomType1
Name5 RoomType2
Name6 RoomType3
Name7
Name8
3 Name9 RoomType2
Name10
EDIT:
I tried how to show column value only one time if it is repeated and blank until different value comes in sql but the out put is not as i expected. It has duplications as sample shown below for FamilyID=1
Result from how to show column value only one time if it is repeated and blank until different value comes in sql
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType1
Name3 RoomType1
Name1 RoomType2
Name2 RoomType2
Name3 RoomType2
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
You can use a solution like this:
SELECT T1.FamilyID,T2.UserName,T3RoomType FROM Table1 T1
JOIN Table2 T2 ON T1.FamilyID =T2.FamilyID
LEFT JOIN Table3 T3 ON T2.UserID =T3.RoomID
GROUP BY T2.UserID

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