How to get value instead of fieldname in Unpivot query - sql

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

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

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

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

Concatenate a single Column value depending on Condition

Below are the 3 tables
QuotationMaster
QuoteID QuoteNo CustomerName
-----------------------------
1 Q1 Name1
2 Q2 Name2
3 Q3 Name3
4 Q4 Name4
5 Q5 Name5
QuoteItemDetails : one quote can have many items
QuoteItemID QuoteID ItemCode ItemID
---------------------------------------------
1 1 100 1
1 1 200 2
2 2 200 2
QuoteBatchDetails : one QuoteItem can have many batches of QuoteID and ItemID are the common columns. BatchNo is varchar
QuotebatchID QuoteID BatchNo ItemID
---------------------------------------------
1 1 A 1
2 1 B 1
3 1 C 2
4 2 E 2
5 2 F 2
I want the result as
QuoteID QuoteNo CustName ItemCode BatchNo
-------------------------------------------------
1 Q1 Name1 100 A,B
1 Q1 Name1 200 C
2 Q2 Name2 200 E,F
I want to create a procedure which takes QuoteID as parameter of INT type and get the result as above.
The only problem I am facing is to concatenate the BatchNo which depends on ItemID and further on QuoteID.
Using the below query I am able to concatenate the BatchNo for a particular ID but I am not sure how to add this to the main procedure, when I do that errors pops up like subquery returns more than one value.I understand because for every quote there can be more than 1 item.
select
ID.QuoteID,ID.ItemID,
stuff((select ', ' + BatchNo
from SD_QuoteBatchDetails BD where ID.ItemID=BD.ItemID and ID.QuoteID=BD.QuoteID
for xml path('')),
1,2,'') [Values]
from SD_QuoteItemDetails QID,SD_QuoteBatchDetails ID where ID.QuoteID=QID.QuoteID
group by ID.ItemID,ID.QuoteID
Can anyone provide a query for the same.
SELECT b.QuoteItemID,
a.QuoteNo,
a.CustomerName,
b.ItemCode,
c.BatchList
FROM QuotationMaster a
INNER JOIN QuoteItemDetails b
ON a.QuoteID = b.QuoteID
INNER JOIN
(
SELECT
QuoteID,
ItemID,
STUFF(
(SELECT ', ' + BatchNo
FROM QuoteBatchDetails
WHERE QuoteID = a.QuoteID AND
ItemID = a.ItemID
FOR XML PATH (''))
, 1, 1, '') AS BatchList
FROM QuoteBatchDetails AS a
GROUP BY QuoteID, ItemID
) c ON b.QuoteID = c.QuoteID AND
b.ItemID = c.ItemID;
SQLFiddle Demo