jardar.maatje@gmail.com wrote:[color=blue]
> I am logging scientific data. For this a normal relationship database
> is not idéal but I am going to try it.
>
> I have basically three tables like:
>
> datarecord:
> * idx - integer
> * time - datetime
> * type - integer
>
> metadata:
> * idx - integer
> * unit - varchar(20) (for example m,bar,m/s,celcius)
> * name - text (for exampel Pressure, Temperature)
>
> datavalue:
> * idx - integer
> * rec_id - integer (foreign key pointing to datarecord.idx)
> * meta_id - integer (foreign key pointing to metadata.idx)
> * value - real
>
> An example dataset would be:
> datarecord
> idx time type
> 1 01.02.2006 12:12:00 1
> 2 01.02.2006 12:13:00 1
>
> metadata:
> idx unit name
> 1 bar pressure
> 2 celcius temperature
> 3 degrees heading
> 4 celcius maxtemp
> 5 celcius mintemp
>
>
> datavalue
> idx rec_id meta_id value
> 1 1 1 10.3
> 2 1 2 44.2
> 3 1 3 34
> 4 1 4 55.3
> 5 2 1 9.2
> 6 2 2 44.0
> 7 2 3 48
> 8 2 4 54.3
> 9 2 5 22.8
>
> I would like to construct a query that for example could give me time,
> pressure, temperature, maxtemperature like:
> 01.02.2006 10.3 44.2 55.3
> 01.02.2006 9.2 44.0 54.3
>
> Is this possible with SQL? And how do I do this?
>
> Best regards
>
> Jardar
>[/color]
First of all this is NOT a complex query, if it is for you then you
might consider starting with something like "SQL for Dummies" -
excellent beginner book! or similar to understand join and outer join
query constructs. Another is might be to de-normalize this a bit...
One reason to "normalize" a database is to reduce repeating data but
in your metadata table, you will be repeating unit many times, however
there are times when you must denormalize for various reasons
(performance is one simplicity another).
When inserting data into this database you must insert data in two
tables for EACH record - and the way you need to code it would be to
insert into datarecord - get the resulting idx (assuming it is an
autokey or next value), the return to the program and insert into
datavalue table - very expensive in terms of I/O.
I would suggest combining datarecord and datavalue (denormalize) or
redesign taking the previous paragraph into consideration.
given:
insert into datarecord values (date_time, presssure, temp, heading,etc.)
consider min and max temp (anything) can be calculated -(Select
min(temp) from datavalue where date_time betweed date1 and date2;)
You really only have one value for temperature at any give point in time
(date_time) you cannot not have a mintemp, maxtemp and temp that are
different at the same point in time - all 3 values will be the same.