Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

return recordset in 1 row with multiple values from a table

Posted on 2013-05-19
11
Medium Priority
?
226 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 450 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

609 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