Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

SQL Server dynamic pivot/cross tab?

I have a column that has dates and a column that has info. I need the dates to go across the top as a header and the data (NetInv) to be underneath. There isn't a set # of dates and they vary from case to case.

This it what I tried but I get the error

"Msg 206, Level 16, State 2, Line 3

Operand type clash: date is incompatible with float"

Here is my code:

DECLARE @query VARCHAR(4000)

DECLARE @date VARCHAR(2000)

SELECT @date = STUFF(( SELECT DISTINCT

'],[' + ltrim(str(ecodate))

FROM dbo.DateP

ORDER BY '],[' + ltrim(str(ecodate))

FOR XML PATH('')

), 1, 2, '') + ']'

SET @query =

'SELECT * FROM

(

SELECT PHDWinID,EcoDate,NetInv

FROM view.DateP

)t

PIVOT (SUM(NetInv) FOR EcoDate

IN ('+@date+')) AS pvt'

EXECUTE (@query)

Thank you for your help.

1 Answer

Relevance
Still have questions? Get your answers by asking now.