开发者

SQL sort based on sum of various columns

I have a MS-SQL table of game results which contains a home team, home team score, away team, and away team score. I am attempting to query the teams, wins, and losses ordered by first, total wins(descending) and then by total losses(ascending). The table does not contain a wins or losses c开发者_运维技巧olumn. Wins and losses must be calculated based on the score. If I need to create a view or temporary table, how do I create that view with teams, wins, and losses?


SELECT *
FROM MyTable
WHERE ...
ORDER BY SUM(win) DESC, SUM(loss) ASC

For a more detailed answer, provide your schema and sample data.


Assuming you can pull back that information in your query, you can use aggregates in ORDER BY clauses:

ORDER BY
    SUM(win) DESC
   ,SUM(loss) ASC

edit: update for comment

With only the scores being stored, you'll want to either use a temporary table or a CTE (depends on the DBMS you are using if the support them) to first create a table that has the teams and wins and losses in them, and then you can SELECT from that and ORDER accordingly.


Homework?

Anyway, you can specify the sort order in a SQL statement by following the column with the order. For example,

select x, y, z from table order by x asc, y desc, z asc


select 
    teamName, 
    'wins' = 
        case 
            when wins is null then 0 
            else wins 
        end, 
    'losses' = 
        case 
            when losses is null then 0 
            else losses 
        end 
from 
    teams 
left join 
( 
    select 
        a.wins as wins, 
        b.losses as losses, 
        'team' = 
            case 
                when a.team is null then b.team 
                else a.team 
            end 
    from 
    ( 
        select 
            homeTeam as team, 
            sum(wins) as wins 
        from 
        ( 
            select 
                homeTeam, 
                count(*) as wins 
            from 
                results 
            where 
                homeScore > awayScore 
            group by 
                homeTeam 
            union all 
            select 
                awayTeam, 
                count(*) as wins 
            from 
                results 
            where 
                homeScore < awayScore 
            group by 
                awayTeam         
        ) a 
        group by homeTeam 
    ) a 
    full outer join 
    ( 
        select 
            homeTeam as team, 
            sum(losses) as losses 
        from 
        ( 
            select 
                homeTeam, 
                count(*) as losses 
            from 
                results 
            where 
                homeScore < awayScore 
            group by 
                homeTeam 
            union all 
            select 
                awayTeam, 
                count(*) as losses 
            from 
                results 
            where 
                homeScore > awayScore 
            group by 
                awayTeam 
        ) a 
        group by 
            homeTeam 
    ) b 
    on a.team = b.team 
) c 
on teams.id = c.team
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜