Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can i use the pivot with selecting top three values


for example i am inserting the prices of products at different different dates now i want to fetch last three updated prices for all products at the same time in the manner
headings like

Product name.......OldDate....NewDate....NewerDate
Posted
Updated 13-Oct-12 1:43am
v2
Comments
OriginalGriff 13-Oct-12 6:59am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps if you give an example?
Use the "Improve question" widget to edit your question and provide better information.
sreekanthv12 13-Oct-12 22:56pm    
please give your table or please provide in details your question couldn't understand.
Sandeep Mewara 14-Oct-12 2:45am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Pradeep_kaushik 15-Oct-12 7:39am    
my question is just like the description in the below answer but want this format for only last three updating dates
and i am using this query in my project through the in build sqlserver of VS2008

1 solution

Yes, you can use pivots !

Have a looks at this example:
SQL
CREATE TABLE #Prices (ProductName NVARCHAR(30), Price MONEY, UpdateDate DATETIME)

INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('ABC',2.20,'2012-01-03')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('ABC',2.85,'2012-03-07')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('ABC',3.01,'2012-05-13')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('ABC',3.05,'2012-07-15')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('DEF',13.15,'2012-01-05')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('DEF',12.95,'2012-03-07')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('DEF',13.09,'2012-05-10')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('GHI',5.19,'2012-01-03')
INSERT INTO #Prices (ProductName, Price, UpdateDate)
	VALUES('GHI',5.88,'2012-07-15')

DECLARE @cols NVARCHAR(200)
DECLARE @dt NVARCHAR(1000)
DECLARE @pt NVARCHAR(2000)

SET @cols = STUFF((SELECT DISTINCT '],[' + CONVERT(NVARCHAR(10),P.[UpdateDate],120)
					FROM [#Prices] AS P
					ORDER BY '],[' + CONVERT(NVARCHAR(10),P.[UpdateDate],120)
			FOR XML PATH('')),1,2,'') + ']'

SET @dt = 'SELECT * ' + 
		'FROM #Prices ' 

SET @pt = 'SELECT [ProductName], ' + @cols + ' ' +
		'FROM (' + @dt + ') AS DT ' +
		'PIVOT(MAX([Price]) FOR [UpdateDate] IN (' + @cols + ')) AS PT ' +
		'ORDER BY [ProductName]'
EXEC (@pt)

DROP TABLE #Prices


Results:

ProductName2012-01-032012-01-052012-03-072012-05-102012-05-132012-07-15
ABC2.20NULL2.85NULL3.013.05
DEFNULL13.1512.9513.09NULLNULL
GHI5.19NULLNULLNULLNULL5.88


The rest belongs to you!
 
Share this answer
 
v2
Comments
Pradeep_kaushik 15-Oct-12 7:31am    
thanks But I want Price for only last three updated dates
Maciej Los 15-Oct-12 11:38am    
So where is a problem? To get 3 latest records you need to use: TOP and ORDER BY clause.
Sandeep Mewara 15-Oct-12 12:47pm    
I have no clue who can vote it 1! :doh:

Good effort, nice answer. 5!
Maciej Los 15-Oct-12 13:02pm    
Thank you, Sandeep ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900