SQL分組排序

2021-05-22 17:14:13 字數 1626 閱讀 4753

不得不承認,我腦子梗塞的很嚴重。。。

表[gcc_bloginfo]

blogid        uid          blogtitle        createtime            issketch          checkright

1                1            "hello"           2010-04-01          2                    1

2                1             "hello2"        2010-04-02          2                    1

3                1             "hello3"        2010-04-03          2                    2

4                2              "hello4"       2010-04-01          2                    1

5                2              "hello5"       2010-04-05          2                    1

6                3              "hello6"       2010-04-01          1                    1

7                3              "hello7"       2010-04-02          2                    1

最後查詢結果為:

blogid        uid          blogtitle        createtime            issketch          checkright

2                1             "hello2"        2010-04-02          2                    1

5                2              "hello5"       2010-04-05          2                    1

7                3              "hello7"       2010-04-02          2                    1

分組:[a]表                                       [b]表

要求輸出每個學科前2名學生資訊,分數

select b1.subject, b1.score, a.* from b b1 left join a on a.id = b1.id

where b1.id in(

select id from b where score in

(select distinct top 2 score from b where subject=b1.subject order by score desc))

order by b1.subject,b1.score desc

sql實現分組排序

今天在公司遇到了乙個要分組排序的功能不知道怎麼實現,所以下班了想了下,大家分享了 if object id tempdb.temptbl is not null drop table temptbl create table temptbl store id varchar 4 kind char,...

sql的分組排序

group by 與 order by 的使用 之前使用的時候,網上查了下 都說group by 和order by 後面的引數必須是select 中的引數,後來自己試了下,只需要是from 表中的字段都行 select id title,score,creator name name,create...

sql的分組排序

group by 與 order by 的使用 之前使用的時候,網上查了下 都說group by 和order by 後面的引數必須是select 中的引數,後來自己試了下,只需要是from 表中的字段都行 select id title,score,creator name name,create...