从不同的表中选择最小值

时间:2016-05-11 14:39:42

标签: sql sql-server

我有这3张桌子:

Table_Group
/ ******************* /
GROUP_ID | Value_1 | Value_2 | Value_3 |
GROUP_1 | A | 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

我想得到这张表:

GROUP_ID | L_min | W_min | H_min
GROUP_1 | 1.1 | 2.0 | 2.2
Group_2 | 1.3 | 3.7 | 4.5

这意味着,我正在尝试为每个组获得L,W和H的最小值。

我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

您可以JOINGROUP 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