Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 View from 2 tables / mulitple row to one

Author  Topic 

BryTack
Starting Member

2 Posts

Posted - 2014-05-02 : 09:08:25

Hi, Could someone please be kind and point me in the right direction for this problem.

I want to combine 2 tables into a view. The first table is a list of 'Products' (P1, P2.....)

Table: Product

ID User Start Date ..
=====================================
P1 Fred 12/04/2012
P2 Jane 01/12/2011
P3 Mike 01/12/2011
P4 Stu 01/12/2009
P5 Etc


Table two is qualities for each product. A product can have from zero to 8 qualities. Qualities have a description and a value (varchar).

Table: Qualities

Product Descrip.Value
=====================================
P1 Colour Red
P2 Colour Green
P2 Hard 2.2
P2 Weight 123
P3 Colour Black
P4 Colour White
P4 Weight 8.12
P6 Mass 12.2
P2 abc def Etc

I would like to create a view that joins these tables. Each row show a product and all the qualities it has, from quality 1 to quality 8. If there is no N'th quality, to show blank/null/zero.

The problem is how to get values from multiple rows (in table Qualities) in a single row in the view.



Wanted View
Product User Quality1 Value1 Quality Value2 Quality3 Value3 Quality4 Value4 etc..
=====================================
P1 Fred Colour Red 0 0 0 0 0 0 0
P2 Jane Colour Green Hard 2.2 Weight 123 abc def
P3 Mike Colour Black 0 0 0 0 0 0
P4 Stu Colour White Weight 8.12 0 0 0 0
etc

Many thnks





BryTack
Starting Member

2 Posts

Posted - 2014-05-02 : 09:25:36
Sorry for the terrible formatting and spelling. It is not easy writing on a small android device on a fast moving train!

Look forward to my enlightenment.
Bryan
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-02 : 11:00:44
This did it for me (for the first 4 quality/value pairs):


create table #p (id char(2), [User] varchar(20), StartDate date)

insert into #p values
('P1', 'Fred', '12/04/2012'),
('P2', 'Jane', '01/12/2011'),
('P3', 'Mike', '01/12/2011'),
('P4', 'Stu', '01/12/2009')

create table #q (Product char(2), Descrip varchar(20), Value varchar(20))

insert into #q values
('P1', 'Colour', 'Red'),
('P2', 'Colour', 'Green'),
('P2', 'Hard', '2.2'),
('P2', 'Weight', '123'),
('P3', 'Colour', 'Black'),
('P4', 'Colour', 'White'),
('P4', 'Weight', '8.12'),
('P6', 'Mass', '12.2')

select p.id, p.[User]
, max(case when rn = 1 then Descrip else '0' end) as Quality1
, max(case when rn = 1 then Value else '0' end) as Value1
, max(case when rn = 2 then Descrip else '0' end) as Quality2
, max(case when rn = 2 then Value else '0' end) as Value2
, max(case when rn = 3 then Descrip else '0' end) as Quality3
, max(case when rn = 3 then Value else '0' end) as Value3
, max(case when rn = 4 then Descrip else '0' end) as Quality4
, max(case when rn = 4 then Value else '0' end) as Value4
from
(
select Product, Descrip, Value,
rn = ROW_NUMBER() over(partition by product order by descrip)
from #q
) q
join #p p on p.id = q.product
group by p.id, p.[User]
Go to Top of Page
   

- Advertisement -