请查看以下查询。 对于Eg:
Select (select count(loc_id) from TotalLocation_table) as TotalLocations,
(select count(*) from EngagedccLocTable where loc in ( select loc_id from location_table) as EngageddLocations
//calculate the percentage of locations by using EngagedLocations and TotalLocations.used above queries to calculate percentage.
(select count(*) from EngagedccLocTable where loc in ( select loc_id from location_table)/(select count(loc_id) from TotalLocation_table)*100 as percentage
from loc_table;
我想使用别名来计算百分比,而不是使用相同的查询。
喜欢(EngageddLocations/TotalLocations)*100 as percentage
。
如何使用此请告诉我。
答案 0 :(得分:1)
您可以尝试以下
Select (EngageddLocations / TotalLocations) * 100 as percentage
from
(
select count(tl.loc_id) as TotalLocations,
count(ec.loc) as EngageddLocations
from TotalLocation_table tl
JOIN EngagedccLocTable ec ON tl.loc_id = ec.loc
) X
答案 1 :(得分:0)
with EngageddLocations as (select count(*) as Engagedd
from EngagedccLocTable
where loc in ( select loc_id
from location_table))
, TotalLocations as (select count(loc_id) as total
from TotalLocation_table)
select EngageddLocations.Engagedd as EngageddLocations
, TotalLocations.Total as TotalLocations
, (EngageddLocations.Engagedd, TotalLocations.Total*100) as percentage
from EngageddLocations
, TotalLocations;