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 |
|
aclarke
Posting Yak Master
133 Posts |
Posted - 2001-12-24 : 12:41:46
|
| One of my clients is selling area rugs. There's a subcategory table that groups individual rug SKUs together. A subcategory of rugs can have multiple sizes. Each size for a subcategory has a price associated with it. Subcategories are also part of a collection. Here's the schema (unnecessary details stripped out):CREATE TABLE [dbo].[Subcategory] ( [subcategoryID] [int] NULL , [CollectionID] [smallint] NULL , [SubcategoryName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOCREATE TABLE [dbo].[SKU] ( [SKUID] [int] NULL , [SubcategoryID] [int] NULL , [SKU] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOCREATE TABLE [dbo].[Size] ( [sizeID] [int] NULL , [length] [real] NULL , [width] [real] NULL ,) ON [PRIMARY]GOCREATE TABLE [dbo].[XRef_SizeSubcategory] ( [sizeSubcategoryID] [int] NULL , [SizeID] [int] NULL , [SubcategoryID] [int] NULL , [Price] [money] NULL ,) ON [PRIMARY]GOinsert into subcategory values (1, 1, 'Subcategory 1')insert into subcategory values (2, 1, 'Subcategory 2')insert into SKU values (1, 1, 'SKU 1 for Subcategory 1')insert into SKU values (2, 1, 'SKU 2 for Subcategory 1')insert into SKU values (3, 2, 'SKU 1 for Subcategory 2')insert into SKU values (4, 2, 'SKU 2 for Subcategory 2')insert into [Size] values (1, 3, 2)insert into [Size] values (2, 4, 3)insert into [Size] values (3, 5, 2)insert into [Size] values (4, 6, 3)insert into XRef_SizeSubcategory values (1, 1, 1, 43)insert into XRef_SizeSubcategory values (2, 2, 1, 36)insert into XRef_SizeSubcategory values (3, 3, 1, 27)insert into XRef_SizeSubcategory values (4, 4, 1, 90)insert into XRef_SizeSubcategory values (5, 1, 2, 475)insert into XRef_SizeSubcategory values (6, 2, 2, 300)insert into XRef_SizeSubcategory values (7, 3, 2, 950)I need to pull back all subcategories for a collection, along with the number of rugs in each subcategory, number of rug sizes, minimum and maximum sizes and minimum & max prices (the smallest rug is not necessarily the cheapest and that's OK). So the results might look like this for categoryID 1:SubcategoryID, SubcategoryName, numSizes, minPrice, maxPrice, minWidth, minLength, maxWidth, maxLength1, 'Subcategory 1', 4, 43, 90, 2, 3, 3, 6Of course there would likely be several subcategories in a collection. Also, I need to determine sizes by surface area of the rug.So there you go. I can't figure out how to do this with a query - the only way I can figure out how to do it would be to generate a report and save these values in another table. Any other ideas? |
|
|
aclarke
Posting Yak Master
133 Posts |
Posted - 2001-12-24 : 22:23:03
|
Hi kids. Merry Christmas, which I guess is why there are so few people reading the forums right now...I've been SLAVING over this all day and this is as far as I've gotten. I'm posting it here in the hopes that somebody actually finds my question interesting and maybe this will prime your analytical pump, and help you understand what I'm talking about...select sc.subcategoryID, sc.subcategoryName, min(ss.price) as minPrice, max(ss.price) as maxPrice, min(sz.width) as minWidth, min(sz.length) as minLength, max(sz.width) as maxWidth, max(sz.length) as maxLength, min(sz.width * sz.length) as minArea, max(sz.width * sz.length) as maxArea, (min(sz.width) * min(sz.length)) as calcMinArea, (max(sz.width) * max(sz.length)) as calcMaxArea, count(ss.sizeID) as numSizesfrom XRef_SizeSubcategory ss left join [Size] sz on ss.sizeID = sz.sizeID left join subcategory sc on ss.subcategoryID = sc.subcategoryIDgroup by sc.subcategoryID, sc.subcategoryName/*having (min(sz.width) * min(sz.length)) = min(sz.width * sz.length) and (max(sz.width) * max(sz.length)) = max(sz.width * sz.length)*/ OK as you can see here, there's the possibility that (min(sz.width) * min(sz.length)) <> min(sz.width * sz.length). Hence the "having" clause I just threw in here for demonstration purposes. For instance, if there's a 2x9 rug and a 3x3 rug, then the 3x3 rug should be the smallest and the 2x9 the largest (with the minWidth, maxWidth, minLength, maxLength) variables set appropriately. However, in this example, minWidth would be 2, maxWidth 3, minLength 3 and maxWidth 9 so I'd actually end up telling the customer they could get a 2x3 and a 3x9 rug. That's what I'm trying to avoid. Help!!!OK, so to say this one other way, I actually want to get the lengths and widths that were used to calculate "calcMinArea" and "calcMaxArea" above...Thank you so much for reading this. (I hope you had a) Merry Christmas!Edited by - aclarke on 12/24/2001 22:24:22 |
 |
|
|
aclarke
Posting Yak Master
133 Posts |
Posted - 2001-12-29 : 03:23:12
|
OK...Thanks to Nazim and the postings at http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=11707, I've finally figured this one out. See below if you're interested.First I'll create a view:CREATE VIEW dbo.RugSizes ASSELECT sc.collectionID, sc.subcategoryID, sc.subcategoryName, ss.price, sz.width, sz.length, sz.width * sz.length AS areaFROM dbo.SizeSubcategory ss LEFT OUTER JOIN dbo.Size sz ON ss.sizeID = sz.sizeID LEFT OUTER JOIN dbo.Subcategory sc ON ss.subcategoryID = sc.subcategoryIDWHERE (sc.categoryID = 3) ORDER BY sc.collectionID, sc.subcategoryID, area Now here's my query:select bMin.subcategoryID, bMin.price as minPrice, bMin.width as minWidth, bMin.length as minLength, bMin.area as minArea, bMax.price as maxPrice, bMax.width as maxWidth, bMax.length as maxLength, bMax.area as maxAreafrom v_rugSizes bMin, v_rugSizes bMax, ( select a.subcategoryID, min(a.area) as theMinArea, max(a.area) as theMaxArea from v_rugSizes a group by a.subcategoryID ) as awhere (a.subcategoryID = bMin.subcategoryID and a.theMinArea = bMin.area) and (a.subcategoryID = bMax.subcategoryID and a.theMaxArea = bMax.area)order by bMin.subcategoryID I realize that I might be the only person to actually read this post, but I feel like if I have an answer to a posting I should put it up, even if the post is my own! |
 |
|
|
|
|
|
|
|