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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-12-02 : 08:13:44
|
| brian writes "I need to create a killer join querry and am having all kinds of trouble figuring out if it is even possible.I have a table called Devices, that stores multiple types of Devices, as kept track of in a DeviceTypes table. Each type of device reports different data. A device is something out in the real world that reports time stamped data back to the database. For example, one might measure temperature, and another measure energy usage. One column in the Devices table is called DataTable char(30) and stores the name of the table the data for that device is stored. So Each data table has a column called ReadingTime (datetime primary key) and then some number of columns that contain the data based on device type. Now there is one more table of interest, AlarmsPending, which holds alarms to be emailed after a configured delay, in need of attention by a real person. Columns in the table are DeviceID, AlarmTime, and DataName, which is the name of the column in the data table that holds the value in an alarm state. I would like to run a query that involves all the alarms and gets the details for it to be emailed. I can do this with a cursor in a sproc, but I was thinking it would be much faster if I could just do a query. Plus I can use it in many places that are similar.I want a record set of every alarm pending to include the value in the data table.Here is a sample set of data followed by a record set the query should returnDevicesDeviceID DataTable DeviceTypeID1 datatable1 12 datatable2 13 datatable3 14 datatable4 25 datatable5 2DeviceTypesDeviceTypeID Description CreateDataTablePrototype1 Temperature 'create table datatable*** (ReadingTime datetime primary key, temp1 int,temp2 int,temp3 int,temp4 int)'2 Energy 'create table datatable*** (ReadingTime datetime primary key, kw int, kvar int, input1 int, input2 int)'AlarmsPendingAlarmID DeviceID AlarmTime DataName1 1 1/1/2004 12:30:00 PM temp12 1 1/1/2004 12:35:00 PM temp33 3 1/1/2004 12:35:00 PM temp44 4 1/1/2004 12:40:00 PM kw5 5 1/1/2004 12:40:00 PM kw6 2 1/1/2004 12:45:00 PM temp2datatable1ReadintTime temp1 temp2 temp3 temp4...1/1/2004 12:25:00 PM 80 70 74 701/1/2004 12:30:00 PM 81 70 78 701/1/2004 12:35:00 PM 81 70 78 701/1/2004 12:40:00 PM 82 70 79 701/1/2004 12:45:00 PM 82 70 79 70datatable2ReadintTime temp1 temp2 temp3 temp4...1/1/2004 12:25:00 PM 70 70 70 701/1/2004 12:30:00 PM 70 71 71 701/1/2004 12:35:00 PM 70 72 70 701/1/2004 12:40:00 PM 70 74 70 701/1/2004 12:45:00 PM 70 80 70 70datatable3ReadintTime temp1 temp2 temp3 temp4...1/1/2004 12:25:00 PM 70 70 70 701/1/2004 12:30:00 PM 70 69 71 731/1/2004 12:35:00 PM 70 70 72 761/1/2004 12:40:00 PM 70 70 73 781/1/2004 12:45:00 PM 70 70 73 81datatable4ReadintTime kw kvar input1 input2...1/1/2004 12:25:00 PM 185 48 1 01/1/2004 12:30:00 PM 183 47 1 01/1/2004 12:35:00 PM 179 49 1 01/1/2004 12:40:00 PM 210 69 1 01/1/2004 12:45:00 PM 215 72 1 0datatable5ReadintTime kw kvar input1 input2...1/1/2004 12:25:00 PM 105 16 1 01/1/2004 12:30:00 PM 103 16 1 01/1/2004 12:35:00 PM 104 15 1 01/1/2004 12:40:00 PM 135 21 1 01/1/2004 12:45:00 PM 134 20 1 0Query and resulting record setselect ap.AlarmID, ap.Devic |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2004-12-03 : 19:46:27
|
| You have a very bad design here... Very bad.HTH |
 |
|
|
|
|
|
|
|