SQLZOO 정답: 9 - Windows 기능

7748 단어 SQLZOO
  • Show the lastName, party and votes for the constituency ‘S14000024’ in 2017.
  • SELECT lastName, party, votes
      FROM ge
     WHERE constituency = 'S14000024' AND yr = 2017
    ORDER BY votes DESC
    
  • Show the party and RANK for constituency S14000024 in 2017. List the output by party.
  • SELECT party, votes,
           RANK() OVER (ORDER BY votes DESC) as posn
      FROM ge
     WHERE constituency = 'S14000024' AND yr = 2017
    ORDER BY party
    
  • Use PARTITION to show the ranking of each party in S14000021 in each year. Include yr, party, votes and ranking (the party with the most votes is 1).
  • SELECT yr,party, votes,
          RANK() OVER (PARTITION BY yr ORDER BY votes DESC) as posn
      FROM ge
     WHERE constituency = 'S14000021'
    ORDER BY party,yr
    
  • Use PARTITION BY constituency to show the ranking of each party in Edinburgh in 2017. Order your results so the winners are shown first, then ordered by constituency.
  • SELECT constituency,party, votes,
    RANK() OVER (PARTITION BY constituency ORDER BY votes DESC) as posn
      FROM ge
     WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
       AND yr  = 2017
    ORDER BY posn,constituency
    
  • Show the parties that won for each Edinburgh constituency in 2017.
  • SELECT constituency,party FROM
    (SELECT constituency,party, votes,
    RANK() OVER (PARTITION BY constituency ORDER BY votes DESC) as posn
      FROM ge
     WHERE constituency BETWEEN 'S14000021' AND 'S14000026'
       AND yr  = 2017) RK
    WHERE RK.posn=1
    
  • Show how many seats for each party in Scotland in 2017.
  • SELECT party,COUNT(*) FROM (
    SELECT constituency,party, votes,
    RANK() OVER (PARTITION BY constituency ORDER BY votes DESC) as posn
      FROM ge
     WHERE constituency LIKE 'S%'
       AND yr  = 2017) rk
    WHERE rk.posn=1
    GROUP BY rk.party
    

    최근에 sql를 쓰고 있기 때문에 인터넷에서 답을 찾지 못하고 이 글을 썼습니다. 그 중에서 sql가 가장 좋은 것은 아닐 수도 있지만 결과는 문제없습니다.이것은 CSDN을 사용한 이래 첫 번째 CSDN 문장입니다. 여러분의 지적을 환영합니다.만약 여러분께 더 좋은 ql가 있다면, 아낌없이 메시지를 남겨 주십시오.

    좋은 웹페이지 즐겨찾기