sql server 取多条数据的最大值

实列:

 1 SELECT
 2  a.BillDate '[开票时间]',
 3  a.Hdbh '[运单号]',
 4  a.Status '运单状态',
 5  a.DisplayStatus '运单状态字',
 6  b.name '开票部门',
 7  d.name '运达部门',
 8  b.OrgAttributes '部门类型',
 9  e.MemberProblem,
10  e.CustomerResponse,
11  e.HandleSuggestion
12 FROM
13  shipment_bill a
14  JOIN sys_organization b ON a.DepartmentId=b.id
15  JOIN shipment_transport c ON a.id=c.BillId
16  JOIN sys_organization d ON c.EndDepartment=d.id
17  JOIN (SELECT g.BillId AS BillId ,g.MemberProblem AS MemberProblem ,g.CustomerResponse AS CustomerResponse,g.HandleSuggestion AS HandleSuggestion FROM member_tracking g JOIN (SELECT BillId,MAX(Id)AS Id FROM member_tracking GROUP BY BillId) f ON f.Id=g.Id) e ON e.BillId=a.id
18 WHERE ---查询样表()
19  Status IN ( 0, 1, 2, 3 ) 
20  AND a.CreateTime < '2019-12-16' 
21  AND a.IsValid = 1 
22 ORDER BY
23  a.BillDate
---
SELECT
g.BillId AS BillId ,g.MemberProblem AS MemberProblem ,g.CustomerResponse AS CustomerResponse,g.HandleSuggestion AS HandleSuggestion ---一层查询

FROM member_tracking g

JOIN
(SELECT BillId,MAX(Id)AS Id FROM member_tracking GROUP BY BillId) f ON f.Id=g.Id---查询最大值(注意显示列只有两列,两列以上才疏学浅,还不会)
posted @ 2019-12-26 16:30  吉连  阅读(2995)  评论(0编辑  收藏  举报