SQL MAX 聚合函数没有带来最新日期
SQL MAX aggregate function not bringing the latest date
目的:我试图找到老师购买和打字的最大日期。
订单table
ID
Ordertype
Status
TeacherID
PurchaseDate
SchoolID
TeacherassistantID
1
Pencils
Completed
1
1/1/2021
1
1
2
Paper
Completed
1
3/5/2021
1
1
3
Notebooks
Completed
1
4/1/2021
1
1
4
Erasers
Completed
2
2/1/2021
2
2
教师table
TeacherID
Teachername
1
Mary Smith
2
Jason Crane
学校table
ID
schoolname
1
ABC school
2
PS1
3
PS2
这是我尝试的代码:
SELECT o.ordertype, o.status, t.Teachername, s.schoolname
,MAX(o.Purchasedate) OVER (PARTITION by t.ID) last_purchase
FROM orders o
INNER JOIN teachers t ON t.ID=o.TeacherID
INNER JOIN schools s ON s.ID=o.schoolID
WHERE o.status in ('Completed','In-progress')
AND o.ordertype not like 'notebook'
它应该是这样的:
Ordertype
Status
teachername
last_purchase
schoolname
Paper
Completed
Mary Smith
3/5/2021
ABC School
Erasers
Completed
PS1
2/1/2021
ABC school
它带来了多行,而不仅仅是最新购买日期及其关联的行。我想我需要一个子查询。
聚合函数不适合您要执行的操作。它们的目的是汇总多行中的值,而不是选择特定的行。
只是一个 window 函数不会过滤任何行。
您想使用 window 函数进行过滤:
SELECT ordertype, status, Teachername, schoolname, Purchasedate
FROM (SELECT o.ordertype, o.status, t.Teachername, s.schoolname,
o.Purchasedate,
ROW_NUMBER() OVER (PARTITION by t.ID ORDER BY o.PurchaseDate DESC) as seqnum
FROM orders o JOIN
teachers t
ON t.ID = o.TeacherID
schools s
ON s.ID = o.schoolID
WHERE o.status in ('Completed', 'In-progress') AND
o.ordertype not like 'notebook'
) o
WHERE seqnum = 1;
你可以用不同的方式使用它。最好使用 Group By 对其他列进行分组,然后使用 Order by 对所有记录重新排序,就像下面一样。
SELECT top 1 o.ordertype, o.status, t.Teachername, s.schoolname
,o.Purchasedate
FROM orders o
INNER JOIN teachers t ON t.ID=o.TeacherID
INNER JOIN schools s ON s.ID=o.schoolID
having o.status in ('Completed','In-progress')
AND o.ordertype not like 'notebook'
group by o.ordertype, o.status, t.Teachername, s.schoolname
order by o.Purchasedate Desc
目的:我试图找到老师购买和打字的最大日期。
订单table
ID | Ordertype | Status | TeacherID | PurchaseDate | SchoolID | TeacherassistantID |
---|---|---|---|---|---|---|
1 | Pencils | Completed | 1 | 1/1/2021 | 1 | 1 |
2 | Paper | Completed | 1 | 3/5/2021 | 1 | 1 |
3 | Notebooks | Completed | 1 | 4/1/2021 | 1 | 1 |
4 | Erasers | Completed | 2 | 2/1/2021 | 2 | 2 |
教师table
TeacherID | Teachername |
---|---|
1 | Mary Smith |
2 | Jason Crane |
学校table
ID | schoolname |
---|---|
1 | ABC school |
2 | PS1 |
3 | PS2 |
这是我尝试的代码:
SELECT o.ordertype, o.status, t.Teachername, s.schoolname
,MAX(o.Purchasedate) OVER (PARTITION by t.ID) last_purchase
FROM orders o
INNER JOIN teachers t ON t.ID=o.TeacherID
INNER JOIN schools s ON s.ID=o.schoolID
WHERE o.status in ('Completed','In-progress')
AND o.ordertype not like 'notebook'
它应该是这样的:
Ordertype | Status | teachername | last_purchase | schoolname |
---|---|---|---|---|
Paper | Completed | Mary Smith | 3/5/2021 | ABC School |
Erasers | Completed | PS1 | 2/1/2021 | ABC school |
它带来了多行,而不仅仅是最新购买日期及其关联的行。我想我需要一个子查询。
聚合函数不适合您要执行的操作。它们的目的是汇总多行中的值,而不是选择特定的行。
只是一个 window 函数不会过滤任何行。
您想使用 window 函数进行过滤:
SELECT ordertype, status, Teachername, schoolname, Purchasedate
FROM (SELECT o.ordertype, o.status, t.Teachername, s.schoolname,
o.Purchasedate,
ROW_NUMBER() OVER (PARTITION by t.ID ORDER BY o.PurchaseDate DESC) as seqnum
FROM orders o JOIN
teachers t
ON t.ID = o.TeacherID
schools s
ON s.ID = o.schoolID
WHERE o.status in ('Completed', 'In-progress') AND
o.ordertype not like 'notebook'
) o
WHERE seqnum = 1;
你可以用不同的方式使用它。最好使用 Group By 对其他列进行分组,然后使用 Order by 对所有记录重新排序,就像下面一样。
SELECT top 1 o.ordertype, o.status, t.Teachername, s.schoolname
,o.Purchasedate
FROM orders o
INNER JOIN teachers t ON t.ID=o.TeacherID
INNER JOIN schools s ON s.ID=o.schoolID
having o.status in ('Completed','In-progress')
AND o.ordertype not like 'notebook'
group by o.ordertype, o.status, t.Teachername, s.schoolname
order by o.Purchasedate Desc