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
Related
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;
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!
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');
i've been trying to build this query. hoping someone can help.
I have 2 tables.
1 table contains
Code | name | Value | Period
1 name1 1 2010
2 name2 2 2010
table 2 contains
code | name |
1 name1
2 name2
3 name3
4 name4
what i want to be displayed is
1 name1 1
2 namw2 2
3 name3 0
4 name4 0
In some instances table 1 may have a value for all name variables in table 2
but where there are only 1,2,3 names i want it to display the other one but with a value of 0 or blank.
Try this:
select
T2.*,
isnull(T1.code, 0) as code -- or value
from
table2 T2
left outer join table1 T1 on T1.name = T2.name
You can replace isnull(T1.code, 0) as code with isnull(T1.value, 0) as value. I'm not sure what you're after ...
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