query:我有4行,需要将3行的结果添加到一行中,并保持最后一行不变

ohtdti5x  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(354)

对于这个问题,我有一个棘手的问题。首先是代码:

SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type

我在干什么?
例如,我在数警察的抢劫报告,是由不同类型的用户制作的。在我的示例中,“admin”用户报告了6起代码为“2”(抢劫)的事件,以此类推,如“where”子句所示(事件必须是抢劫,也是代码2)。
这将产生以下结果:

+-----------------------+----------+
| user_type_description | Quantity |
+-----------------------+----------+
| Admin                 |        6 |
| Moderator             |        8 |
| Fully_registered_user |        8 |
| anonymous_user        |        9 |
+-----------------------+----------+

基本上 Admin,Moderator and Fully_registered_userappropriately registered users . 我需要在结果中添加它们,结果显示如下:

+--------------+------------+
| Proper_users | Anonymous  |
+--------------+------------+
|           22 |          9 |
+--------------+------------+

我不善于与人相处 sql . 感谢您的帮助。谢谢。

3pvhb19x

3pvhb19x1#

您可以尝试使用基于当前结果集的条件聚合函数。 SUMCASE WHEN 表情。

SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
       SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
FROM (
    SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
    FROM incident
    INNER JOIN user ON incident.user_id=user.user_id
    INNER JOIN user_type ON user.user_type=user_type.user_type
    WHERE incident.code=2
    GROUP BY user.user_type
) t1
643ylb08

643ylb082#

您只需要条件聚合:

SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
        SUM( ut.user_type_description IN ('anonymous_user') as anonymous
FROM incident i INNER JOIN
     user u
     ON i.user_id = u.user_id INNER JOIN
     user_type ut
     ON u.user_type = ut.user_type
WHERE i.code = 2;

笔记:
表别名使查询更易于编写和读取。
它使用mysql快捷方式来添加值——只需添加booelean表达式。

62o28rlo

62o28rlo3#

我会用cte来解决这个问题,但最好把这个关联放在一个表中。

WITH
user_type_categories
AS
(
    SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]  
    UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]  
    UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]  
    UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]  
)
SELECT 
      CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
        SUM(incident.user_id) 
      END AS [Proper_Users_Quantity]
     , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
        SUM(incident.user_id) 
      END AS [Anonymous_Quantity]
FROM 
    [incident]
    INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
    INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
    LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
WHERE 
    [incident].[code] = 2

相关问题