英文:
MySql count column based from same table
问题
这种类型的问题已经有人问过,但那不能解决我的问题。
我有一个名为 scraping_data 的表格
+-----------------------------------------------------------+
| id      | accountId | label         | subLabel  | status|
+-----------------------------------------------------------+
| 1       | 1         | 应用程序       | 好          | 1     |
| 2       | 1         | 应用程序       | 差          | 1     |
| 3       | 1         | 应用程序       | 好          | 1     |
| 4       | 1         | 渠道           | 质量        | 1     |
| 5       | 1         | 应用程序       | 好          | 1     |
| 6       | 1         | 渠道           | 差          | 1     |
+-----------------------------------------------------------+
我想按 subLabel 获取计数,例如这里 label 应用程序 出现 4 次,其中好出现 3 次,差出现 1 次。同样 渠道 出现 2 次,其中质量出现 1 次,差出现 1 次。
我的输出将是:
应用程序 好计数 3 和差计数 1
查询:
SELECT * FROM scraping_data
英文:
This type of questions already asked but that is not solve my problem.
I have table called scraping_data
+-----------------------------------------------------------+
| id		| accountId	| label			| subLabel	| status|
+-----------------------------------------------------------+
| 1			| 1			| Application	| Nice		| 1		|
| 2			| 1			| Application	| poor		| 1		|
| 3			| 1			| Application	| Nice		| 1		|
| 4			| 1			| Chennal		| Quality	| 1		|
| 5			| 1			| Application	| Nice		| 1		|
| 6			| 1			| Channel		| poor		| 1		|
+-----------------------------------------------------------+
Here I want to take counts by subLabel, for Example here label Application comes 4 times with Nice  times and poor 1 time. And same Channel come 2 times with Quality 1 time, poor 1 time.
My output will be like:
Application Nice count 3 and poor count 1
query:
SELECT * FROM scraping_data
答案1
得分: 2
你可以使用条件聚合和 group by 以及 sum() 来实现:
select label, sum(case when subLabel = 'Nice' then 1 else 0 end) as nice_count,
              sum(case when subLabel = 'poor' then 1 else 0 end) as poor_count
from mytable
group by label
或者使用 count():
select label, count(case when subLabel = 'Nice' then 1 end) as nice_count,
              count(case when subLabel = 'poor' then 1 end) as poor_count
from mytable
group by label
要动态生成透视数据,你可以使用预准备语句:
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when subLabel = ''',
      subLabel,
      ''' then 1 else 0 end) AS `',
      subLabel, '_count`'
    )
  ) INTO @sql
FROM
  mytable;
SET @sql = CONCAT('SELECT label, ', @sql, ' 
                  FROM mytable 
                   GROUP BY label');
                   
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
英文:
You can do it using the conditional aggregation using group by and sum() :
select label, sum(case when subLabel = 'Nice' then 1 else 0 end) as nice_count,
              sum(case when subLabel = 'poor' then 1 else 0 end) as poor_count
from mytable
group by label
Or using count() :
select label, count(case when subLabel = 'Nice' then 1 end) as nice_count,
              count(case when subLabel = 'poor' then 1 end) as poor_count
from mytable
group by label
To dynamically generate pivoted data you can use Prepared statements :
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when subLabel = ''',
      subLabel,
      ''' then 1 else 0 end) AS `',
      subLabel, '_count`'
    )
  ) INTO @sql
FROM
  mytable;
SET @sql = CONCAT('SELECT label, ', @sql, ' 
                  FROM mytable 
                   GROUP BY label');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
答案2
得分: 1
SELECT `label`, count(`sublabel`)
FROM `scraping_data`
WHERE `label` = 'application' -- < optional
GROUP BY `label`
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论