MySQL查询与分组 [英] MySQL Query with Group By

查看:123
本文介绍了MySQL查询与分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩过很多不同的查询,但一直没能找到这个项目的正确语法。我真的希望有人能理解我的混乱。

I have played around with many different queries, but haven't been able to locate the right syntax for this project.. I really hope someone can make sense of my mess.

我有一张包含订单的表,每个订单一行,每行包含客户编号,采购订单编号和订单日期等信息。采购订单号码可以绑定到一个或多个订单,这是我迷路的地方。我试图创建一个包含客户编号,PO#,最早订单日期和最后一个订单日期(根据PO#分组)的单个行的查询。

I have a table with orders, one row per order, and each row containing info such as the customer number, PO#, and date of the order. A PO number can be tied to one or more orders, which is where I am getting lost. I am trying to create a query that will produce a single row containing the customer number, PO#, date of the earliest order, and date of the last order, grouped by the PO#.

任何指向正确方向的指针都会非常感谢!

Any pointers in the right direction will by much appreciated!

推荐答案

这将得到订单和范围的东西......

This will get the order and range stuff...

select
      o.PONumber,
      o.CustomerID,
      count(*) as TotalOrders,
      min( o.OrderID ) as FirstOrder,
      min( o.OrderDate) as FirstOrderDate,
      max( o.OrderID ) as LastOrder,
      max( o.OrderDate) as LastOrderDate
   from
      Orders o
   group by
      o.PONumber,
      o.CustomerID

这篇关于MySQL查询与分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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