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
 SQL Server Development (2000)
 Better idea for a SELECT

Author  Topic 

sica
Posting Yak Master

143 Posts

Posted - 2001-09-29 : 07:26:42
I have 4 tables as following:

typetable
typeid typename
1 type1
2 type2
3 type3
4 type4
5 type5

periodtable
periodid periodname
1 2000
2 2001
3 1999
4 1998

maintable
mainid mainname
1 test1
2 test2
3 test3

The 4th table has a contrain from alla other table
reductiontable
reductionid mainid periodid typeid
1 1 1 1
2 1 2 1
3 1 3 1
4 1 4 1
5 1 1 2
6 1 2 2
7 1 3 2
8 1 4 2
9 1 1 3

What I want is to return all available typeid for a certain mainid in case of all period are not fulfilled.
Ex: For mainid = 1 the selection should return:
type3
type4
type5

because the type1 and type2 are already having the period fullfilled.

My solution is to take all the types and compare the period from the periodtable w/ existing tuples in reductiontable.If the number doestn't match I have a type
My select looks like this:

SELECT typeid, typename
FROM typetable
WHERE typeid IN (
SELECT CASE
WHEN typeid=1 AND (SELECT COUNT(periodid)FROM periodtable) <> (SELECT COUNT(periodid)FROM reductiontable WHERE mainid = 1 AND typeid=1)THEN typeid
WHEN typeid=2 AND (SELECT COUNT(periodid) FROM periodtable) <> (SELECT COUNT(periodid) FROM reductiontable WHERE mainid=1 AND typeid=2) THEN typeid
WHEN typeid=3 AND (SELECT COUNT(periodid) FROM periodtable) <> (SELECT COUNT(periodid) FROM reductiontable WHERE mainid=1 AND typeid=3) THEN typeid
WHEN typeid=4 AND (SELECT COUNT(periodid) FROM periodtable) <> (SELECT COUNT(periodid) FROM reductiontable WHERE mainid=1 AND typeid=4) THEN typeid
WHEN typeid=5 AND (SELECT COUNT(periodid) FROM periodtable) <> (SELECT COUNT(periodid) FROM reductiontable WHERE mainid=1 AND typeid=5) THEN typeid
END AS typeid
FROM typetable )

Does anyone have a better idea?

Thanks.

Best regards,
Sica

   

- Advertisement -