2

我有这个 MySQL 查询:

select Region,
CONCAT('$', FORMAT(AVG(sales), 0)) as 'Average_Sales_by_Region', 
count(*) as '# of Dist in state'
from dist, Regions_US
where dist.state=Regions_US.State
group by Region ORDER BY AVG(sales) DESC;

以下是上述 MySQL 查询的输出:

+--------------------+-------------------------------+-------------------------+
| Region             | Average_Sales_by_Region       | # of Dist in state      |
+--------------------+-------------------------------+-------------------------+
| Alaska-Hawaii      | $8,150                        |                       4 |
| Mountain           | $20,216                       |                      74 |
| West North Central | $18,268                       |                      40 |
| South Atlantic     | $16,225                       |                     178 |
| East South Central | $14,967                       |                      30 |
| West South Central | $13,704                       |                     125 |
| East North Central | $12,668                       |                      79 |
| New England        | $11,916                       |                      32 |
| Pacific            | $11,553                       |                     120 |
| Middle Atlantic    | $10,292                       |                     131 |
+--------------------+-------------------------------+-------------------------+

有一个名为 company_name 的字段,我想对其进行 DISTINCT:

select DISTINCT company_name,
Region,CONCAT('$', FORMAT(AVG(sales), 0)) as 'Average_Sales_by_Region', 
count(*) as '# of Dist in state'
from dist, Regions_US
where dist.state=Regions_US.State
group by Region ORDER BY AVG(sales) DESC;

我希望它在“company_name”的字段名称上是 DISTINCT,但我不希望 company_name 字段显示在输出中。有没有办法在不显示 DISTINCT company_name 的情况下执行它?它的语法是什么,它会在上面的 MySQL 查询中使用什么?还是有其他方法可以做到这一点?谢谢!

4

1 回答 1

2

将其添加到GROUP BY子句并从子句中删除SELECT

SELECT
  Region,
  CONCAT('$', FORMAT(avgSales, 0)) as 'Average_Sales_by_Region', 
  TheCount as '# of Dist in state'
FROM
(
   SELECT 
     Region, 
     AVG(Sales) avgSales, 
     COUNT(*) theCount
   from dist, Regions_US
   where dist.state=Regions_US.State
   group by Region,company_name
) AS sub       
ORDER BY avgSales DESC;

更新:如果你想在同一个子句中选择 thesales和 the ,你不能这样做,你不应该这样做。MySQL 允许你这样做,但你会为每组.AVG(sales)SELECTsalesRegion, company_name

使用 a 时,这称为单值规则GROUP BY,遵循此规则是标准。这意味着,子句中的每一列都SELECT必须在 aGROUP BY或聚合函数中。您不能选择不在GROUP BY聚合函数中的行。在您的情况下,您已经sales使用聚合函数选择了AVG,为什么sales还要在同一个查询中选择?

但是,如果你想这样做,你可以这样做:

SELECT
  Region,
  CONCAT('$', FORMAT(avgSales, 0)) as 'Average_Sales_by_Region', 
  TheCount as '# of Dist in state',
  (SELECT sales
   FROM dist d2
   WHERE d2.Region       = d1.Region
     AND d2.company_name = d1.company_name) AS Sales
FROM
(
   SELECT 
     Region, 
     AVG(Sales) avgSales, 
     COUNT(*) theCount
   from dist
   INNER JOIN Regions_US ON dist.state = Regions_US.State
   group by Region,company_name
) AS sub       
ORDER BY avgSales DESC;

假设这Region, company_name是从dist表格中出来的。

请注意:INNER JOIN在您的查询中使用而不是隐式连接语法,它们是相同的。

但我不确定sales每组的值是否一致Region, Company_name

于 2013-01-30T13:09:32.490 回答