July 4, 2005 at 4:36 pm
I have a Class table thus: ClassID (PK), SetCode, SubjectCode, YearCode, InstructorID and I need to select from this to get a resultset with unique values for SetCode, SubjectCode and YearCode and include an additional field which can be used as a unique index into this resultset. So, the actual resultset will have UniqueID, SetCode, SubjectCode and YearCode. Now, separately I need to select from the Class table unique values for SetCode, SubjectCode, YearCode and InstructorID but I also need to include the same UniqueID column with the same values as in the first resultset.
This is so that a UI can be built with a tree structure listing unique SetCode, SubjectCode, YearCode and then for each of these nodes a unique list of InstructorID’s can be listed when the user clicks on that node. My UI developer tells me it would save time if these resultsets could be joined using a single column rather than 3 columns, hence the need to somehow generate this UniqueID column.
I have thought about having a separate table, ClassList with a composite unique key thus: SetCode, SubjectCode, YearCode with a further field UniqueID which is an IDENTITY. I would propose maintaining the records in this table using triggers on the Class table above. This way I can always join with this table in my two selects above and include the UniqueID column in the resultsets.
I have 2 questions:
1) Is this the best way to achieve the two resultsets that can be joined at the UI using a single column?
2) What would the Insert, Update and Delete triggers look like? (hint – I’ve never written a trigger before…)
Regards
July 5, 2005 at 11:16 am
Hi,
Does your unique ID have to be an incremental integer type? If not, I would just concatenate the values of SetCode, SubjectCode and YearCode to generate a unique field.
To get your first results...
SELECT FirstUniqueID = CONVERT(varchar,SetCode) + CONVERT(varchar,SubjectCode)+CONVERT(varchar,YearCode),
SetCode,
SubjectCode,
YearCode
FROM Class
GROUP BY CONVERT(varchar,SetCode) + CONVERT(varchar,SubjectCode)+CONVERT(varchar,YearCode)
To get your second results...
SELECT SecondUniqueID = CONVERT(varchar,SetCode) + CONVERT(varchar,SubjectCode)+CONVERT(varchar,YearCode)+CONVERT(varchar,InstructorID),
FirstUniqueID = CONVERT(varchar,SetCode) + CONVERT(varchar,SubjectCode)+CONVERT(varchar,YearCode)
SetCode,
SubjectCode,
YearCode,
InstructorID
FROM Class
GROUP BY CONVERT(varchar,SetCode) + CONVERT(varchar,SubjectCode)+CONVERT(varchar,YearCode)+CONVERT(varchar,InstructorID)
Now you have the same FirstUniqueID in each result.
Does this work for you?
July 5, 2005 at 11:50 am
I think this is a *much* better way of achieving the same result!
I'm going to implement this and see how the dev likes it!
Many Thanks
July 5, 2005 at 12:46 pm
if the table is big you may as well populate a computed column and create an index on it. You will not be sorry you did
* Noel
July 5, 2005 at 2:24 pm
Again this makes sense. The table can end up with ~20,000 rows which will certainly benefit from an indexed computed column.
Thanks
July 5, 2005 at 2:27 pm
The benefit is only there if you have a where condition on it and that the range of values is good (didn't read the whole thread).
Just my 2 cents.
July 5, 2005 at 3:05 pm
the index in the computed column is a way of materialize the data and not calculate it on the fly. IT will definetly speed things up even if it is not in the where clause (which it should )
* Noel
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply