zoukankan      html  css  js  c++  java
  • 【转】成绩单、业绩表SQL(一个纵表变横表 一个用开窗函数)

    原文地址:http://blog.csdn.net/shenghuiping2001/article/details/4593914

    原始表: 

    name            course              score 
    ----------------------------------------- 
    张三            语文                80 
    张三            数学                75 
    张三            外语                60 
    李四            语文                90 
    李四            数学                85 
    李四            外语                70 
    王五            语文                95 
    王五            数学                80 
    王五            外语                75 


    结果表: 

    姓名        语文        数学        外语 
    ----------------------------------------- 
    张三        80          75          60 
    李四        90          85          70 
    王五        95          80          75 

    ========================================= 

    原始表: 

    city          volume 
    --------------------- 
    北京          16000 
    东莞          5000 
    上海          200000 
    广州          8500 
    湛江          100000 

    结果表: 

    城市          排名 
    --------------------- 
    北京          3 
    东莞          5 
    上海          1 
    广州          4 
    湛江          2

    SQL code 1:

    select name, sum(case when course = "语文" then score else 0 end) "语文", sum(case whencourse = "数学" then score else 0 end) "数学", sum(case when course = "外语" then scoreelse 0 end) "外语" from table1 group by name; select city, row_number() over(order byvolume desc) "排名" from table2;
    SQL code 2:
    SELECT name,SUM(DECODE(course,'语文',score,0)),SUM(DECODE(course,'数学',score,0)),SUM(DECODE(course,'外语',score,0)) FROM tb1 GROUP BY name; SELECT city "城市",rownum "排名" FROM tb2 ORDER BY volume DESC
    SQL code 3:
    select name,max(case when course='语文' then score end) "语文", max(case when course='数学' thenscore end) "数学", max(case when course='外语' then score end) "外语" from table_name group byname;
     
    select city "城市",row_number() over(order by volume descas "排名" from ww0924 order by rowid;
  • 相关阅读:
    姐姐的vue(1)
    LeetCode 64. Minimum Path Sum 20170515
    LeetCode 56. 56. Merge Intervals 20170508
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 24. Swap Nodes in Pairs 20170424
    LeetCode 19. Remove Nth Node From End of List 20170417
    LeetCode No.9 Palindrome Number 20170410
    LeetCode No.8. String to Integer (atoi) 2017/4/10(补上一周)
    LeetCode No.7 Reverse Integer 2017/3/27
    LeetCode No.4 Median of Two Sorted Arrays 20170319
  • 原文地址:https://www.cnblogs.com/mezero/p/2278403.html
Copyright © 2011-2022 走看看