我如何计算SQL中唯一行的独特出现?
在XAMPP上使用MySQL。我有这个我正在尝试使用此输出的表。有些行确实重复,因此我需要仅返回唯一的行和每个许可证的出现。
行ID | red_id | cmp_id | cmp_name | cmp_version | app_id | app_name | app_vers | licens | licens | clide eleaster_id | monitor_dig | easuse_count_count_count | 1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
77960664 | unicode | json | sharp(unicode4c) | lts | c | 777 vlight | 67.9 | 77960664 | SHARP | 77956767 | UNICODE4C | 67.9 | ( |
c | for | monitor_id | ) | UNICODE | 77956767 | LTS JSON L | 9.9 | Unicode License V7 | Approved | TV | 76654 | na | 0 |
204 | 79895001 | 87965001 | Common Component1 | 1.2 | 95655001 | Component1 | 1.0 | Commercial License | Approved | CZ | 5260 | na | 2 |
Example of desired output
app_name | app_version | oss_count | commercial_count | total |
---|---|---|---|---|
app_1_name | 1.0 | 45 | 5 | 50 |
APP_2_NAME | 2.0 | 34 | 0 | 34 |
OSS计数是从其中不包含“商业”的列许可证的列计数。 商业计数是来自其中包含“商业”的列许可证的计数。
我已经尝试了以下查询,使我接近我想要的问题。 这只是重复每一行总数。
SELECT DISTINCT app_name, app_version,
(SELECT COUNT(license)
FROM apps_components
WHERE license NOT LIKE '%Commercial%') AS oss_count,
(SELECT COUNT(license)
FROM apps_components
WHERE license LIKE '%Commercial%') AS commercial_count,
(SELECT COUNT(license)
FROM apps_components) AS total
FROM apps_components
GROUP BY app_name, app_version;
而且,这确实计算了每个唯一行的出现,但是它不太正确,因为它重复了commmcmercial_count
列。因此,我认为如果其中一个App_names中不包含任何“商业”的出现,则应该是0。
SELECT DISTINCT app_name, app_version,
COUNT(CASE WHEN license NOT LIKE '%Commercial%' THEN 1 ELSE 0 END) as oss_count,
COUNT(CASE WHEN license LIKE '%Commercial%' THEN 1 ELSE 0 END) as commercial_count
FROM apps_components
GROUP BY app_name;
如果需要更多数据或示例,请告诉我,我可以更新此信息。感谢任何帮助!
Using MySQL on XAMPP. I have this table that I'm trying to produce this output with. Some of the rows do repeat so I need to return just the unique rows and the occurrences of the license in each.
line id | red_id | cmp_id | cmp_name | cmp_version | app_id | app_name | app_vers | license | status | requester | monitor_id | monitor_dig | issue_count |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 76074884 | 77960664 | Unicode for C Sharp (Unicode4C) | 67.9 | 77956767 | LTS JSON L | 9.9 | Unicode License V7 | Approved | CZ | 76654 | na | 0 |
2 | 76074884 | 77960664 | Unicode for C Sharp (Unicode4C) | 67.9 | 77956767 | LTS JSON L | 9.9 | Unicode License V7 | Approved | TV | 76654 | na | 0 |
204 | 79895001 | 87965001 | Common Component1 | 1.2 | 95655001 | Component1 | 1.0 | Commercial License | Approved | CZ | 5260 | na | 2 |
Example of desired output
app_name | app_version | oss_count | commercial_count | total |
---|---|---|---|---|
app_1_name | 1.0 | 45 | 5 | 50 |
app_2_name | 2.0 | 34 | 0 | 34 |
oss count is the count from column license of licenses that don't contain 'commercial' in it.
commercial count is the count from column licenses that contain 'commercial' in it.
I've tried the following queries which get me close to what I want.
This one just repeats the overall totals in each row.
SELECT DISTINCT app_name, app_version,
(SELECT COUNT(license)
FROM apps_components
WHERE license NOT LIKE '%Commercial%') AS oss_count,
(SELECT COUNT(license)
FROM apps_components
WHERE license LIKE '%Commercial%') AS commercial_count,
(SELECT COUNT(license)
FROM apps_components) AS total
FROM apps_components
GROUP BY app_name, app_version;
and this one does count the occurrences for each unique row but it's not quite right as it repeats for the commmcercial_count
column. So I think those should be 0 if one of the app_names do not contain any occurrences of 'commercial` in it.
SELECT DISTINCT app_name, app_version,
COUNT(CASE WHEN license NOT LIKE '%Commercial%' THEN 1 ELSE 0 END) as oss_count,
COUNT(CASE WHEN license LIKE '%Commercial%' THEN 1 ELSE 0 END) as commercial_count
FROM apps_components
GROUP BY app_name;
If more data is needed or examples please let me know and I can update this. Appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论