左连接没有重复记录只显示1个最小值

Cli*_*r87 0 sql join duplicates

我有一个table_1:

id  custno
1   1
2   2
3   3
Run Code Online (Sandbox Code Playgroud)

和table_2:

id  custno  qty
1   1       10 
2   1       7
3   2       4
4   3       7
5   1       5
6   1       5
Run Code Online (Sandbox Code Playgroud)

当我运行此查询以显示每个客户的最小订单数量时:

SELECT table_1.custno,table_2.qty 
FROM table_1 LEFT OUTER JOIN table_2 ON  table_1.custno = table_2.custno AND  
qty = (SELECT MIN(qty) FROM table_2  WHERE table_2.custno = table_1.custno   )
Run Code Online (Sandbox Code Playgroud)

然后我得到这个结果:

custno qty
1      5
1      5
2      4
3      7
Run Code Online (Sandbox Code Playgroud)

如何获得qty每个的最小值custno

我怎么能这样做?

谢谢!

Yos*_*ari 5

你的意思是aggregation(GROUP BY):

SELECT table_1.custno,MIN(table_2.qty) AS [min_val]
FROM table_1 
LEFT OUTER JOIN table_2 ON  table_1.custno = table_2.custno 
GROUP BY table_1.custno
Run Code Online (Sandbox Code Playgroud)