作者oherman (qq)
看板Database
標題[SQL ] 請問rank的用法
時間Mon Oct 12 15:04:21 2015
資料庫名稱:sql server
資料庫版本:2014
內容/問題描述:
請問我有一個資料表名稱是table_schema
欄位及資料如下:
table_name field_name
==================================
customer id
customer name
employee ide
employee name
我想用rank或row_number產生如下資料:
sequnence table_name field_name
=====================================
01 customer id
02 customer name
01 employee id
02 employee name
squnence的產生由rank語法產生,請問sql語法應該怎麼下?
重點就是不同的table_name,sequence必須重新計數,
試過over by (table_name,field_name)
結果都不是我想要的,用group by更不行,請問各位先進有沒有合適的語法?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.216.68
※ 文章網址: https://www.ptt.cc/bbs/Database/M.1444633463.A.D9A.html
※ 編輯: oherman (118.163.216.68), 10/12/2015 15:05:27
※ 編輯: oherman (118.163.216.68), 10/12/2015 15:07:27
→ ishewood: SELECT table_name, field_name, 10/12 15:48
→ ishewood: ROW_NUMBER() OVER( 10/12 15:49
→ ishewood: PARTITION BY table_name ORDER BY field_name 10/12 15:50
→ ishewood: ) AS sequence 10/12 15:50
→ ishewood: FROM table_schema 10/12 15:50
→ ishewood: ORDER BY table_name, field_name; 10/12 15:51
→ ishewood: 看是不是你要的 10/12 15:51
→ oherman: 感謝,正是我要的^^ 10/12 16:21