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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Not sure what to do.

Author  Topic 

RandalJohnson
Starting Member

3 Posts

Posted - 2004-07-19 : 14:27:56
I have three files:
1. CATEGORY - Item Category File
CatCode
CatName
CatRank

2. CATITEM - Item Category Xref
CatCode
ItemNum

3. ITEMMAST - Item Master
ItemNum
ItemDesc
Etc.

Each item number in the data base can be assigned several categories under which it falls. The catagories in the category file are ranked according to importance. What I need to do is create a view that will retrieve the combined information in order by Category, Item Number being unique by item number. In otherwords if the item is assigned to 5 categories I need it to appear once with the category of the highest rank assigned to it. The result set must contain all of the information from the item master as well as it's category code.
Yow how the heck do I do this?

Thanks,

Randal

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-07-19 : 14:29:33
When you say files, do you mean SQL Server tables? If not, then what RDBMS are you referring to?

Tara
Go to Top of Page

RandalJohnson
Starting Member

3 Posts

Posted - 2004-07-19 : 14:47:42
Tables certainly tables. To elaborate a little bit let's say I have Itemnum 2345
Item 2345 is assigned to categories 1, 4, 6, and 7,
That CatMst is populated something like
CatCode CatRank
1 30
4 45
6 20
7 60


I want a view that will return
Item CatCode
2345 6


No duplicates. The item number once with it's (lowest) ranking category code.

Hope this helps a bit. Not sure how to accomplish this in SQL Server2000

Thanks,

Randal
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-07-19 : 15:12:39
I think Rank is in the wrong table, or your missing or...

try this


USE Northwind
GO

CREATE TABLE xCATEGORY (
CatCode int IDENTITY(1,1)
, CatName varchar(50)
, CatRank int
)

CREATE TABLE xCATITEM (
CatCode int
, ItemNum int
)

CREATE TABLE xITEMMAST (
ItemNum int IDENTITY(1,1)
, ItemDesc varchar(50)
)
GO

INSERT INTO xITEMMAST (ItemDesc)
SELECT 'SQL For Smarties' UNION ALL
SELECT 'ADMIN 911' UNION ALL
SELECT 'The Gurus...HTML' UNION ALL
SELECT 'Margaritas...Mans best freind' UNION ALL
SELECT 'The zen of Margarita mixing' UNION ALL
SELECT 'Tequila Sunrise...a margarita by any other name..'


INSERT INTO xCATEGORY (CatName, CatRank)
SELECT 'General',1 UNION ALL
SELECT 'COMP 101',2 UNION ALL
SELECT 'ADV COMP',3 UNION ALL
SELECT 'Lounge Lizards',1 UNION ALL
SELECT 'Bartending 101',2 UNION ALL
SELECT 'How not to drink your profits',3

INSERT INTO xCATITEM (CatCode, ItemNum)
SELECT 1,3 UNION ALL
SELECT 2,3 UNION ALL
SELECT 3,3 UNION ALL
SELECT 4,6 UNION ALL
SELECT 5,6 UNION ALL
SELECT 6,6
GO

SELECT CatRank, CatName, ItemDesc
FROM XCATEGORY a
JOIN xCATITEM b ON a.CatCode = b.CatCode
JOIN xITEMMAST c ON b.ItemNum = c.ItemNum
JOIN (SELECT ItemNum, MAX(CatRank) AS MAX_CatRank
FROM XCATEGORY d
JOIN xCATITEM e ON d.CatCode = e.CatCode
GROUP BY ItemNum) AS XXX ON XXX.ItemNum = b.ItemNum
AND XXX.MAX_CatRank = a.CatRank
GO

DROP TABLE xITEMMAST
DROP TABLE xCATEGORY
DROP TABLE xCATITEM
GO





Brett

8-)
Go to Top of Page

RandalJohnson
Starting Member

3 Posts

Posted - 2004-07-19 : 16:02:43
Okay I see what you are doing there mostly. It's just the final join there I'm not sure exactly what that's doing. Is it creating a temporary file that then does the selection? The result set needs to have the Category code and the complete item record. I don't have my SQL server 2000 ap in front of me to try this with. Could you explain a little bit about the sub select you've in there and how it grabs only the top ranking category? I just haven't had reason to do something like this until now so sorry for the slowness on this.

Thanks
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-07-19 : 16:26:13
It returns what I think you want....



CatCode CatName CatRank ItemNum ItemDesc
----------- -------------------------------------------------- ----------- ----------- --------------------------------------------------
3 ADV COMP 3 3 The Gurus...HTML
6 How not to drink your profits 3 6 Tequila Sunrise...a margarita by any other name..



What it does is....

That'll cost you....

Seriously....

1. You say that an Item can be across many Categories, so we must group be Item
2. You've got the Rank on the Category, so you need to join cat to item, though the juction, and get the max, based on the Item
3. then you need to join all three and "coorelate" on Item and Max


I guess without certain constraints I guess you could have problems...



Brett

8-)
Go to Top of Page
   

- Advertisement -