Solved

return recordset in 1 row with multiple values from a table

Posted on 2013-05-19
11
178 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
  • 5
  • 5
11 Comments
 
LVL 24

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 24

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
 

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 24

Expert Comment

by:chaau
ID: 39180307
What version of SQL server do you have?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 24

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 24

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now