简体   繁体   English

如何基于另一个列SQL中的值创建具有计数值的新列

[英]How to create new columns with count values based on the values from another column SQL

I am using Big Query and my table looks like this: 我正在使用Big Query,我的表格如下所示:

ID Month Values ID月值

1234 Aug P 1234年8月
1234 Sept P 1234年9月
3456 Aug D 3456年8月4日
3456 Sept D D 3456年9月
4567 Aug P 4567年8月
4567 Sept D D 4956年9月

I want to generate new column for each month and ID containing P,D as column names and values is their count for each ID and Month. 我想为每个月和包含P,D的ID生成一个新列,列名称和值是每个ID和Month的计数。 Like below: 如下所示:

ID Month PD ID月PD
1234 Aug 1 0 1234八月1 0
1234 Sept 1 0 1234年9月1日0
3456 Aug 0 1 3456八月0 1
3456 Sept 0 1 3456 9月0 1
4567 Aug 1 0 4567八月1 0
4567 Sept 1 1 4567 9月1日1

Below is for BigQuery Standard SQL 以下是BigQuery标准SQL

#standardSQL
SELECT id, month, IF(value='P', 1, 0) AS p, IF(value='D', 1, 0) AS d
FROM `project.dataset.table`

you can play with above using dummy data from your question 您可以使用问题中的虚拟数据进行上述操作

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1234 id, 'Aug' month, 'P' value UNION ALL
  SELECT 1234, 'Sept', 'P' UNION ALL
  SELECT 3456, 'Aug', 'D' UNION ALL
  SELECT 3456, 'Sept', 'D' UNION ALL
  SELECT 4567, 'Aug', 'P' UNION ALL
  SELECT 4567, 'Sept', 'D' 
)
SELECT id, month, IF(value='P', 1, 0) AS p, IF(value='D', 1, 0) AS d
FROM `project.dataset.table`
-- ORDER BY id

with result 结果

Row id      month   p   d    
1   1234    Aug     1   0    
2   1234    Sept    1   0    
3   3456    Aug     0   1    
4   3456    Sept    0   1    
5   4567    Aug     1   0    
6   4567    Sept    0   1     

In case if you have duplicate rows in your table and need to count values vs. just presenting them (as it is in above query) you can use below example 如果您的表中有重复的行并且需要计算值而不是仅仅显示它们(如上面的查询中所示),则可以使用以下示例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1234 id, 'Aug' month, 'P' value UNION ALL
  SELECT 1234, 'Sept', 'P' UNION ALL
  SELECT 1234, 'Sept', 'P' UNION ALL
  SELECT 3456, 'Aug', 'D' UNION ALL
  SELECT 3456, 'Sept', 'D' UNION ALL
  SELECT 4567, 'Aug', 'P' UNION ALL
  SELECT 4567, 'Sept', 'D' 
)
SELECT id, month, COUNTIF(value='P') AS p, COUNTIF(value='D') AS d
FROM `project.dataset.table`
GROUP BY id, month
-- ORDER BY id, month    

with result 结果

Row id      month   p   d    
1   1234    Aug     1   0    
2   1234    Sept    2   0    
3   3456    Aug     0   1    
4   3456    Sept    0   1    
5   4567    Aug     1   0    
6   4567    Sept    0   1    

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

相关问题 根据特定值创建新列另一列 - create new column based on specific values another columns 如何根据 SQL 中另一列中的值计算列中出现的次数? - How to count the number of occurrences in a column based on values in another column in SQL? 如何根据Sql server中3列的3个不同值查找列数? - How to find count of column, based on 3 distinct values of 3 columns in Sql server? 根据另一列的值创建新列 - Create new column based on values of another column 如何在SQL中创建新列的值为-“前缀中的某些单词” +“另一列中的值” - How to create a new column with values as - 'some word as prefix' + 'values from another column' in SQL 使用 SQL 中另一列的所有唯一值创建新列 - Create new column with all unique values from another column in SQL 如何基于另一列SQL中的唯一值创建数组列 - How to create array column based on unique values in another column SQL 如何根据 sql 中另一个表的计数创建一个新表 - how to create a new table based on the count from another table in sql 如何基于另一个表的值在SQL表上创建列 - How to create a column on a SQL table, based on values of another table 根据另一列中的另外两个值选择列的值 - select columns' values based on another two values from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM