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.
| Author |
Topic |
|
freekrubens
Starting Member
4 Posts |
Posted - 2010-09-30 : 07:53:06
|
| Hi Guys,I'm new to the forum so i'd like to thank you first to take a look at my problem.The situation is quiet simple. I have 2 tables:- a tabel with product information - No_ - Description - Category- a table with purchase prices - No_ - purchase_priceThe data look like this:product information:No_ Description Category12244 beer glass glass25200 patato knife knifes21458 grill pan cookwarepurchase prices:No_ purchase_price12244 3,0025200 2,7525200 2,8112244 3,1121458 26,3812244 3,08As you notice the table purchase prices containst an entry for every time this item is purchase. The number of times an item is purchased is not fixed. It could be 1 or 2 or 8 times. I can say it won't be more then 10 times.In this case item 12244 was purchased 3 times, 255200 was purchased 2 times and 21458 was purchased 1 time.Now, i'm looking for a query that can display 1 table containing all this data in the following format:No_ Description Category pp1 pp2 pp3 12244 beer glass glass 3,00 3,11 3,0825200 patato knife knifes 2,75 2,8121458 grill pan cookware 26,38I know that there PIVOT can transform row data to column data but from my understanding i need to use an agregated function. But in my second table i have nothing to group or sum, do i?What query should i use to get the desired result?Regards,Freek |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-30 : 08:39:36
|
Try this:#assuming that the purchase is not going to be more than 10.SELECT no,description,category ,[1],[2],[3],[4],[5],[6],[7], ,[9],[10]FROM(SELECT P.no,description,category,purchase_price,rank() OVER(Partition by P.no ORDER BY P.purchase_price) AS PurNumFROM product_information IINNER JOIN purchase_prices P ON I.no=P.no )A PIVOT (MAX(purchase_price) FOR PurNum IN([1],[2],[3],[4],[5],[6],[7], ,[9],[10])) AS pvot |
 |
|
|
freekrubens
Starting Member
4 Posts |
Posted - 2010-09-30 : 09:31:44
|
| Hi rohitvishwakarma,I'm not sure what i'me doing but this code works!I really would like to understand what this statement does.This part: rank() OVER(Partition... a have never used before.If you find the time to explain in 2 words what it does exactly do here, that would be great.If not, a bigg thanks for the solution!regards,Freek |
 |
|
|
freekrubens
Starting Member
4 Posts |
Posted - 2010-09-30 : 09:50:28
|
| I did notice something in the result.Lets say record A has 2 purchase prices and record B has 2 purchase prices.In the result I have 10 columns for the prices but the result is displayed for like this:NO_ 1 2 3 4 5 6 7 8 9 A 3,00 3,11 NULL NULL NULL NULL NULL NULL NULLB 2,50 NULL 2,49 NULL NULL NULL NULL NULL NULLWhy are the prices for record B displayed in column 1 and 3 in stead off column 1 and 2 like record A?Many thanks for your support!regards,Freek |
 |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-30 : 10:00:29
|
quote: Originally posted by freekrubens I did notice something in the result.Lets say record A has 2 purchase prices and record B has 2 purchase prices.In the result I have 10 columns for the prices but the result is displayed for like this:NO_ 1 2 3 4 5 6 7 8 9 A 3,00 3,11 NULL NULL NULL NULL NULL NULL NULLB 2,50 NULL 2,49 NULL NULL NULL NULL NULL NULLWhy are the prices for record B displayed in column 1 and 3 in stead off column 1 and 2 like record A?Many thanks for your support!regards,Freek
I have got the following sample data in my tables:product_informationno description category12244 beer glass glass25200 potato knife knife21458 grill pan cookwarepurchase_pricesno purchase_price12244 30025200 27525200 28112244 31121458 263812244 30821458 500021458 1231and my result using the query:SELECT no,description,category ,[1],[2],[3],[4],[5],[6],[7], ,[9],[10]FROM(SELECT P.no,description,category,purchase_price,rank() OVER(Partition by P.no ORDER BY P.purchase_price) AS PurNumFROM product_information IINNER JOIN purchase_prices P ON I.no=P.no )APIVOT (SUM(purchase_price) FOR PurNum IN([1],[2],[3],[4],[5],[6],[7], ,[9],[10])) AS pvot is:[no][description ][category][1][2][3][4][5][6][7] [9][10]12244 beer glass glass 300 308 311 NULL NULL NULL NULL NULL NULL NULL21458 grill pan cookware 1231 2638 5000 NULL NULL NULL NULL NULL NULL NULL25200 potato knife knife 275 281 NULL NULL NULL NULL NULL NULL NULL NULLand there is no null for me in between |
 |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-30 : 10:05:52
|
run this query:SELECT P.no,description,category,purchase_price,rank() OVER(Partition by P.no ORDER BY P.purchase_price) AS PurNumFROM product_information IINNER JOIN purchase_prices P ON I.no=P.no and the o/p using my sample data is: no description category purchase_price PurNum12244 beer glass glass 300 112244 beer glass glass 308 212244 beer glass glass 311 321458 grill pan cookware 1231 121458 grill pan cookware 2638 221458 grill pan cookware 5000 325200 potato knife knife 275 125200 potato knife knife 281 2The Rank() will rank the column within a group defined by Partition by(in our case it is no i.e product number).There is also ORDER BY present in the query which will order the data within a group on purchase_price in Ascending orderfor eg: take the case of product number 12244 the purchase price is ranked in ascending order and the ranks given are 1,2 and 3.Similarly the ranks for other two groups i.e 21458 & 25200 are1,2,3 & 1,2 respectively.After doing the pivot operation on the Rank we will get the columns 1,2 & 3 filled for products 12244 & 21458 and columns 1 & 2 filled for 25200. Hope I explained it well. working in office If not feel free to post hereThanksRohit |
 |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
|
|
freekrubens
Starting Member
4 Posts |
Posted - 2010-09-30 : 11:20:27
|
| Thanks again for the answer.I'm leaving for 1 week holiday now but will come back to this isuue.A ran the query that you asked and it returned value 3 but there where only 2 lines in de table.I did use other data to run your code, but most of the data looks ok except for the NULL values in between.regards, |
 |
|
|
|
|
|
|
|