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
Solved

return recordset in 1 row with multiple values from a table

Posted on 2013-05-19
11
206 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query and VBA 5 46
Error when creating an UPDATE Trigger in SQL 6 21
Dynamic SQL select query 4 38
SQL R 21 28
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

829 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