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)
 A better way?

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2008-01-16 : 10:12:07
With 2 tables, 1 a code table with names, and another with a nonunique id column, if there are more than 1 value for the code table, return the word Both, otherwise return the code value name

I'm looking to see if there's a better way than what I have come up with

The desired result set is


ParentID Name
----------- --------------------------------------------------
1 Agencey 1
2 Agencey 2
3 Both

(3 row(s) affected)


Here's the code


CREATE TABLE ACodes ([id] int IDENTITY(1,1), [Name] varchar(50))
CREATE TABLE Capacity ([Parentid] int, ACodeID int)
GO

INSERT INTO ACodes ([Name])
SELECT 'Agencey 1' UNION ALL SELECT 'Agencey 2'
GO

INSERT INTO Capacity (ParentID, ACodeID)
SELECT 1, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 3, 1 UNION ALL
SELECT 3, 2
GO

SELECT DISTINCT ParentID, 'Both' AS [Name] FROM Capacity
WHERE ParentID in ( SELECT ParentID
FROM Capacity c
GROUP BY ParentID
HAVING COUNT(*) > 1)
UNION ALL
SELECT ParentID, [Name]
FROM Capacity c INNER JOIN ACodes a ON c.ACodeID = a.[ID]
WHERE ParentID in ( SELECT ParentID
FROM Capacity c
GROUP BY ParentID
HAVING COUNT(*) = 1)
ORDER BY ParentID
GO

DROP TABLE ACodes, Capacity
GO



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-16 : 10:31:58
Try this:-
SELECT c.ParentID,
CASE WHEN COUNT(a.ID) > 1 THEN 'Both' ELSE MAX(a.[Name]) END AS Name
FROM Capacity c
INNER JOIN ACodes a
ON a.ID=c.ACodeID
GROUP BY c.ParentID
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-16 : 10:39:03
This?
SELECT		c.ParentID,
CASE
WHEN MIN(c.aCodeID) < MAX(c.aCodeID) THEN 'Both'
ELSE MIN(ac.Name)
END AS Name
FROM aCodes AS ac
INNER JOIN Capacity AS c ON c.aCodeID = ac.ID
GROUP BY c.ParentID



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-16 : 10:49:58
Or this?
--Peso
SELECT d.ParentID,
COALESCE(a.Name, 'Both') AS Name
FROM (
SELECT ParentID,
CASE
WHEN MIN(aCodeID) = MAX(aCodeID) THEN MAX(aCodeID)
ELSE NULL
END AS CodeID
FROM Capacity
GROUP BY ParentID
) AS d
LEFT JOIN aCodes AS a ON a.ID = d.CodeID

E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-01-16 : 10:57:55
quote:
Originally posted by visakh16

Try this:-
SELECT c.ParentID,
CASE WHEN COUNT(a.ID) > 1 THEN 'Both' ELSE MAX(a.[Name]) END AS Name
FROM Capacity c
INNER JOIN ACodes a
ON a.ID=c.ACodeID
GROUP BY c.ParentID



That's what I started with...I just forgot to use MAX


Thanks peeps



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -