SQL对某列中的出现进行计数并计算另一列中的总数

时间:2018-10-23 12:41:16

标签: mysql group-by sum aggregate-functions aggregation

我有一个派生表,其格式如下:

import requests
from bs4 import BeautifulSoup
import uuid
import csv
import dateutil.parser as parser


class met():
    def __init__(self):
        global homePage
        global downloadDir
        global filname
        downloadDir = ""
        uFileName = str(uuid.uuid4())
        filname = downloadDir + uFileName + ".csv"
        homePage = requests.get("https://www.met.ie/")

    def pageHtml(self):

        soup = BeautifulSoup(homePage.content, 'html.parser')
        titleList = soup.findAll('title')
        for div in soup.find_all("div"):
            for section in div.find_all('section', class_ = "container hourly-forecast mb-5",id = "24HourForecast"):
                #print(section)
                for div1 in section.find_all("div"):
                    print(div1)


if __name__ == '__main__':
    objCall = met()
    objCall.pageHtml()

每个名称N始终具有相同的状态,但费用将不同。我正在尝试得出看起来像这样的结果

    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

不起作用,我在想是否可以同时将两列分组。

编辑-https://www.db-fiddle.com/f/bB1RSy8GV35FGEi3Lr1Tqw/0

编辑-不知道它是MySQL的版本还是什么版本,但是在示例数据中,我认为它可以正常运行,但在实际查询中却不行。重新创建整个架构和整个查询是不可能的,但也许是另外一回事了……

2 个答案:

答案 0 :(得分:1)

您可以尝试如下操作,删除不同的

SELECT
     THESTATUS, 
     COUNT(THENAME),
     SUM(THECOST)
FROM THETABLE
GROUP BY thestatus

demo

答案 1 :(得分:0)

SELECT 

DISTINCT THESTATUS as 'Status groups',

COUNT(THENAME) AS 'Count of Names', 

SUM(THECOST) as 'Total Cost of Group' 

FROM THETABLE GROUP BY THESTATUS