Mysql 在sql中找不到销售最多或较少的用户。它只返回组中的第一行

Mysql 在sql中找不到销售最多或较少的用户。它只返回组中的第一行,mysql,sql,Mysql,Sql,下面是代码片段。我试着找出谁卖的最多,谁卖的更少,但它只返回了小组的第一排 SELECT branches.branch_name as Branch, cities.city_name as Province, districts.district_name as District, salesmans.salesman_name as EmpNameMost, salesmans.salesma

下面是代码片段。我试着找出谁卖的最多,谁卖的更少,但它只返回了小组的第一排

    SELECT 
        branches.branch_name as Branch, 
        cities.city_name as Province, 
        districts.district_name as District, 
        salesmans.salesman_name as EmpNameMost, 
        salesmans.salesman_surname as EmpSurMost, 
        MAX(salesmans.sale_count) as SaleAmtMost,
        salesmans.salesman_name as EmpNameLeast, 
        salesmans.salesman_surname as EmpSurLeast, 
        MIN(salesmans.sale_count) as SaleAmtLeast 
    FROM salesmans JOIN branches 
        ON salesmans.branch_id = branches.branch_id 
    JOIN cities 
        ON branches.city_id = cities.city_id 
    JOIN districts 
        ON cities.district_id = districts.district_id 
    WHERE districts.district_name = 'Marmara Bölgesi' 
    GROUP BY branches.branch_name;
我的代码的输出:


欢迎使用S/O。您可能对查询中的最小/最大函数有不正确的理解。这些函数以及count()、sum()、avg()等都被视为聚合函数。“分组依据”表示获取给定组的最小值或最大值。在本例中,您仅按分支机构名称进行分组,因此它将检查所有销售人员,提取最小和最大计数,并将其粘贴在分支机构的一行上

您需要的是一个嵌套查询。您需要首先找到每个分支的最小值和最大值,然后找到具有这些值的销售人员

select
        MinMaxPerBranch Province, 
        MinMaxPerBranch District, 
        s.salesman_name EmpNameMost, 
        s.salesman_surname EmpSurMost, 
        s.salesman_name EmpNameLeast, 
        s.salesman_surname EmpSurLeast, 
        s.sale_count SaleAmtMost,
        s.sale_count SaleAmtLeast,
        case when s.sale_count = MinMaxPerBranch.SaleAmtMost
             then 'Highest Sales' else 'Lowest Sales' end SalesResult
    FROM
       ( select
                s2.branch_id,
                -- to get district and city while we have the joins, pull the
                -- max() on those too since they wont change per their respective
                -- branch. Prevents needing to re-join again on next part of query
                max( d2.district_name ) as District,
                max( c2.city_name ) Province,
                MAX(s2.sale_count) SaleAmtMost,
                MIN(s2.sale_count) SaleAmtLeast 
            FROM
                salesmans s2
                    JOIN branches b2
                        ON s2.branch_id = b2.branch_id 
                        JOIN cities c2
                            ON b2.city_id = c2.city_id
                            JOIN districts d2
                                ON c2.district_id = d2.district_id 
                                AND d2.district_name = 'Marmara Bölgesi' 
            group by
                -- branch_id is the smallest granularity within city and district
                s2.branch_id ) MinMaxPerBranch

        JOIN salesmans s
            -- THIS part will join back to the sales people, but ONLY if they 
            -- have the matching lowest or highest for the branch.
            on MinMaxPerBranch.Branch_id = s.branch_id
            AND (  MinMaxPerBranch.SaleAmtMost = s.sale_count
                OR MinMaxPerBranch.SaleAmtLeast = s.sale_count )

您的查询格式不正确,应生成错误。
选择
列与
分组依据
不一致。示例数据、期望的结果以及对要实现的逻辑的清晰解释都会有所帮助。