Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem with PIVOT query

Author  Topic 

fbaker
Starting Member

9 Posts

Posted - 2008-02-27 : 12:41:51
(SQL Server 2005)
I have the following query (trying to execute in Managment Studio):

SELECT Rate, Lender, Pricing, Max(Pricing) AS Maximum, Min(Pricing) As Minimum, Avg(Pricing) As Median
FROM [10_Tier].[dbo].[Temp10Tier_1000]
PIVOT (Sum(Pricing) For Lender)

I get the error: Incorrect syntax near ')' Microsoft SQL Server Error 102

I've been looking all over and have found many examples but I cannot get any of them to work.

What I want to accomplish is to be able to create a pivot query and use it in the "Query Builder" when designing a report for the reporting services, so that I can have a table that mimicks a "Matrix" - so that I can have multiple value columns.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-27 : 12:48:45
quote:
Originally posted by fbaker

(SQL Server 2005)
I have the following query (trying to execute in Managment Studio):

SELECT * FROM
(SELECT Rate, Lender, Pricing, Max(Pricing) AS Maximum, Min(Pricing) As Minimum, Avg(Pricing) As Median
FROM [10_Tier].[dbo].[Temp10Tier_1000]) m
PIVOT (Sum(Pricing) For Lender IN (columnlist))p

I get the error: Incorrect syntax near ')' Microsoft SQL Server Error 102

I've been looking all over and have found many examples but I cannot get any of them to work.

What I want to accomplish is to be able to create a pivot query and use it in the "Query Builder" when designing a report for the reporting services, so that I can have a table that mimicks a "Matrix" - so that I can have multiple value columns.




give columnlist as values inside Lender column which you want to come as seperate columns ([Val1],[Val2],....)

If column values are not definite & varies at runtime hold the above query batch in a string variable and execute dynamically
Go to Top of Page

fbaker
Starting Member

9 Posts

Posted - 2008-02-27 : 13:41:12
Ok, I was able to get the query to work by specifying a lender I knew was in the table in the IN clause. Thank you.

I have another question but I should give you some background first. I'm designing a report for a web site, there is a web page that generates the data in this Temp10Tier_1000 table based on their selections. I will need the report to use the query I got working - which is fine for design time - but when I go to plug it into my web page and dynamically asign the report data, how do I do what you are saying by storing the query in a string and executing it dynamically?

I don't know if I'm making sense. The only way I can see doing it is by querying the table first in my code to see what lenders there are, and then build the IN part of the SQL Statment by looping through the lenders available.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-28 : 00:29:49
quote:
Originally posted by fbaker

Ok, I was able to get the query to work by specifying a lender I knew was in the table in the IN clause. Thank you.

I have another question but I should give you some background first. I'm designing a report for a web site, there is a web page that generates the data in this Temp10Tier_1000 table based on their selections. I will need the report to use the query I got working - which is fine for design time - but when I go to plug it into my web page and dynamically asign the report data, how do I do what you are saying by storing the query in a string and executing it dynamically?

I don't know if I'm making sense. The only way I can see doing it is by querying the table first in my code to see what lenders there are, and then build the IN part of the SQL Statment by looping through the lenders available.


Are you using queries inline with the application. You can also put the query into a stored procedure and call it from your application. You dont need to loop through table to get list of Lenders. You can use FOR XML PATH('') feature to build a string of lenders before you give it to PIVOT. something like


DECLARE @Lenderlist varchar(2000),@Sql varchar(8000)

SELECT @Lenderlist= LEFT(ll.Lenderlist,LEN(ll.Lenderlist)-1)
FROM
(SELECT Lender + ',' as [text()]
FROM [10_Tier].[dbo].[Temp10Tier_1000]
FOR XML PATH(''))ll(Lenderlist)

SELECT @Sql='SELECT * FROM
(SELECT Rate, Lender, Pricing, Max(Pricing) AS Maximum, Min(Pricing) As Minimum, Avg(Pricing) As Median
FROM [10_Tier].[dbo].[Temp10Tier_1000]) m
PIVOT (Sum(Pricing) For Lender IN ([' + REPLACE(@Lenderlist,',','],['+']))p'

EXEC(@Sql)

Go to Top of Page
   

- Advertisement -