user2528601 user2528601 - 6 months ago 17
SQL Question

Select Min value from different tables

I have this 3 tables:

Table_Group

/*************************************************/

GROUP_ID | Value_1 | Value_2 | Value_3 |

Group_1      |
a             |
b             |
c            |

Group_2      |
d             |
e             |
f             |


Table_series

/*************************************************/

SERIE_ID | L    | W    | H    |

Serie_1     |
1.5 |
2.0   |
2.2 |

Serie_2     |
1.8 |
3.0   |
3.5 |

Serie_3     |
1.1 |
2.5   |
3.7 |

Serie_4     |
1.3 |
4.5   |
3.7 |


Table_GroupSeries

/*************************************************/

GROUP_ID |SERIE_ID

Group_1      |Series_1

Group_1      |Series_2

Group_1      |Series_3

Group_2      |Series_4


And I would like to get this table:

GROUP_ID |L_min|W_min|H_min

Group_1      |1.1     |2.0         |2.2

Group_2      |1.3     |3.7         |4.5


This means, that I'm trying to get the minimum value of L,W and H for each Group.

How could I do this?

Answer

You can JOIN and GROUP BY:

SELECT t1.GROUP_ID, MIN(L) AS L_min, MIN(W) AS W_min, MIN(H) AS H_min
FROM Table_Group AS t1
JOIN Table_GroupSeries AS t2 ON t1.GROUP_ID = t2.GROUP_ID
JOIN Table_series AS t3 ON t2.SERIE_ID = t3.SERIE_ID
GROUP BY t1.GROUP_ID