我有两张表 App 和商店
(Invidual NonGrouped(%) / total NonGrouped(%))*100
。 ABC的例子是(75/ 165)*100 = 45.45
,类似于XYZ,它是(50/ 165)*100 = 30.30
。此处165
是75+50+40
我的查询如下:
SELECT
AppName,
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END ) AS Grouped,
SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) AS NonGrouped,
ROUND(((SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) AS GroupedPercentage,
ROUND(((SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) AS NonGroupedPercentage
FROM
Apps
INNER JOIN Store
ON (Apps.Store = Store.Store)
AND AppName !='' GROUP BY AppName
我能够找到除AverageNonGrouped(%)以外的列,因为我不知道如何获得总NonGrouped(%),有谁可以帮我这个
答案 0 :(得分:1)
这是一个很大的查询,但它完成了工作(我已经在一个数据集上进行了测试)
SELECT
AppName,
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END ) AS Grouped,
SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) AS NonGrouped,
ROUND(((SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) AS GroupedPercentage,
ROUND(((SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) AS NonGroupedPercentage,
ROUND(((SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) /
(SELECT SUM(NonGroupedPercentage)
FROM (
SELECT
ROUND(((SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) /
(SUM( CASE HasGroup WHEN 0 THEN 1 ELSE 0 END ) +
SUM( CASE HasGroup WHEN 1 THEN 1 ELSE 0 END )))*100), 2) AS NonGroupedPercentage
FROM
apps
INNER JOIN store
ON (apps.Store = store.Store)
AND AppName !=''
GROUP BY AppName
) AS total) *100 AS AverageNonGroupedPercentage
FROM
apps
INNER JOIN store
ON (apps.Store = store.Store)
AND AppName !=''
GROUP BY AppName