如何选择数据并将其添加到特定行? [英] How to select and add data to specific rows?

查看:55
本文介绍了如何选择数据并将其添加到特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何循环浏览一列中的值以查找一组特定的字符,然后,如果该行具有这些字符,则在新列中为该行专门分配一个值?

How would you loop through the values in one column looking for a specific set of characters and then, if the row has those characters, assign a value specifically to that row in a new column?

例如,在这个问题中,我们必须为每个不同的响应分配一个点,为每个同意的响应分配零.

For example, in this problem we must assign one point to every disagreeing response and zero to the agreeing ones.

     Id   Gender   Age Participate Question            Response Score
    <int>  <chr> <int>       <int>    <chr>               <chr> <dbl>
1     16   Male    20           1       Q1   Slightly Disagree     0
2     17   Male    40           1       Q1    Definitely Agree     0
3     18   Male    33           1       Q1    Definitely Agree     0
4     19   Male    18           1       Q1    Definitely Agree     0
5     20   Male    24           1       Q1 Definitely Disagree     0
6     21 Female    42           1       Q1   Slightly Disagree     0
7     22 Female    19           1       Q1      Slightly Agree     0
8     28 Female    49           1       Q1   Slightly Disagree     0
9     29 Female    17           1       Q1      Slightly Agree     0
10    31   Male    18           1       Q1      Slightly Agree     0`

推荐答案

根据我对以下问题的理解,查询应该对您有用.

According to my understanding of your problem below query should work for you.

UPDATE yourSchema.yourTable anAlias
SET Score = finalData.Score
FROM
  (SELECT Id,
          CASE
              WHEN Response LIKE '%Disagree%' THEN 1
              ELSE 0
          END AS score
   FROM yourSchema.yourTable) finalData
WHERE anAlias.Id = finalData.Id;

这篇关于如何选择数据并将其添加到特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆