没有重复记录的左连接只显示1个最小值 [英] left join of with without duplicate records only showing 1 minimum value

查看:143
本文介绍了没有重复记录的左连接只显示1个最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个table_1:

I have a table_1:

id  custno
1   1
2   2
3   3

和table_2:

id  custno  qty
1   1       10 
2   1       7
3   2       4
4   3       7
5   1       5
6   1       5

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

When I run this query to show the minimum order quantities from every customer:

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   )

然后我得到这个结果:

custno qty
1      5
1      5
2      4
3      7

如何获取最小值每个 custno 的数量

How can I get the minimum value of qty per each custno?

我该怎么做?

Thx!

解决方案

你的意思是聚合( 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

这篇关于没有重复记录的左连接只显示1个最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆