- identity()
語法:IDENTITY (data_type [ , seed , increment ] ) AS column_name
- data_type:識別欄位資料類型
- seed:第一個資料列整數值,預設值為 1
- increment:後續資料列增加的整數值,預設值為 1
- column_name:欄位名稱
- 簡易範例:列出業務人員接單數量,並根據其數量排序
-- 使用 AdventureWorks2012
IF OBJECT_ID (N'ROWTable') IS NOT NULL
DROP TABLE ROWTable
SELECT
IDENTITY(int,1,1) AS ROWNO , -- 從 1 開始,一次增加 1 來當成順序
P.BusinessEntityID ,
P.LastName ,
P.FirstName ,
T.SalesCount
INTO ROWTable -- 產生 ROWTable
FROM [Person].[Person] AS P
JOIN
(
SELECT
SalesPersonID ,
COUNT(*) AS SalesCount
FROM [Sales].[SalesOrderHeader]
GROUP BY SalesPersonID
) AS T ON P.BusinessEntityID = T.SalesPersonID
ORDER BY SalesCount DESC -- 根據接單數量排序
-- 查詢產生順序結果 ROWTable
SELECT * FROM ROWTable
沒有留言:
張貼留言