看板 Database 關於我們 聯絡資訊
沒SQL Server, ... 用 PostgreSQL select id,year,unit from ( select id , year , unit , unit<>lead(unit)over(x) or unit<>lag(unit)over(x) z from tableX window x as (partition by id order by year desc) )Q where z ※ 引述《Schematic (小小寶的媽)》之銘言: : 資料庫名稱:SQL SERVER : 資料庫版本:2012 : 內容/問題描述: : 列出某位員工在哪一年換了單位,沒有換單位的員工不用列出 : Num id year unit : -------------------------- : 1 1 99 A : 2 1 98 B : 3 1 97 B : 4 1 96 B : 1 2 99 B : 2 2 98 B : 3 2 97 B : 1 3 97 C : 2 3 96 C : 3 3 95 C : 4 3 94 B : 1 4 97 A : 2 4 96 B : 3 4 95 B : 預期結果,員工1在99年從B單位換到A單位 : id year unit : ----------------------- : 1 99 A : 1 98 B : 3 95 C : 3 94 B : 4 97 A : 4 96 B : 謝謝 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.172.92.7 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1463153703.A.90F.html ※ 編輯: standia (1.172.92.7), 05/13/2016 23:35:56