我有这个文件表(这里是简化版):
+------+-------+--------------------------------------+
| id | rev | content |
+------+-------+--------------------------------------+
| 1 | 1 | ... |
| 2 | 1 | ... |
| 1 | 2 | ... |
| 1 | 3 | ... |
+------+-------+--------------------------------------+
Run Code Online (Sandbox Code Playgroud)
如何为每个id选择一行并且只选择最大转速?
使用上面的数据,结果应该包含两行:[1, 3, ...]和[2, 1, ..].我正在使用MySQL.
目前,我在while循环中使用检查来检测并覆盖结果集中的旧转速.但这是实现结果的唯一方法吗?是不是有SQL解决方案?
更新
作为答案提示,有是一个SQL的解决方案,并且这里sqlfiddle演示.
更新2
我注意到在添加上述sqlfiddle之后,问题被投票的速率超过了答案的upvote率.那不是故意的!小提琴是基于答案,特别是接受的答案.
mysql sql aggregate-functions greatest-n-per-group groupwise-maximum
多年来GROUP BY,我一直在使用所有类型的聚合查询.最近,我一直在反向设计一些PARTITION BY用于执行聚合的代码.在阅读我能找到的所有文档时PARTITION BY,听起来很像GROUP BY,可能还添加了一些额外的功能?它们是相同通用功能的两个版本,还是它们完全不同?
可能重复:
SQL中的GROUP BY /聚合函数混淆
我收到了一个错误 -
列'Employee.EmpID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中.
select loc.LocationID, emp.EmpID
from Employee as emp full join Location as loc
on emp.LocationID = loc.LocationID
group by loc.LocationID
Run Code Online (Sandbox Code Playgroud)
这种情况符合Bill Karwin给出的答案.
修正以上,适合ExactaBox的答案 -
select loc.LocationID, count(emp.EmpID) -- not count(*), don't want to count nulls
from Employee as emp full join Location as loc
on emp.LocationID = loc.LocationID
group by loc.LocationID
Run Code Online (Sandbox Code Playgroud)
原始问题 -
对于SQL查询 -
select *
from Employee as emp full join Location as loc
on emp.LocationID = loc.LocationID
group by (loc.LocationID) …Run Code Online (Sandbox Code Playgroud) 我有一张看起来像这个来电者'makerar'的桌子
cname | wmname | avg
--------+-------------+------------------------
canada | zoro | 2.0000000000000000
spain | luffy | 1.00000000000000000000
spain | usopp | 5.0000000000000000
Run Code Online (Sandbox Code Playgroud)
我想为每个cname选择最大平均值.
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname;
Run Code Online (Sandbox Code Playgroud)
但我会收到一个错误,
ERROR: column "makerar.wmname" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname;
Run Code Online (Sandbox Code Playgroud)
所以我这样做
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname, wmname;
Run Code Online (Sandbox Code Playgroud)
但这不会给出意图的结果,并显示下面的错误输出.
cname | wmname | max
--------+--------+------------------------
canada …Run Code Online (Sandbox Code Playgroud) 根据MSDN,Median不能作为Transact-SQL中的聚合函数使用.但是,我想知道是否可以创建此功能(使用Create Aggregate函数,用户定义函数或其他方法).
这样做的最佳方式(如果可能) - 允许在聚合查询中计算中值(假设数值数据类型)?
该文档说明如何将GROUPBY对象在同一时间使用的字典与输出列名作为关键字应用多个功能:
In [563]: grouped['D'].agg({'result1' : np.sum,
.....: 'result2' : np.mean})
.....:
Out[563]:
result2 result1
A
bar -0.579846 -1.739537
foo -0.280588 -1.402938
Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于Series groupby对象.当dict类似地传递给一个由DataFrame组成的组时,它希望键是该函数将应用于的列名.
我想要做的是将多个函数应用于多个列(但某些列将被多次操作).此外,某些函数将依赖于groupby对象中的其他列(如sumif函数).我目前的解决方案是逐列,并执行类似上面的代码,使用lambdas作为依赖于其他行的函数.但这需要很长时间,(我认为迭代一个groupby对象需要很长时间).我将不得不改变它,以便我在一次运行中遍历整个groupby对象,但我想知道在pandas中是否有一种内置的方式可以做得有点干净.
例如,我尝试过类似的东西
grouped.agg({'C_sum' : lambda x: x['C'].sum(),
'C_std': lambda x: x['C'].std(),
'D_sum' : lambda x: x['D'].sum()},
'D_sumifC3': lambda x: x['D'][x['C'] == 3].sum(), ...)
Run Code Online (Sandbox Code Playgroud)
但正如预期的那样,我得到一个KeyError(因为如果agg从DataFrame中调用键,则键必须是一个列).
有没有内置的方法来做我想做的事情,或者可能添加这个功能的可能性,还是我只需要手动迭代组合?
谢谢
USE AdventureWorks2008R2;
GO
SELECT SalesOrderID, ProductID, OrderQty
,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Total'
,AVG(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Avg'
,COUNT(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Count'
,MIN(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Min'
,MAX(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Max'
FROM Sales.SalesOrderDetail
WHERE SalesOrderID IN(43659,43664);
Run Code Online (Sandbox Code Playgroud)
我读到了那个条款,我不明白为什么需要它.这个功能Over有什么作用?怎么Partitioning By办?为什么我不能写一个查询Group By SalesOrderID?
我有一张这样的桌子
Movie Actor
A 1
A 2
A 3
B 4
Run Code Online (Sandbox Code Playgroud)
我想得到一部电影的名称和该电影中的所有演员,我希望结果是这样的格式:
Movie ActorList
A 1, 2, 3
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我刚刚让自己陷入了一些SQL困境.我不认为我能说出这个问题 - 所以让我告诉你.
我有两张桌子,一张叫人,一张叫约会.我试图返回一个人的约会数量(包括他们有零).约会包含的person_id并有person_id每个预约.所以这COUNT(person_id)是一种明智的做法.
查询:
SELECT person_id, COUNT(person_id) AS "number_of_appointments"
FROM appointment
GROUP BY person_id;
Run Code Online (Sandbox Code Playgroud)
将正确返回,person_id具有的约会数量.但是,没有退回约会0的人(显然因为他们不在该表中).
调整语句以从person表中获取person_id给出了类似于:
SELECT person.person_id, COUNT(appointment.person_id) AS "number_of_appointments"
FROM appointment
JOIN person ON person.person_id = appointment.person_id
GROUP BY person.person_id;
Run Code Online (Sandbox Code Playgroud)
然而,这仍然只会返回一个有约会的person_id而不是我想要的那个与0个约会的人一起返回的人!
有什么建议吗?
我希望能够从电子邮件表中选择一堆行,并通过发件人对它们进行分组.我的查询如下所示:
SELECT
`timestamp`, `fromEmail`, `subject`
FROM `incomingEmails`
GROUP BY LOWER(`fromEmail`)
ORDER BY `timestamp` DESC
Run Code Online (Sandbox Code Playgroud)
查询几乎按我的意愿运行 - 它选择通过电子邮件分组的记录.问题是主题和时间戳不对应于特定电子邮件地址的最新记录.
例如,它可能会返回:
fromEmail: john@example.com, subject: hello
fromEmail: mark@example.com, subject: welcome
Run Code Online (Sandbox Code Playgroud)
当数据库中的记录是:
fromEmail: john@example.com, subject: hello
fromEmail: john@example.com, subject: programming question
fromEmail: mark@example.com, subject: welcome
Run Code Online (Sandbox Code Playgroud)
如果"编程问题"主题是最新的,那么在分组电子邮件时如何让MySQL选择该记录呢?
sql ×8
group-by ×4
mysql ×3
sql-server ×3
postgresql ×2
clause ×1
median ×1
pandas ×1
python ×1
sql-order-by ×1
t-sql ×1