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 |
|
benking9987
Posting Yak Master
124 Posts |
Posted - 2011-11-04 : 15:03:05
|
| I think I have something wrong with me, but I can't figure out for the life of me how to generate the results I want. I ultimately want a record set of related products within my database. I have a table that has all of the criteria that I want to use to relate my products together. Essentially I am going to use a field called "Brand", "Color", "Gender", and "Total Sales" to see how items are related to eachother.here's what I'm after. I want the top 10 items (sorted descending by Total Sales) for each item. So, the data should look like this:Style1, Brand, Color, 10, RelatedStyle1Style1, Brand, Color, 8, RelatedStyle2Style1, Brand, Color, 6, RelatedStyle3Style1, Brand, Color, 5, RelatedStyle4Style1, Brand, Color, 4, RelatedStyle5Style1, Brand, Color, 4, RelatedStyle6Style1, Brand, Color, 3, RelatedStyle7Style1, Brand, Color, 2, RelatedStyle8Style1, Brand, Color, 1, RelatedStyle9Style1, Brand, Color, 1, RelatedStyle10Style2, Brand, Color, 18, RelatedStyle1Style2, Brand, Color, 12, RelatedStyle2Style2, Brand, Color, 10, RelatedStyle3etc, etc.....Any idea?Thanks in advance. |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-11-04 : 15:10:40
|
| I don't quite follow you but I think you are looking at a row-number functionsCheck out ROW_NUMBER() in BOLThe syntax would be something likeROW_NUMBER() OVER ( PARTITION BY [Brand], [Color], [Gender] ORDER BY [Total Sales] DESC ) AS rankand take where the rank is <= 10But I'm not 100% sure I understand what you are asking forCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
benking9987
Posting Yak Master
124 Posts |
Posted - 2011-11-04 : 16:33:51
|
Thank you! This is almost exactly what I need. I took what you gave me and modified a little bit. I still have one more piece to hook up to this and then I should have exactly what I need.Here's where I am:SELECT Style, PrimaryColor, Brand, TotalSales, Gender, ROW_NUMBER() OVER ( PARTITION BY [Brand], [PrimaryColor], [Gender] ORDER BY [TotalSales] DESC ) AS rankFROM PPB_RelatedData This is giving me the result information combined and sorted exactly as I need it, what I need at this point is this data iterated over all the records within the same table. For example, the SQL above lists all skus that fall in a like Color, Brand, Gender combination. So for every sku within that combination I want to indicate that these are related items to the given SKU.So I'd end up with something like this:Style, RelatedStyle, Color, Brand, Gender, TotalSales, RankThe bolded items are being generated in the SQL above. Just need to connect up the resulting "Related set" for every primary key within the table.Having a hard time explaining exactly what I need. |
 |
|
|
benking9987
Posting Yak Master
124 Posts |
Posted - 2011-11-04 : 19:07:51
|
| I see a lot of people reading this topic but nobody replying. I'm wondering if I'm not clear on what I need.The RowNumber() and Over() portion of this statement are providing me with the sets of records that I need. Ultimately I need to group like items together and rank them by TotalSales. But then I need to associate those created "groups" with each record in my table.So for example, if I have a group that has a Black color, is for Brand X, and fits gender Women, I get 10 results with the one that sells the best on top. Now for EACH RECORD that matches Black colors, Brand X, gender Women, I need to show the associated "group" of records created in the RowNumber() and Over() portion of the SQL statement posted. So I'd get something like this:From Group1Style1, Group1, RelatedStyle1Style1, Group1, RelatedStyle2Style1, Group1, RelatedStyle3Style1, Group1, RelatedStyle4Style1, Group1, RelatedStyle5Style1, Group1, RelatedStyle6Style1, Group1, RelatedStyle7Style1, Group1, RelatedStyle8Style1, Group1, RelatedStyle9Style1, Group1, RelatedStyle10Style2, Group1, RelatedStyle1Style2, Group1, RelatedStyle2Style2, Group1, RelatedStyle3Style2, Group1, RelatedStyle4Style2, Group1, RelatedStyle5Style2, Group1, RelatedStyle6Style2, Group1, RelatedStyle7Style2, Group1, RelatedStyle8Style2, Group1, RelatedStyle9Style2, Group1, RelatedStyle10Style3, Group1, RelatedStyle1etc.Does this make more sense? |
 |
|
|
benking9987
Posting Yak Master
124 Posts |
Posted - 2011-11-07 : 15:33:07
|
| Still appears that there are lots of people reading this but nobody answering. I'm thinking that this may need to be some sort of 2 step process to get exactly where I want to get. If there are any other methods anyone can suggest, I'm completely open at this point. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-11-07 : 15:41:30
|
Sorry -- I've been busy and I thought someone else would help.I'm not 100% sure of your outputStyle1, Group1, RelatedStyle1Style1, Group1, RelatedStyle2Style1, Group1, RelatedStyle3Style1, Group1, RelatedStyle4Style1, Group1, RelatedStyle5Style1, Group1, RelatedStyle6Style1, Group1, RelatedStyle7........ Can you please post some real sample data in the form of a table defintion and insert statements? A temp table or table variable would be absolutely fine.Then based on that fake data, the required output for that fake data? I don't see why the ROW_NUMBER FUNCTION isn't working for you.best of luck -- I'll keep checking this thread tomorrow.CharlieCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
benking9987
Posting Yak Master
124 Posts |
Posted - 2011-11-07 : 16:34:20
|
Thanks for the reply. Sorry to nudge, I suspected I haven't been clear enough on what I need. Here is the SQL statement that is getting me very very close to what I need:SELECT Parent_SKU AS Related_SKU, PrimaryColor, Brand, TotalSales, TargetAudience, ROW_NUMBER() OVER ( PARTITION BY [Brand], [PrimaryColor], [TargetAudience] ORDER BY [TotalSales] DESC ) AS RankFROM PPB_RelatedData The results of that query are too long to post here, but I'll put a few of them:Parent_SKU PrimaryColor Brand TotalSales TargetAudience RankBG8265 black Brand1 2 women 1BG6284 black Brand1 1 women 2BG6236 black Brand1 0 women 3BG8255 black Brand1 0 women 4BG6394 black Brand1 0 women 5BG6400 black Brand1 0 women 6BG8230 black Brand1 0 women 7BG6298 black Brand1 0 women 8BG6299 black Brand1 0 women 9BG6301 black Brand1 0 women 10BG6349 black Brand1 0 women 11BG6365 black Brand1 0 women 12BG6389 black Brand1 0 women 13BG6344 blue Brand1 0 women 1BG8222 gold Brand1 0 women 1BG6341 grey Brand1 0 women 1BG6369 mother-of-pearl Brand1 0 women 1BG6336 red Brand1 0 women 1BG8238 silver Brand1 0 women 1BG6291 silver Brand1 1 women 1BG6292 silver Brand1 0 women 2BG6274 silver Brand1 0 women 3BG8229 silver Brand1 0 women 4BG8237 silver Brand1 0 women 5BG6323 silver Brand1 0 women 6BG6329 silver Brand1 0 women 7BG6330 white Brand1 0 women 1BG6382 white Brand1 0 women 2BG6384 white Brand1 0 women 3BG6391 white Brand1 0 women 4BG6367 white Brand1 0 women 5BG6350 white Brand1 0 women 6BG6356 white Brand1 0 women 7BG8209 white Brand1 0 women 8BG6395 white Brand1 0 women 9BG6398 white Brand1 0 women 10BG8248 white Brand1 0 women 11BG8256 white Brand1 0 women 12BG8267 white Brand1 0 women 13FS81324 black Brand2 2 men 1FS84898 black Brand2 2 men 2FS84873 black Brand2 1 men 3FS84841 black Brand2 1 men 4FS81285 black Brand2 0 men 5FS80936 black Brand2 0 men 6FS81202 black Brand2 0 men 7FS81241 black Brand2 0 men 8FS84872 grey Brand2 0 men 1FS84899 grey Brand2 0 men 2FS84897 silver Brand2 3 men 1FS81284 silver Brand2 2 men 2FS84913 silver Brand2 1 men 3FS81242 silver Brand2 1 men 4FS84877 silver Brand2 0 men 5FS84840 silver Brand2 0 men 6FS84843 silver Brand2 0 women 1FS81323 white Brand2 3 men 1FS84878 white Brand2 1 men 2FS84871 white Brand2 0 men 3FS75449 white Brand2 0 men 4FS81244 yellow Brand2 1 men 1FS80937 yellow Brand2 0 men 2 You can see that this statement is grouping together items based on if they share characteristics. So, anything that has "black" as PrimaryColor, is "Brand1" in Brand, and has "women" as Target audience is then grouped and ranked in order of total sales column. Lets say that every items linked by these shared characteristics comprise a group, and each group is made up of 1 or more items and then the groups are consecutively numbered. So, in this instance, anything that is black, meant for women, and is "Brand1" forms group1. From the same results, anything that is "blue", "brand1", "women" forms group2 (there is only 1 item in this group on these results), etc.What I need as a result would look like this:Parent_SKU Related_SKU PrimaryColor Brand TotalSales TargetAudience RankBG8265 BG8265 black Brand1 2 women 1BG8265 BG6284 black Brand1 1 women 2BG8265 BG6236 black Brand1 0 women 3BG8265 BG8255 black Brand1 0 women 4BG8265 BG6394 black Brand1 0 women 5BG8265 BG6400 black Brand1 0 women 6BG8265 BG8230 black Brand1 0 women 7BG8265 BG6298 black Brand1 0 women 8BG8265 BG6299 black Brand1 0 women 9BG8265 BG6301 black Brand1 0 women 10BG8265 BG6349 black Brand1 0 women 11BG8265 BG6365 black Brand1 0 women 12BG8265 BG6389 black Brand1 0 women 13BG6284 BG8265 black Brand1 2 women 1BG6284 BG6284 black Brand1 1 women 2BG6284 BG6236 black Brand1 0 women 3BG6284 BG8255 black Brand1 0 women 4BG6284 BG6394 black Brand1 0 women 5BG6284 BG6400 black Brand1 0 women 6BG6284 BG8230 black Brand1 0 women 7BG6284 BG6298 black Brand1 0 women 8BG6284 BG6299 black Brand1 0 women 9BG6284 BG6301 black Brand1 0 women 10BG6284 BG6349 black Brand1 0 women 11BG6284 BG6365 black Brand1 0 women 12BG6284 BG6389 black Brand1 0 women 13BG6236 BG8265 black Brand1 2 women 1BG6236 BG6284 black Brand1 1 women 2BG6236 BG6236 black Brand1 0 women 3BG6236 BG8255 black Brand1 0 women 4BG6236 BG6394 black Brand1 0 women 5BG6236 BG6400 black Brand1 0 women 6BG6236 BG8230 black Brand1 0 women 7BG6236 BG6298 black Brand1 0 women 8BG6236 BG6299 black Brand1 0 women 9BG6236 BG6301 black Brand1 0 women 10BG6236 BG6349 black Brand1 0 women 11BG6236 BG6365 black Brand1 0 women 12BG6236 BG6389 black Brand1 0 women 13BG8255 BG8265 black Brand1 2 women 1BG8255 BG6284 black Brand1 1 women 2BG8255 BG6236 black Brand1 0 women 3BG8255 BG8255 black Brand1 0 women 4BG8255 BG6394 black Brand1 0 women 5BG8255 BG6400 black Brand1 0 women 6BG8255 BG8230 black Brand1 0 women 7BG8255 BG6298 black Brand1 0 women 8BG8255 BG6299 black Brand1 0 women 9BG8255 BG6301 black Brand1 0 women 10BG8255 BG6349 black Brand1 0 women 11BG8255 BG6365 black Brand1 0 women 12BG8255 BG6389 black Brand1 0 women 13BG6394 BG8265 black Brand1 2 women 1BG6394 BG6284 black Brand1 1 women 2BG6394 BG6236 black Brand1 0 women 3BG6394 BG8255 black Brand1 0 women 4BG6394 BG6394 black Brand1 0 women 5BG6394 BG6400 black Brand1 0 women 6BG6394 BG8230 black Brand1 0 women 7BG6394 BG6298 black Brand1 0 women 8BG6394 BG6299 black Brand1 0 women 9BG6394 BG6301 black Brand1 0 women 10BG6394 BG6349 black Brand1 0 women 11BG6394 BG6365 black Brand1 0 women 12BG6394 BG6389 black Brand1 0 women 13BG6400 BG8265 black Brand1 2 women 1BG6400 BG6284 black Brand1 1 women 2BG6400 BG6236 black Brand1 0 women 3BG6400 BG8255 black Brand1 0 women 4BG6400 BG6394 black Brand1 0 women 5BG6400 BG6400 black Brand1 0 women 6BG6400 BG8230 black Brand1 0 women 7BG6400 BG6298 black Brand1 0 women 8BG6400 BG6299 black Brand1 0 women 9BG6400 BG6301 black Brand1 0 women 10BG6400 BG6349 black Brand1 0 women 11BG6400 BG6365 black Brand1 0 women 12BG6400 BG6389 black Brand1 0 women 13etc..... Is this more clear where I'm going with this? |
 |
|
|
|
|
|
|
|