选择查询中的号码组

发布于 2024-11-07 07:13:53 字数 357 浏览 0 评论 0原文

我有一个表,其中保存用户编号及其“分数”。

user_number | score
  0832824       6
  0478233       3
    ...        ...

分数从 3 到 15。我想创建一个查询来获取分数以及具有该分数的用户总数。但我需要制作四组不同的分数:12 到 15、8 到 11、5 到 7 和小于 5。

有点像这样:

  score  | total_users
12 to 15      5000
 8 to 11      3000
   ...         ...

感谢您提供的任何答案!

I have a table where I keep users numbers and their "score"

user_number | score
  0832824       6
  0478233       3
    ...        ...

The score goes from 3 to 15. I want to create a query that would fetch the score and the total of users with this score. But I need to make four different groups of score : 12 to 15, 8 to 11, 5 to 7 and less than 5.

Sort of like this :

  score  | total_users
12 to 15      5000
 8 to 11      3000
   ...         ...

Thanks for any answer you can provide!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与酒说心事 2024-11-14 07:13:53
SELECT t.range AS score, COUNT(*) AS total_users from 
(   
    SELECT CASE WHEN score BETWEEN 12 AND 15 THEN '12 to 15'     
                WHEN score BETWEEN 8 AND 11 THEN '8 to 11' 
                WHEN score BETWEEN 5 AND 7 THEN '5 to 7'    
                WHEN score < 5 THEN 'less than 5' 
           END AS range   
    FROM scores) t 
GROUP BY t.range 
SELECT t.range AS score, COUNT(*) AS total_users from 
(   
    SELECT CASE WHEN score BETWEEN 12 AND 15 THEN '12 to 15'     
                WHEN score BETWEEN 8 AND 11 THEN '8 to 11' 
                WHEN score BETWEEN 5 AND 7 THEN '5 to 7'    
                WHEN score < 5 THEN 'less than 5' 
           END AS range   
    FROM scores) t 
GROUP BY t.range 
梦里寻她 2024-11-14 07:13:53

您可以使用 BETWEEN 子句单独选择每个组,并使用组合结果UNION

SELECT  score = '12 to 15', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 12 AND 15
UNION ALL
SELECT  score = '8 to 11', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 8 AND 11
UNION ALL
SELECT  score = '5 to 7', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 5 AND 7
UNION ALL
SELECT  score = 'less than 5', total_users = COUNT(*)
FROM    ATable
WHERE   score < 5

You could select each group individually using a BETWEEN clause and combine the results using a UNION

SELECT  score = '12 to 15', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 12 AND 15
UNION ALL
SELECT  score = '8 to 11', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 8 AND 11
UNION ALL
SELECT  score = '5 to 7', total_users = COUNT(*)
FROM    ATable
WHERE   score BETWEEN 5 AND 7
UNION ALL
SELECT  score = 'less than 5', total_users = COUNT(*)
FROM    ATable
WHERE   score < 5
独自唱情﹋歌 2024-11-14 07:13:53
SELECT SUM(CASE WHEN score BETWEEN 12 AND 15 THEN 1 ELSE 0 END) AS [12-15],
       SUM(CASE WHEN score BETWEEN 8 AND 11 THEN 1 ELSE 0 END) AS [8-11],
       SUM(CASE WHEN score BETWEEN 5 AND 7 THEN 1 ELSE 0 END) AS [5-7],
       SUM(CASE WHEN score < 5 THEN 1 ELSE 0 END) AS [<5]
    FROM YourTable
SELECT SUM(CASE WHEN score BETWEEN 12 AND 15 THEN 1 ELSE 0 END) AS [12-15],
       SUM(CASE WHEN score BETWEEN 8 AND 11 THEN 1 ELSE 0 END) AS [8-11],
       SUM(CASE WHEN score BETWEEN 5 AND 7 THEN 1 ELSE 0 END) AS [5-7],
       SUM(CASE WHEN score < 5 THEN 1 ELSE 0 END) AS [<5]
    FROM YourTable
墨小墨 2024-11-14 07:13:53
CREATE TABLE scores
(
   score         NUMBER,
   user_number   VARCHAR2 (20)
);

INSERT INTO scores
     VALUES (3, '000001');

INSERT INTO scores
     VALUES (4, '000002');

INSERT INTO scores
     VALUES (4, '000003');

INSERT INTO scores
     VALUES (12, '000005');

COMMIT;

  SELECT score, COUNT (DISTINCT user_number) number_of_users
    FROM (SELECT CASE
                    WHEN score < 5 THEN 'Below 5'
                    WHEN score >= 5 AND score <= 7 THEN '5 to 7'
                    WHEN score >= 8 AND score <= 11 THEN '8 to 11'
                    WHEN score >= 12 AND score <= 15 THEN '12 to 15'
                    ELSE 'unclassified'
                 END
                    score,
                 user_number
            FROM scores)
GROUP BY score;
CREATE TABLE scores
(
   score         NUMBER,
   user_number   VARCHAR2 (20)
);

INSERT INTO scores
     VALUES (3, '000001');

INSERT INTO scores
     VALUES (4, '000002');

INSERT INTO scores
     VALUES (4, '000003');

INSERT INTO scores
     VALUES (12, '000005');

COMMIT;

  SELECT score, COUNT (DISTINCT user_number) number_of_users
    FROM (SELECT CASE
                    WHEN score < 5 THEN 'Below 5'
                    WHEN score >= 5 AND score <= 7 THEN '5 to 7'
                    WHEN score >= 8 AND score <= 11 THEN '8 to 11'
                    WHEN score >= 12 AND score <= 15 THEN '12 to 15'
                    ELSE 'unclassified'
                 END
                    score,
                 user_number
            FROM scores)
GROUP BY score;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文