Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PostgreSQLのSQLn本ノック

soudai sone
December 02, 2016

 PostgreSQLのSQLn本ノック

PGConf.Asiaでの登壇資料です

http://www.pgconf.asia/JP/

soudai sone

December 02, 2016
Tweet

More Decks by soudai sone

Other Decks in Technology

Transcript

  1. SELECT SELECT * FROM table_name WHERE id = 1 OR

    id = 2 JE͕ͱͷσʔλ͕ݕࡧ͞ΕΔ
  2. SELECT SELECT * FROM table_name WHERE id IN ( SELECT

    id FROM table_name) ) αϒΫΤϦͷݕࡧ݁ՌΛར༻ͯ͠8)&3&͢Δ
  3. SELECT SELECT * FROM table_name WHERE id IN ( SELECT

    unnest(ARRAY[1,2])) ) "33":͕ٖࣅςʔϒϧ
  4. SELECT SELECT * FROM table_name WHERE id IN ( SELECT

    unnest(ARRAY[1,2])) ) "33":͕ٖࣅςʔϒϧ JE͕ͱͷσʔλ͕ݕࡧ͞ΕΔ
  5. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ
  6. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ
  7. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ ·ͣݕࡧͷݩΛߜΓࠐΉ
  8. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ ݕࡧͨ݁͠ՌͰ*/۟ʹ౤͛Δ ͕͜͜ઌఔͷαϒΫΤϦ෦෼
  9. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ
  10. VTFS@JE OBNF BSFB@JE  TPOF   TPVEBJ  

    UBLFUPNP   TPVEBJ  BSFB@JE BSFB@OBNF  ޿ౡ  Ԭࢁ  ౦ژ  େࡕ ݕࡧ݁Ռ
  11. SELECT SELECT * FROM users WHERE area_id IN ( SELECT

    area_id FROM area WHERE name = '޿ౡ' )
  12. SELECT SELECT * FROM users WHERE area_id IN ( SELECT

    area_id FROM area WHERE name = '޿ౡ' )
  13. DELETE DELETE FROM users WHERE area_id IN ( SELECT area_id

    FROM area WHERE name = '޿ౡ' ) 4&-&$5ͷ݁Ռͱಉ͡ର৅Λ࡟আ͢Δ
  14. VTFS@JE OBNF CJSUIEBZ BHF  ૖େ    ኽָ

    r   ᗅָ r   ࡣָ r 
  15. VTFS@JE OBNF CJSUIEBZ BHF  ૖େ    ኽָ

    r   ᗅָ r   ࡣָ r  ஀ੜ೔͕དྷͨΒߋ৽͕ඞཁ
  16. UPDATE UPDATE users SET age = new_users.age FROM (SELECT id,

    EXTRACT(YEAR FROM age(now(), birthday)) AS age FROM users) AS new_users WHERE users.id = new_users.id
  17. UPDATE UPDATE users SET age = new_users.age FROM (SELECT id,

    EXTRACT(YEAR FROM age(now(), birthday)) AS age FROM users) AS new_users WHERE users.id = new_users.id
  18. UPDATE UPDATE users SET age = new_users.age FROM (SELECT id,

    EXTRACT(YEAR FROM age(now(), birthday)) AS age FROM users) AS new_users WHERE users.id = new_users.id ஀ੜ೔͔Βݱࡏͷ೥ྸΛܭࢉ
  19. UPDATE UPDATE users SET age = new_users.age FROM (SELECT id,

    EXTRACT(YEAR FROM age(now(), birthday)) AS age FROM users) AS new_users WHERE users.id = new_users.id
  20. UPDATE UPDATE users SET age = new_users.age FROM (SELECT id,

    EXTRACT(YEAR FROM age(now(), birthday)) AS age FROM users) AS new_users WHERE users.id = new_users.id ߋ৽ͷର৅Λ8)&3&ͰϚονϯά
  21. VTFS@JE BHF   VTFS@JE OBNF CJSUIEBZ BHF  ૖େ

       ኽָ r   ᗅָ r   ࡣָ r 
  22. VTFS@JE BHF   VTFS@JE OBNF CJSUIEBZ BHF  ૖େ

       ኽָ r   ᗅָ r   ࡣָ r  ஀ੜ೔͔Β೥ྸΛࢉग़
  23. VTFS@JE BHF   VTFS@JE OBNF CJSUIEBZ BHF  ૖େ

       ኽָ r   ᗅָ r   ࡣָ r 
  24. VTFS@JE BHF   VTFS@JE OBNF CJSUIEBZ BHF  ૖େ

       ኽָ r   ᗅָ r   ࡣָ r  ೥ྸͰ61%"5& ߋ৽ͷର৅Λ8)&3&ͰϚονϯά
  25. ച্೔࣌ ച্ֹۚ        

    ộ ộ ച্೔ ച্ֹۚ         ộ ộ
  26. ച্೔࣌ ച্ֹۚ        

    ộ ộ ച্೔ ച্ֹۚ         ộ ộ
  27. ച্೔࣌ ച্ֹۚ        

    ộ ộ ച্೔ ച্ֹۚ         ộ ộ
  28. ച্೔࣌ ച্ֹۚ        

    ộ ộ ച্೔ ച্ֹۚ         ộ ộ ೔෇͝ͱʹूܭ͢Δඞཁ͕͋Δ
  29. ച্೔࣌ ച্ֹۚ        

    ộ ộ ച্೔ ച্ֹۚ         ộ ộ ूܭ݁ՌΛผUBCMFʹ*/4&35
  30. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔
  31. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔
  32. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔ ɹച্MPHͷ݄ॳ͔Β݄຤ͷσʔλΛநग़
  33. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔ ೔࣌Λ೔෇ʹม׵
  34. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔ ೔෇ʹม׵ͨ͠ച্೔ͰάϧʔϓԽ
  35. INSERT INSERT INTO ച্_2016_12 SELECT to_char(ച্೔࣌,'YYYY/MM/DD') AS ച্೔ , sum(ച্ֹۚ)

    AS ച্ FROM ച্log WHERE ച্೔࣌ BETWEEN date_trunc('month',now()) AND date_trunc(‘month',now()) + interval '1 month' - interval '1 days' GROUP BY ച্೔ ूܭ݁ՌΛ*/4&35
  36. CASEͷجຊతͳߏจ SELECT users.name, CASE area.name WHEN ‘޿ౡ' THEN 'தࠃ஍ํ' WHEN

    ‘Ԭࢁ' THEN 'தࠃ஍ํ' WHEN ‘େࡕ' THEN 'ؔ੢஍ํ' ELSE 'ͦͷଞ' END AS area_group FROM users INNER JOIN area USING (area_id)
  37. CASEͷجຊతͳߏจ SELECT users.name, CASE WHEN area.name = '޿ౡ' OR area.name

    = ‘Ԭࢁ' THEN 'தࠃ஍ํ' WHEN area.name ='େࡕ' THEN 'ؔ੢஍ํ' ELSE 'ͦͷଞ' END AS area_group FROM users INNER JOIN area USING (area_id)
  38. CASEͷجຊతͳߏจ SELECT users.name, area.name, CASE area.name WHEN ‘޿ౡ' THEN 'தࠃ஍ํ'

    WHEN ‘Ԭࢁ' THEN 'தࠃ஍ํ' WHEN ‘େࡕ' THEN 'ؔ੢஍ํ' ELSE 'ͦͷଞ' END AS area_group FROM users INNER JOIN area USING (area_id) ORDER BY CASE area.name WHEN '޿ౡ' THEN 1 ELSE 2 END, area_group
  39. OBNF BSFB BSFB@HSPVQ TPVEBJ ޿ౡ தࠃ஍ํ TPOF ޿ౡ தࠃ஍ํ UBLFUPNP

    Ԭࢁ தࠃ஍ํ TPVEBJ Ԭࢁ தࠃ஍ํ IPHF ౦ژ ͦͷଞ GPP େࡕ ؔ੢஍ํ GVHB େࡕ ؔ੢஍ํ
  40. CASEͷجຊతͳߏจ SELECT users.name, area.name, CASE area.name WHEN ‘޿ౡ' THEN 'தࠃ஍ํ'

    WHEN ‘Ԭࢁ' THEN 'தࠃ஍ํ' WHEN ‘େࡕ' THEN 'ؔ੢஍ํ' ELSE 'ͦͷଞ' END AS area_group FROM users INNER JOIN area USING (area_id) ORDER BY CASE area.name WHEN 'Ԭࢁ' THEN 1 ELSE 2 END, area_group ԬࢁΛ্ʹ͍ͨ࣌͠
  41. OBNF BSFB BSFB@HSPVQ TPVEBJ Ԭࢁ தࠃ஍ํ TPOF Ԭࢁ தࠃ஍ํ UBLFUPNP

    ޿ౡ தࠃ஍ํ TPVEBJ ޿ౡ தࠃ஍ํ IPHF ౦ژ ͦͷଞ GPP େࡕ ؔ੢஍ํ GVHB େࡕ ؔ੢஍ํ
  42. CASEͷجຊతͳߏจ SELECT users.name, area.name, CASE area.name WHEN ‘޿ౡ' THEN 'தࠃ஍ํ'

    WHEN ‘Ԭࢁ' THEN 'தࠃ஍ํ' WHEN ‘େࡕ' THEN 'ؔ੢஍ํ' ELSE 'ͦͷଞ' END AS area_group FROM users INNER JOIN area USING (area_id) ORDER BY CASE area.name WHEN '޿ౡ' THEN 1 ELSE 2 END, area_group
  43. CASEͷجຊతͳߏจ $users = $this->db->users->get(); $this->db->begin(); foreach ($users as $user_id =>

    $user) { if ($user[“pref”] == ‘Ԭࢁ’) { $user[“pref”] = ‘େ౎ձ’ } elseif ($user[“pref”] == ‘޿ౡ’){ $user[“pref”] = ‘෱ࢁ’ } $this->db->update($user); } $this->db->commit();
  44. CASEͷجຊతͳߏจ $users = $this->db->users->get(); $this->db->begin(); foreach ($users as $user_id =>

    $user) { if ($user[“pref”] == ‘Ԭࢁ’) { $user[“pref”] = ‘େ౎ձ’ } elseif ($user[“pref”] == ‘޿ౡ’){ $user[“pref”] = ‘෱ࢁ’ } $this->db->update($user); } $this->db->commit(); ͦ΋ͦ΋8)&3&۟ͰQSFGࢦఆͯ͠ 61%"5&ͳΒճ
  45. CASEͷجຊతͳߏจ UPDATE users SET pref = CASE pref WHEN ‘Ԭࢁ’

    THEN ‘େ౎ձ’ WHEN ‘޿ౡ’ THEN ‘෱ࢁ’ ELSE pref END WHERE pref IN (‘Ԭࢁ’,’޿ౡ’)
  46. CASEͷجຊతͳߏจ JE OBNF BHF  TPOF   UBLFUPNP 

     TPVEBJ  VTFS@JE CJSUIEBZ QSFG   ޿ౡ   ෱ࢁ   େ౎ձ VTFST VTFS@QSPQFSUJFT 
  47. CASEͷجຊతͳߏจ UPDATE users AS u, (SELECT users.id AS id, CASE

    WHEN user_properties.birthday > ‘1996-07-30’ THEN ‘ෆৄ’ ELSE age END AS age FROM users INNER JOIN user_properties ON user.id = user_properties.user_id AND user_properties.pref IN (‘෱ࢁ’,’ͳ͝΍’,’େ౎ձ’)) AS tmp SET u.age = tmp.age WHERE u.id = tmp.id
  48. CASEͷجຊతͳߏจ JE OBNF BHF  TPOF   UBLFUPNP ෆৄ

     TPVEBJ  VTFS@JE CJSUIEBZ QSFG   ޿ౡ   ෱ࢁ   େ౎ձ
  49. ঎඼໊ ച্ ച্೔ ϓϩάϥϚͷͨΊͷ42-   ಺෦ߏ଄͔ΒֶͿ   ιϑτ΢ΣΞσβΠϯ

      42-࣮ફೖ໳   ཧ࿦͔ΒֶͿσʔλϕʔε   42-ٯҾ͖େશͷۃҙ   ϓϩάϥϚͷͨΊͷ42-   42-࣮ફೖ໳   ཧ࿦͔ΒֶͿσʔλϕʔε   42-࣮ફೖ໳   Ϧʔμϒϧίʔυ   Ϧʔμϒϧίʔυ   σʔλϕʔεɾϦϑΝΫλ   ୡਓʹֶͿ%#ઃܭ   ɿ ɿ ɿ ɿ ɿ ɿ ˞࣮຿Ͱ͸ฤ͸ਖ਼نԽ͢Δ΂͖
  50. ࣮ࡍͷSQL SELECT to_char( sum(“ച্"),'FM999,999,999' ) AS "೔ผച্" , "ച্೔" FROM

    "ച্ද" GROUP BY “ച্೔" ORDER BY "ച্೔" ܻ۠੾ΓͰΧϯϚΛೖΕΔ
  51. ೔ผച্ ച্೔        

                                                    ɿ ɿ ɿ ɿ
  52. ࣮ࡍͷSQL SELECT * , lag("೔ผച্") OVER (ORDER BY "ച্೔")AS લ೔ച্

    FROM (SELECT to_char(sum("ച্"),'FM999,999,999') AS "೔ผച্" , "ച্೔" FROM "ച্ද" GROUP BY "ച্೔") AS "ूܭ"
  53. ࣮ࡍͷSQL SELECT * , lag("೔ผച্") OVER (ORDER BY "ച্೔")AS લ೔ച্

    FROM (SELECT to_char(sum("ച্"),'FM999,999,999') AS "೔ผച্" , "ച্೔" FROM "ച্ද" GROUP BY "ച্೔") AS "ूܭ"
  54. ࣮ࡍͷSQL SELECT * , lag("೔ผച্") OVER (ORDER BY "ച্೔")AS લ೔ച্

    FROM (SELECT to_char(sum("ച্"),'FM999,999,999') AS "೔ผച্" , "ച্೔" FROM "ച্ද" GROUP BY "ച্೔") AS "ूܭ" ઌ΄ͲͷΫΤϦΛαϒΫΤϦʹ͢Δ
  55. ࣮ࡍͷSQL SELECT * , lag("೔ผച্") OVER (ORDER BY "ച্೔")AS લ೔ച্

    FROM (SELECT to_char(sum("ച্"),'FM999,999,999') AS "೔ผച্" , "ച্೔" FROM "ച্ද" GROUP BY "ച্೔") AS "ूܭ" 8JOEPXؔ਺ͷର৅Λࢦఆ ઌ΄ͲͷΫΤϦΛαϒΫΤϦʹ͢Δ
  56. ࣮ࡍͷSQL SELECT * , lag("೔ผച্") OVER (ORDER BY "ച্೔")AS લ೔ച্

    FROM (SELECT to_char(sum("ച্"),'FM999,999,999') AS "೔ผച্" , "ച্೔" FROM "ച্ද" GROUP BY "ച্೔") AS "ूܭ" 8JOEPXؔ਺ͷର৅Λࢦఆ ઌ΄ͲͷΫΤϦΛαϒΫΤϦʹ͢Δ લߦͷऔಘ͢ΔΧϥϜΛࢦఆ
  57. ೔ผച্ ച্೔ લ೔ച্     /6--  

                                                                                             ɿ ɿ ɿ ɿ ɿ ɿ
  58. ೔ผച্ ച্೔ લ೔ച্     /6--  

                                                                                             ɿ ɿ ɿ ɿ ɿ ɿ લߦ͕ແ͍ͷͰ/6--
  59. ؔ਺ આ໌ SPX@OVNCFS ߦ൪߸ SBOL ϥϯΩϯά ಉ཰Ͱ൪߸Λඈ͹͢ EFOTF@SBOL ϥϯΩϯά ಉ཰Ͱ൪߸Λඈ͹͞ͳ͍

    QFSDFOU@SBOL ϥϯΩϯά Ͱදࣔ  SBOL  શߦ਺ DVNF@EJTU QFSDFOU@SBOLʹྨࣅ ݱࡏͷߦͷҐஔ  શߦ਺ OUJMF / ϥϯΩϯά /ʹ෼ׂ MBH WBMVF P⒎TFU EFGBVMU ιʔτঢ়ଶͰͷલͷߦͷ஋ MFBE WBMVF P⒎TFU EFGBVMU ιʔτঢ়ଶͰͷޙͷߦͷ஋ pSTU@WBMVF WBMVF ࠷ॳͷ஋ MBTU@WBMVF WBMVF ࠷ޙͷ஋ OUI@WBMVF WBMVF / /൪໨ͷ஋ ͔Β਺͑Δ
  60. λεΫ໊ ৄࡉ ରԠظؒ Πϯετʔϧ \ॴଐΞγετ ߨࢣتా^ <  جຊฤ \ॴଐΞγετ

    ߨࢣتా^ <  42-Oຊ \ॴଐΦϛΧϨ ߨࢣીࠜ^ <  σʔλ෼ੳ \ॴଐΈ͔͔ ߨࢣେࢁ^ < 
  61. λεΫ໊ ৄࡉ ରԠظؒ Πϯετʔϧ \ॴଐΞγετ ߨࢣتా^ <  جຊฤ \ॴଐΞγετ

    ߨࢣتా^ <  42-Oຊ \ॴଐΦϛΧϨ ߨࢣીࠜ^ <  σʔλ෼ੳ \ॴଐΈ͔͔ ߨࢣେࢁ^ < 
  62. λεΫ໊ ৄࡉ ରԠظؒ Πϯετʔϧ \ॴଐΞγετ ߨࢣتా^ <  جຊฤ \ॴଐΞγετ

    ߨࢣتా^ <  42-Oຊ \ॴଐΦϛΧϨ ߨࢣીࠜ^ <  σʔλ෼ੳ \ॴଐΈ͔͔ ߨࢣେࢁ^ < 
  63. λεΫ໊ ৄࡉ ରԠظؒ Πϯετʔϧ \ॴଐΞγετ ߨࢣتా^ <  جຊฤ \ॴଐΞγετ

    ߨࢣتా^ <  42-Oຊ \ॴଐΦϛΧϨ ߨࢣીࠜ^ <  σʔλ෼ੳ \ॴଐΈ͔͔ ߨࢣେࢁ^ <