我有一个派生表,其结果如下:
THENAME | THESTATUS | THECOST
----------------------------------
name1 | statusX | cost1.1
name1 | statusX | cost1.2
name2 | statusZ | cost2.1
name2 | statusZ | cost2.1
name3 | statusY | cost3.1
(...)
nameN | status(X,Y,Z) | cost
每个名字总是有相同的地位,但成本将是不同的。我想得出一个类似这样的结果
Status Groups | Count of Names | Total Cost of Group
X 2 10
Y 5 20
Z 4 5
基本上,显示每个状态的总成本和每个状态包含的项目数。
去做类似的事情
SELECT
thestatus,
COUNT(DISTINCT thestatus),
SUM(thecost)
FROM THETABLE
GROUP BY thestatus
不起作用,我在想是否有可能同时对两列进行分组。。
编辑-https://www.db-fiddle.com/f/bb1rsy8gv35fgei3lr1tqw/0
编辑-不确定它是mysql版本还是什么,但在示例数据中,我认为它工作正常,但在实际查询中没有。重新创建整个模式和整个查询是不可能的,但可能是另外一回事。。。
2条答案
按热度按时间taor4pac1#
pgx2nnw82#
你可以像下面这样尝试,删除
演示