Solved

return recordset in 1 row with multiple values from a table

Posted on 2013-05-19
11
222 Views
Last Modified: 2013-05-27
Hi,

I have a table that holds the prices of a product for each state tbWholesalerProductPrice, I'd like to run an efficient query that returns data for the product tbWholesalerProduct and the price for each state

Right now in the code below I get a row for each price however I'd like a column in each row with each price instead

Name, State, Price
Product1, NSW,$1
Product1, QLD,$1.50

instead
Name, NSW, QLD, ACT, VIC, SA, TAS, WA, NT
Product1, $1, $1.1,$1,$1.3,$2,$1,$2,$1.3

select P.*,
            WPP.vState, WPP.mPrice
from  tbWholesalerproduct P ON P.cstatus = 'A'
                        and iProductid= 123
                        and W.iWholesalerID = P.iWholesalerID
left join tbWholesalerproductPrice WPP ON P.iWholeSalerProductID = WPP.iWholeSalerProductID            
where W.iClientid =1

Any ideas appreciated
0
Comment
Question by:louise_8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39180179
You need to use PIVOT

SELECT Name, [NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT]
FROM
(select P.Name,
            WPP.vState, WPP.mPrice
from  tbWholesalerproduct P ON P.cstatus = 'A'
                        and iProductid= 123
                        and W.iWholesalerID = P.iWholesalerID
left join tbWholesalerproductPrice WPP ON P.iWholeSalerProductID = WPP.iWholeSalerProductID            
where W.iClientid =1) AS Sourse
PIVOT
(
Sum(mPrice) For
vState IN ([NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT])
) AS pvt
ORDER BY Name

Open in new window

0
 

Author Comment

by:louise_8
ID: 39180211
Thanks Chaau, good idea although

Getting error
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'ON'.
0
 
LVL 25

Expert Comment

by:chaau
ID: 39180225
The error is in your original statement here : from  tbWholesalerproduct P ON P.cstatus = 'A'. Please fix it.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:louise_8
ID: 39180249
Sorry my bad, I've fixed it to
SELECT Name, [NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT]
FROM
(select P.Name,
         WPP.vState, WPP.mPrice
from  tbWholesalerproduct P
left join
tbWholesalerproductPrice WPP ON
                         P.iProductid= 123
                        and W.iWholesalerID = P.iWholesalerID  
                        and P.cstatus = 'A'            
where W.iClientid =1) AS Sourse
PIVOT
(
Sum(mPrice) For
vState IN ([NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT])
) AS pvt
ORDER BY Name

getting this error: Msg 170, Level 15, State 1, Line 12
Line 12: Incorrect syntax near 'PIVOT'.

The line SELECT Name, [NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT] -is the column Name supposed to match a table name perhaps
0
 
LVL 25

Expert Comment

by:chaau
ID: 39180307
What version of SQL server do you have?
0
 
LVL 7

Accepted Solution

by:
manthanein earned 150 total points
ID: 39180525
Not an elegant solution.. but hope it works..

select iClientID,  
	sum(case(WPP.vState when 'NSW' then  WPP.mPrice) NSW,
	sum(case(WPP.vState when 'QLD' then  WPP.mPrice) QLD,
	sum(case(WPP.vState when 'ACT' then  WPP.mPrice) ACT,
	sum(case(WPP.vState when 'VIC' then  WPP.mPrice) VIC,
	sum(case(WPP.vState when 'SA' then  WPP.mPrice) SA,
	sum(case(WPP.vState when 'TAS' then  WPP.mPrice) TAS,
	sum(case(WPP.vState when 'WA' then  WPP.mPrice) WA,
	sum(case(WPP.vState when 'NT' then  WPP.mPrice) NT

from (
	select P.*,
        WPP.vState, WPP.mPrice
	from  tbWholesalerproduct P ON P.cstatus = 'A' and iProductid= 123 and W.iWholesalerID = P.iWholesalerID
	left join tbWholesalerproductPrice WPP ON P.iWholeSalerProductID = WPP.iWholeSalerProductID            
) 
group by iClientid
where W.iClientid =1 

Open in new window

0
 

Author Comment

by:louise_8
ID: 39183046
sql server 2008

cheers
0
 
LVL 25

Expert Comment

by:chaau
ID: 39183084
Hi, here is the SQL fiddle with a simple SQL Fiddle with the pilot.
0
 

Author Comment

by:louise_8
ID: 39183305
Chaau

Thanks for the comments, I updated my query as per your fiddle
to SELECT Name, [NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT]
FROM
(select
         vState, mPrice
from  tbWholesalerproductPrice) AS Sourse
PIVOT
(
Sum(mPrice) For
vState IN ([NSW], [QLD], [ACT], [VIC], [SA], [TAS], [WA], [NT])
) AS pvt
ORDER BY Name

I still get the error

Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near 'PIVOT'.
0
 
LVL 25

Expert Comment

by:chaau
ID: 39183499
It is either your database version is not 2008, or something else. Can you run select @@version

anyway, you can always do this with case, as the other expert suggested above
0
 

Author Closing Comment

by:louise_8
ID: 39200109
Thanks manthanein
I'd to amend a little but your, query is the basis for whats working now thank you

Chaau, thank you too unfortunately our sql version cant use pivot
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question