从同一张表格中统计 MySql 列。

huangapple go评论84阅读模式
英文:

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;

Demo here

答案2

得分: 1

SELECT `label`, count(`sublabel`)
FROM `scraping_data`
WHERE `label` = 'application' -- < optional
GROUP BY `label`

Group byCount

英文:
SELECT `label`, count(`sublabel`)
FROM `scraping_data`
WHERE `label` = &#39;application&#39; -- &lt; optional
GROUP BY `label`

Group by and Count.

huangapple
  • 本文由 发表于 2023年5月6日 17:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定