简体   繁体   English

如何使用mysql基于同一个表的其他两列填充列?

[英]how do I populate a column based on other two columns of the same table using mysql?

My data is as below (table name is botdata) 我的数据如下(表名是botdata)

数据

Now i need to pivot this data to have Chatsession ID as rows and Metrics as columns. 现在我需要将此数据透视为将Chatsession ID作为行,将Metrics作为列。 There has to be a new column Met014 which should show 1 only when Met003 and Met004 is 1. Below is the code I am using and the result I am getting. 必须有一个新的列Met014,只有当Met003和Met004为1时才应显示1.下面是我正在使用的代码和我得到的结果。 Everything else is fine but how do i populate Met014 correctly? 其他一切都很好,但我如何正确填充Met014?

SELECT   ChatSessionID, 
 COUNT(IF(Metrics = "Met001", Metrics, NULL)) AS Met001,
COUNT(IF(Metrics = "Met002", Metrics, NULL)) AS Met002, 
COUNT(IF(Metrics = "Met003", Metrics, NULL)) AS Met003,  
COUNT(IF(Metrics = "Met004", Metrics, NULL)) AS Met004,  
COUNT(IF(Metrics = "Met005", Metrics, NULL)) AS Met005,
  COUNT(IF(Metrics = "Met006", Metrics, NULL)) AS Met006,
  COUNT(IF(Metrics = "Met007", Metrics, NULL)) AS Met007,
COUNT(IF(Metrics = "Met008", Metrics, NULL)) AS Met008,
COUNT(IF(Metrics = "Met009", Metrics, NULL)) AS Met009,
COUNT(IF(Metrics = "Met010", Metrics, NULL)) AS Met010,
COUNT(IF(Metrics = "Met011", Metrics, NULL)) AS Met011,
COUNT(IF(Metrics = "Met012", Metrics, NULL)) AS Met012,
COUNT(CASE WHEN Metrics="Met003" And Metrics="Met004" THEN Metrics ELSE NULL END) AS Met014
 FROM
botdata b
WHERE
  b.Metrics BETWEEN "Met001" AND "Met014"
GROUP BY
  ChatSessionID;

结果

I would phrase this using SUM() . 我会用SUM()来表达这个。 I think you can then do what you want with LEAST() : 我想你可以用LEAST()做你想做的事:

SELECT ChatSessionID, 
       SUM(Metrics = 'Met001') AS Met001,
       SUM(Metrics = 'Met002') AS Met002,
       . . . . . . . . . . . . . . . . .,
       SUM(Metrics = 'Met012') AS Met012,
       LEAST(SUM(Metrics = 'Met003'), SUM(Metrics = 'Met004')) AS Met014
FROM botdata b
GROUP BY ChatSessionID;

If your columns Met001 to Met013 can only take a value of 1, 0 or NULL, try something like this: 如果您的列Met001到Met013只能取值1,0或NULL,请尝试以下操作:

SELECT 
        T.*, (Met003 + Met004) = 2 AS Met014
    FROM
        (SELECT 
            ChatSessionID,
                Metrics = 'Met001' AS Met001,
                Metrics = 'Met002' AS Met002,
                Metrics = 'Met003' AS Met003,
                Metrics = 'Met004' AS Met004,
                Metrics = 'Met005' AS Met005,
                Metrics = 'Met006' AS Met006,
                Metrics = 'Met007' AS Met007,
                Metrics = 'Met008' AS Met008,
                Metrics = 'Met009' AS Met009,
                Metrics = 'Met010' AS Met010,
                Metrics = 'Met011' AS Met011,
                Metrics = 'Met012' AS Met012,
                Metrics = 'Met013' AS Met013
        FROM
            botdata b
        WHERE
            b.Metrics BETWEEN 'Met001' AND 'Met014'
        GROUP BY ChatSessionID) AS T;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 MySQL 在两列中填充名称? - How do I populate names in two columns using MySQL? 在mysql的同一张表中插入一行后,如何从其他列的内容填充列 - how to populate column from the content of other columns after a row is inserted in the same table in mysql 如何制作一个基于MySQL其他列总和的表? - How do I make a table that is based on the sum of other columns in MySQL? 如何根据同一表中的另一列数据填充一列? - How To Populate One Column Based On Another Columns Data In Same Table? MySQL有条件地基于DISTINCT填充第3列,其中涉及一个表中的其他2列 - MySQL conditionally populate column 3 based on DISTINCT involving 2 other columns in one table 如何根据其他列总结MySQL表中的一列 - How to sum up one of the column in MySQL table based on other columns 合并同一表中的两列并将结果保存到mysql中同一表中的其他列中 - Union two columns in the same table and save the result into other column within same table in mysql 我应该如何用其他两列的平均值更新 MySQL 表列? - How should I update a MySQL table column with the average of other two columns? 如何根据MySQL中的两个不同条件从同一列中检索两个值? - How do I retrieve two values from the same column based on two different conditions in MySQL? 如何根据表中其他列的计算更新列? - How can I update a column based on a calculation of other columns in a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM