Skip to content
Advertisement

SQL DB structure for N items with M parameters varying over time

There’re the items:

  • item_1
  • item_N

N is about a few hundreds. New items can be added sometimes.

Each item has a set of parameters:

  • parameter_1
  • parameter_M

M is about 20-30. These parameters are measured several times a day (independently of each other, in different time) and need to be stored in a database. So parameter_j(item_i) has a massive of time-value pairs:

  • <t0, v0> (the first measurement of parameter_j(item_i))
  • <tk, vk> (the latest measurement of parameter_j(item_i))

Base queries that need to be performed:

  • all values of a parameter for an item (ordered by the time of measurement)
  • the latest values of all parameters for all items

What database structure (tables) do I need to perform these quesries efficiently? Could you please provide SQL code for these two types of queries?

Advertisement

Answer

I would probably go with a structure like this:

Structure of tables

I will provide MS SQL codes.

Example code for creating these tables:

I filled up with dummy data via this code:

Query for ,,all values of a parameter for an item (ordered by the time of measurement)”:

Code:

Output:

enter image description here

Query for ,,the latest values of all parameters for all items”:

Code (see explanation SQL Server get latest value by date):

Output:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement