获得不同值的总数

时间:2011-05-04 14:17:48

标签: mysql

我有一个包含3个字段Brand,Mark和BenchMark Score的mysql表。

有关数据如何在此表中的说明。

1.A品牌可以有不同的型号。 2.Different Brands可以有相同名称的模型 3.特定品牌 - 模型组合可以有不同的基准分数。

因此表数据看起来像这样

Brand ModelName Benchmark

B1    M1         1000
B1    M1         2000
B1    M2         3000
B1    M2         2000
B2    M1         3000
B2    M3         4000

现在我需要找到的是报告特定基准分数的时间。

在这里,我们应该只计算那些基准分数,这是模型 - 品牌组合的最大值,而不是所有那些基准分数。

结果应该是这样的

Score Times
3000  2
2000  1
4000  1

那么,如果我需要这个结果,那么mysql语句应该是什么。请帮助

2 个答案:

答案 0 :(得分:2)

SELECT * FROM
(SELECT MX,COUNT(1) SC FROM
(SELECT MAX(MaxScores.MaxScore) MX,ScoreCounts.ScoreCount FROM
(
    SELECT MAX(BenchMark) MaxScore,Brand,ModelName
    FROM benchmarks GROUP BY Brand,ModelName
) MaxScores
INNER JOIN
(
    SELECT COUNT(1) ScoreCount,Brand,ModelName
    FROM benchmarks GROUP BY Brand,ModelName
) ScoreCounts
USING (Brand,ModelName)
GROUP BY Brand,ModelName
) A GROUP BY MX) AA ORDER BY SC DESC,MX;

此查询产生了您正在查找的内容,并按照您的问题的确切顺序显示结果。我用问题中的样本数据试了一下。这是我得到的:

mysql> use test
Database changed
mysql> drop table if exists benchmarks;
Query OK, 0 rows affected (0.05 sec)

mysql> CREATE TABLE benchmarks (Brand CHAR(2),ModelName CHAR(2),BenchMark INT,key (Brand,ModelName));
Query OK, 0 rows affected (0.14 sec)

mysql> INSERT INTO benchmarks VALUES
    -> ('B1','M1',1000),
    -> ('B1','M1',2000),
    -> ('B1','M2',3000),
    -> ('B1','M2',2000),
    -> ('B2','M1',3000),
    -> ('B2','M3',4000);
Query OK, 6 rows affected (0.03 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE benchmarks\G
*************************** 1. row ***************************
       Table: benchmarks
Create Table: CREATE TABLE `benchmarks` (
  `Brand` char(2) DEFAULT NULL,
  `ModelName` char(2) DEFAULT NULL,
  `BenchMark` int(11) DEFAULT NULL,
  KEY `Brand` (`Brand`,`ModelName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> SELECT * FROM benchmarks;
+-------+-----------+-----------+
| Brand | ModelName | BenchMark |
+-------+-----------+-----------+
| B1    | M1        |      1000 |
| B1    | M1        |      2000 |
| B1    | M2        |      3000 |
| B1    | M2        |      2000 |
| B2    | M1        |      3000 |
| B2    | M3        |      4000 |
+-------+-----------+-----------+
6 rows in set (0.01 sec)

mysql> SELECT * FROM
    -> (SELECT MX,COUNT(1) SC FROM
    -> (SELECT MAX(MaxScores.MaxScore) MX,ScoreCounts.ScoreCount FROM
    -> (
    ->     SELECT MAX(BenchMark) MaxScore,Brand,ModelName
    ->     FROM benchmarks GROUP BY Brand,ModelName
    -> ) MaxScores
    -> INNER JOIN
    -> (
    ->     SELECT COUNT(1) ScoreCount,Brand,ModelName
    ->     FROM benchmarks GROUP BY Brand,ModelName
    -> ) ScoreCounts
    -> USING (Brand,ModelName)
    -> GROUP BY Brand,ModelName
    -> ) A GROUP BY MX) AA ORDER BY SC DESC,MX;
+------+----+
| MX   | SC |
+------+----+
| 3000 |  2 |
| 2000 |  1 |
| 4000 |  1 |
+------+----+
3 rows in set (0.01 sec)

试一试!!!

答案 1 :(得分:0)

SELECT  b.model, b.brand, bd.score, COUNT(*)
FROM    (
        SELECT  model, brand, MAX(score) AS mscore
        FROM    benchmark
        GROUP BY
                model, brand
        ) bd
JOIN    benchmark b
ON      b.model = bd.model
        AND b.brand = bd.brand
        AND b.score = bd.mscore
GROUP BY
        b.model, b.brand

(model, brand, score)上创建一个索引,以便快速工作。

<强>更新

SELECT  score, COUNT(*) AS cnt
FROM    benchmark
GROUP BY
        score
ORDER BY
        cnt DESC