SQL Reference¶
The query language supported by odc is a small subset of SQL (Structured Query Language) with a few domain-specific extensions, most notably bitfields allow for naming and accessing sequences of bits in a column.
Note
All example commands on this page can be run against the following file:
The examples assume the odc binary is in your $PATH
.
Built-in Functions¶
Scalar Functions¶
Scalar functions return a single value, based on one or more input values.
Function |
Arity |
Description |
---|---|---|
|
|
Returns the year part of |
|
|
Returns the month part of |
|
|
Returns the day part of |
|
|
Returns the hour part of |
|
|
Returns the minute part of |
|
|
Returns the second part of |
|
|
Returns the timestamp of |
|
|
Returns the number of Julian seconds of |
|
|
Returns the Julian date of |
|
|
Time difference in seconds ( |
|
|
Cosine (where |
|
|
Sine (where |
|
|
Tangent (where |
|
|
Arc cosine (the result is in degrees) |
|
|
Arc sine (the result is in degrees) |
|
|
Arc tangent (the result is in degrees) |
|
|
Arc tangent of two variables (the result in degrees) |
|
|
Exponent |
|
|
Returns multiplied floating point value |
|
|
Hyperbolic cosine |
|
|
Hyperbolic sine |
|
|
Hyperbolic tangent |
|
|
Truncate to integer |
|
|
Round-up to integer |
|
|
Calculate floor value |
|
|
Calculate ceiling value |
|
|
Absolute value |
|
|
Extract |
|
|
|
|
|
|
|
|
Square root (\(\sqrt{x}\)) |
|
|
Natural logarithm |
|
|
Base-10 logarithm |
|
|
Convert degrees to radians |
|
|
Convert radians to degrees |
|
|
Convert Kelvin to Celsius |
|
|
Convert Celsius to Kelvin |
|
|
Convert Kelvin to Fahrenheit |
|
|
Convert Fahrenheit to Kelvin |
|
|
Convert Celsius to Fahrenheit |
|
|
Convert Fahrenheit to Celsius |
|
|
Returns |
|
|
Returns |
|
|
Distance (expressed in metres) between two positions
( |
|
|
Synonym of |
|
|
Returns 1, if ( |
|
|
Returns the current row number, indexed at 1 |
|
|
Returns an |
|
|
Returns wind speed for |
|
|
Returns wind direction for |
Aggregate Functions¶
Aggregate functions return a single value, calculated from values an expression passed as parameter to the function evaluates to for selected rows.
Function |
Arity |
Description |
---|---|---|
|
|
Compute dot product of |
|
|
Compute Root Mean Square of |
|
|
Compute standard deviation |
|
|
Compute the norm of |
|
|
Compute variance |
|
|
Returns average value |
|
|
Returns largest value |
|
|
Returns number of rows |
|
|
Returns smallest value |
|
|
Returns sum |
|
|
Returns value of the given expression evaluated for the first row of the query’s result set |
|
|
Returns value of the given expression evaluated for the last row of the query’s result set |
Data Types¶
The columns in ODB-2 files can currently be one of the following data types.
The actual type of a column can be found using odc header tool.
Bitfields¶
Bitfields allow for naming and accessing sequences of bits in a column.
Referring to Bitfield Members¶
Syntax for accessing a member of a bitfield is:
<column-name>.<bitfield-member-name>
In case it is necessary to specify table name when referring to a column, correct way of referring to a member is:
<column-name>.<bitfield-member-name>@<table-name>
For example:
report_status.active@hdr
Expanding List of Members¶
The list of bitfield members can be expanded with the asterisk (*
) operator.
For example:
report_status.*@hdr
will be expanded to:
report_status.active@hdr, report_status.passive@hdr, report_status.rejected@hdr, report_status.blacklisted@hdr, report_status.use_emiskf_only@hdr
Finding Details of Bitfield Definition¶
The odc header tool can be used to find out details of bitfield definition: its members and number of bits they occupy.
Examples¶
- Unique station identifiers
Which station identifiers are in the ODB-2 file?
odc sql -i example.odb 'select distinct statid' statid@hdr ' 96413' ' 27707' ' 27730' ' 27962' ' 27995' ' 34009' ' 34172'
- Row limit
How to display first 10 rows from an ODB-2 file?
odc sql -i example.odb 'select date, lat, lon, obsvalue where rownumber() < 10' date@hdr lat@hdr lon@hdr obsvalue@body 20210618 1.480000 110.330002 260.000000 20210618 1.480000 110.330002 296.399994 20210618 1.480000 110.330002 295.600006 20210618 1.480000 110.330002 0.952672 20210618 1.480000 110.330002 0.016915 20210618 1.480000 110.330002 50.000000 20210618 1.480000 110.330002 1.000000 20210618 1.480000 110.330002 -0.766044 20210618 1.480000 110.330002 -0.642788
- Geophysical variables
What geophysical variables are in the ODB-2 file?
odc sql -i example.odb 'select distinct varno' varno@body 1 39 40 58 7 111 112 41 42 2 59 29 3 4
See ODB Governance for description of numeric values of varno
. For example:
|
Description |
---|---|
|
Geopotential |
|
Upper air temperature |
|
Upper air meridional wind |
|
Upper air zonal wind |
|
Specific humidity |
|
Upper air rel. humidity |
|
2m temperature |
|
2m dew point |
|
10m meridional component |
|
10m zonal component |
|
2m relative humidity |
|
Upper air dew point |
|
Wind direction |
|
Wind speed |
- Number of temperature records
Count the number of temperature records.
odc sql -i example.odb 'select count(*) where varno=2' count(1) 448.000000
count(*)
is an aggregation function. Based on the other keys present in the SQL query (here: filtering to select only the temperature entries), each population of identified entries see the data subjected to the aggregation function.
- Number of temperature records per station identifier
Count the number of temperature records, this time per station identifier, where the observation values are not missing.
odc sql -i example.odb 'select count(*), statid where varno=2 and obsvalue is not null' count(1) statid@hdr 39.000000 ' 27707' 49.000000 ' 27730' 37.000000 ' 27962' 44.000000 ' 27995' 38.000000 ' 34009' 9.000000 ' 34172' 50.000000 ' 96413'
- Average temperature at 100 hPa
Get the observation count at one station and average temperature observation value by pressure level bins of 100 hPa each, showing also the average pressure in each pressure bin.
odc sql -i example.odb 'select count(*), avg(fg_depar), floor(vertco_reference_1/10000.0), avg(vertco_reference_1/100.0) where varno=2 and statid="27707" and fg_depar is not null' count(1) avg(fg_depar) floor(/(vertco_reference_1,10000)) avg(/(vertco_reference_1,100)) 11.000000 0.505271 0 43.318182 5.000000 -0.160176 1 120.600000 5.000000 -0.227211 2 218.400000 2.000000 1.077009 3 316.500000 3.000000 0.370587 4 438.666667 3.000000 0.385093 5 553.666667 1.000000 0.324625 6 637.000000 1.000000 -0.013323 7 700.000000 4.000000 0.635424 8 836.000000 4.000000 0.118218 9 932.500000
- Meridional and zonal wind near 500 hPa
Get the observation count at one station and mean observation minus first-guess departure for meridional wind and zonal wind near 500 hPa.
odc sql -i example.odb 'select count(*), avg(fg_depar) where varno in (3,4) and statid="27707" and vertco_reference_1>=45000. and vertco_reference_1<55000. and fg_depar is not null' count(1) avg(fg_depar) 4.000000 -0.238838