简体   繁体   中英

SQL Query to calculate percentage based on sums from several tables

I'm trying to calculate a Percentage of gaming sessions per console platform. I 'm calculating usage based on data from two different sesssions-tables (one that just has a summary and one where I sum up per console). That works, and I can sum the total nr or sessions into SessionTotal in T3. So this code almost works, just can't seem to get the outer SELECT to validate. Could someone help me out?

SELECT T3.Sessions, T3.Console, T3.Sessions * 100 /  T2.SessionTotal AS Percentage
FROM
(
    -- This call returns a total nr of sessions played
    SELECT        SUM(Sessions) as SessionTotal 
    FROM

    (SELECT        Sessions, Console, SystemID
    FROM            (SELECT        TOP (100) PERCENT ISNULL(MIN(ps.SessionCount), 0) + ISNULL(COUNT(s.system_id), 0) AS Sessions, MIN(c.name) AS Console, MIN(c.id) AS SystemID
                      FROM            dbo.[Systems] AS c LEFT OUTER JOIN
                                                dbo.Sessions AS s ON c.id = s.system_id LEFT OUTER JOIN
                                                dbo.PreviousSessions AS ps ON c.id = ps.SystemID
                      GROUP BY c.id) AS T1) as T2,                                                
    -- This call returns nr of sessions per system
    SELECT        Sessions, Console, SystemID
    FROM            (SELECT        TOP (100) PERCENT ISNULL(MIN(ps.SessionCount), 0) + ISNULL(COUNT(s.system_id), 0) AS Sessions, MIN(c.name) AS Console, MIN(c.id) AS SystemID
                      FROM            dbo.[Systems] AS c LEFT OUTER JOIN
                                                dbo.Sessions AS s ON c.id = s.system_id LEFT OUTER JOIN
                                                dbo.PreviousSessions AS ps ON c.id = ps.SystemID
                      GROUP BY c.id) AS T3
)
declare @SessionTotal float = 

(

    SELECT        SUM(Sessions) as SessionTotal 
    FROM

    (SELECT        Sessions, Console, SystemID
    FROM            (
                     SELECT TOP (100) PERCENT ISNULL(MIN(ps.SessionCount), 0) + ISNULL(COUNT(s.system_id), 0) AS Sessions, MIN(c.name) AS Console, MIN(c.id) AS SystemID
                      FROM  dbo.[Systems] AS c 
                      LEFT OUTER JOIN dbo.Sessions AS s 
                      ON c.id = s.system_id 
                      LEFT OUTER JOIN dbo.PreviousSessions AS ps 
                      ON c.id = ps.SystemID
                      GROUP BY c.id) AS T1
                      ) as T2 

)   

    SELECT        Sessions, Console,Sessions * 100/@SessionTotal AS Percentage
    FROM            (

                      SELECT TOP (100) PERCENT ISNULL(MIN(ps.SessionCount), 0) + ISNULL(COUNT(s.system_id), 0) AS Sessions, MIN(c.name) AS Console, MIN(c.id) AS SystemID
                      FROM  dbo.[Systems] AS c 
                      LEFT OUTER JOIN dbo.Sessions AS s 
                      ON c.id = s.system_id 
                      LEFT OUTER JOIN dbo.PreviousSessions AS ps 
                      ON c.id = ps.SystemID
                      GROUP BY c.id
                     ) AS T3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM