MySQL在1个表中选择不同的记录,并根据另一个表中的值计算每个组

时间:2014-01-20 20:22:43

标签: mysql sql

表1:

| user           | bid |
---------------------------
|    may    |      0.06      |
|    dee    |      0.05      |
|    jay    |      0.04      |
|    mac    |      0.03      |
|    dee    |      0.02      |
|    mac    |      0.01      |

表2:

| user          | ratio|
---------------------------
|    dee    |      .25      |
|    jay    |      .45      |
|    mac    |      .85      |
|    fil    |      .75      |
|    may    |      .95      |

我想根据表2中的比例计算表1中有多少不同的用户属于特定范围(0- .99)。

输出:

| Ratio_Group     | Count|
---------------------------
|    0.00-0.25 |      1     |
|    0.25-0.50 |      1     |
|    0.50-0.75 |      0     |
|    0.75-0.99 |      2     |

每个比率分组是否有1个查询我可以根据表2中的分组来计算表1中的不同用户?我使用了IF和COUNT语句的组合,但表1中的每一行都被评估和计算,而不仅仅是DISTINCT用户,因此我得到了一个夸大的结果。

例如:

 COUNT((IF table1.user <0.25,1,0))
 COUNT((IF table1.user BETWEEN 0.25 AND 0.50,1,0))
 etc...

4个查询中的每一个都将用于存储过程中,因此每个比率分组需要1个查询,因此我可以将每个组的结果分配给存储过程中的变量。

2 个答案:

答案 0 :(得分:1)

您可以SUMBETWEEN

组合使用DISTINCT
SELECT
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.00 AND 0.25) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user

SELECT
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.25 AND 0.50) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user

SELECT
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.50 AND 0.75 ) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user

SELECT
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.75 AND 0.99) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user

或者您可以将它们组合为

SELECT  '0.00 - 0.25' Ratio_Group ,
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.00 AND 0.25) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user
UNION ALL
SELECT '0.25 - 0.50' Ratio_Group ,
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.25 AND 0.50) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user
UNION ALL
SELECT '0.50 - 0.75' Ratio_Group ,
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.50 AND 0.75 ) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user
UNION ALL
SELECT '0.75 - 0.99' Ratio_Group ,
SUM(DISTINCT `table2`.`ratio` BETWEEN 0.75 AND 0.99) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) GROUP BY table1.user

编辑以下查询将统计每个比率组的不同用户

SELECT  '0.00 - 0.25' Ratio_Group ,
COUNT(DISTINCT `table2`.`user` ) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) 
WHERE `table2`.`ratio` BETWEEN 0.00 AND 0.25

UNION ALL

SELECT '0.25 - 0.50' Ratio_Group ,
COUNT(DISTINCT `table2`.`user`) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) 
WHERE  `table2`.`ratio` BETWEEN 0.25 AND 0.50

UNION ALL

SELECT '0.50 - 0.75' Ratio_Group ,
COUNT(DISTINCT `table2`.`user` ) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) 
WHERE `table2`.`ratio` BETWEEN 0.50 AND 0.75
UNION ALL

SELECT '0.75 - 0.99' Ratio_Group ,
COUNT(DISTINCT `table2`.`user`) `count`
FROM table1 JOIN table2 ON (table1.user =table2.user) 
WHERE `table2`.`ratio` BETWEEN 0.75 AND 0.99

See Fiddle Demo

答案 1 :(得分:0)

如果您不介意只抓住拥有会员的群组,您可以这样做。它使用内部查询来获取唯一成员及其相应的比率,然后使用包含表将这些比率按比例分组。好的一点是你没有硬编码行组,这不像SQL那样;缺点是你没有看到计数为0的行“成员”。

SELECT
    CONCAT(
        ((CEIL(ratio * 4) * 0.25 ) - 0.25),
        " - ",
        (CEIL( ratio *4 ) * 0.25)
    ) AS ratio_group,
    COUNT(user) AS user_count
FROM (
    SELECT
        t1.user,
        t2.ratio
    FROM
        `table1` t1
    INNER JOIN
        `table2` t2 ON t2.user = t1.user
    GROUP BY
        t1.user
) virtual_table
GROUP BY
    CEIL(ratio * 4)

根据您的附加注释,您可以将所有数据组合成一个字符串,并分配给这样的变量。这是一个混蛋的地狱。你对这些数据所做的事情(那时它基本上是一个CSV字符串)取决于你,哈哈。

@data = 
(SELECT
    GROUP_CONCAT(combined SEPARATOR ';')
FROM (
    SELECT
        CONCAT(
            '"',
            CONCAT(
                ((CEIL(ratio * 4) * 0.25) - 0.25),
                " - ",
                (CEIL( ratio *4 ) * 0.25)
            ),
            '",',
            COUNT( user )
        ) AS combined
    FROM (
        SELECT
            t1.user,
            t2.ratio
        FROM 
            `table1` t1
        INNER JOIN
            `table2` t2 ON t2.user = t1.user
        GROUP BY
            t1.user
    ) virtual_table
    GROUP BY
        CEIL( ratio *4 )
) virtual_table2 )