我如何计算SQL中唯一行的独特出现?

发布于 2025-02-10 04:55:26 字数 4479 浏览 2 评论 0原文

在XAMPP上使用MySQL。我有这个我正在尝试使用此输出的表。有些行确实重复,因此我需要仅返回唯一的行和每个许可证的出现。

行IDred_idcmp_idcmp_namecmp_versionapp_idapp_nameapp_verslicenslicensclide eleaster_idmonitor_digeasuse_count_count_count1
77960664unicodejsonsharp(unicode4c)ltsc777 vlight67.977960664SHARP77956767UNICODE4C67.9
cformonitor_idUNICODE77956767LTS JSON L9.9Unicode License V7ApprovedTV76654na0
2047989500187965001Common Component11.295655001Component11.0Commercial LicenseApprovedCZ5260na2

Example of desired output

app_nameapp_versionoss_countcommercial_counttotal
app_1_name1.045550
APP_2_NAME2.034034

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 idred_idcmp_idcmp_namecmp_versionapp_idapp_nameapp_verslicensestatusrequestermonitor_idmonitor_digissue_count
17607488477960664Unicode for C Sharp (Unicode4C)67.977956767LTS JSON L9.9Unicode License V7ApprovedCZ76654na0
27607488477960664Unicode for C Sharp (Unicode4C)67.977956767LTS JSON L9.9Unicode License V7ApprovedTV76654na0
2047989500187965001Common Component11.295655001Component11.0Commercial LicenseApprovedCZ5260na2

Example of desired output

app_nameapp_versionoss_countcommercial_counttotal
app_1_name1.045550
app_2_name2.034034

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文