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 |
|
baranozgul
Starting Member
20 Posts |
Posted - 2005-01-05 : 09:23:02
|
| Hi All,I have an interesting problem with a query.Suppose that you have the following table structure:/* Tests table*/CREATE TABLE [Tests] ( [Id] [int] IDENTITY (1, 1) NOT NULL , [TestName] [int] NOT NULL ,) ON [PRIMARY]GO/* Parameters table, contains the list of parameters each test can take*/CREATE TABLE [Parameters] ( [Id] [int] IDENTITY (1, 1) NOT NULL , [ParameterName] [int] NOT NULL ,) ON [PRIMARY]GO/*ParameterValues table, tells us which Test has which parameters and their values*/CREATE TABLE [ParametersValues] ( [Id] [int] IDENTITY (1, 1) NOT NULL , [TestId] [int] NOT NULL , [ParameterId] [int] NOT NULL , [ParameterValue] [int] NOT NULL ,) ON [PRIMARY]GOPretty simple isn't it? Below is a an example of the data stored in the tables.Testsid Name1 Moisture Test2 Speed Test3 Heat TestParametersid Name1 Oxygen Rate2 Average Speed3 Test Lab4 Tester Guy5 Notes6 Observed By7 TemperatureParameter Valuesid TestId ParameterID ParameterValue1 1 1 98%2 1 3 Chemistry Lab3 1 6 Dr. Stew4 2 2 56 mph5 2 4 Ralph6 3 7 56 FNow what if I would like to list the Tests, which have some certain parameter values? Such as- Tests in which average speed is below 60 mph- Tests in which the observer is Dr. Stew and lab is Chemistry lab.- Tests in which Oxygen rate above 80%, observer is Dr Stew and lab is the Chemistry lab.I find it difficult to build a performance query, considering that the required parameters may change in number, and the size of ParameterValues table may exceed some millions.- Baran Ozgul |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-01-05 : 10:23:55
|
| What have you tried?The key is to join from your tests table to the parameters table, implement your WHERE clause on the parameters table, GROUP BY the testID, and only accept results HAVING a COUNT(*) equal to the number of conditions in your WHERE clause.The HAVING COUNT(*) = x is the key part, if you want to only return rows in which ALL conditions are satisfied. If you want rows in which ANY condition is satisfied, you wouldn't need it.- Jeff |
 |
|
|
|
|
|
|
|