English 中文(简体)
根据各栏的总和分类
原标题:SQL sort based on sum of various columns

我的游戏成果表有MS-SQL,其中包含一个家庭团队、家庭团队的分数、小组的分数以及团队分数。 我试图问小组,首先打赢,损失,总得赢,然后是总损失(损失)。 表格不含,包括一栏或一栏损失。 必须根据分数计算赢利和损失。 如果我需要提出一个观点或临时表格,我如何与团队、赢利和损失形成这种看法?

最佳回答
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
问题回答
SELECT *
FROM MyTable
WHERE ...
ORDER BY SUM(win) DESC, SUM(loss) ASC

更详细的答复是提供你的图表和样本数据。

假设你能够把这些信息撤回到你的询问中,你可以使用<代码>中的合计数。 页: 1 条款:

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

<>strong>edit/strong>: 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.

家庭工作

不管怎么说,你可以通过按顺序排出一栏来具体注明发言顺序。 例如,

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签