[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

return recordset in 1 row with multiple values from a table

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
louise_8
Asked:
louise_8
  • 5
  • 5
1 Solution
 
chaauCommented:
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
 
louise_8Author Commented:
Thanks Chaau, good idea although

Getting error
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'ON'.
0
 
chaauCommented:
The error is in your original statement here : from  tbWholesalerproduct P ON P.cstatus = 'A'. Please fix it.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
louise_8Author Commented:
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
 
chaauCommented:
What version of SQL server do you have?
0
 
manthaneinCommented:
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
 
louise_8Author Commented:
sql server 2008

cheers
0
 
chaauCommented:
Hi, here is the SQL fiddle with a simple SQL Fiddle with the pilot.
0
 
louise_8Author Commented:
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
 
chaauCommented:
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
 
louise_8Author Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now