ここにはいくつかのオプションがあります。利用可能なインデックスに応じて、他のインデックスより優れたパフォーマンスを発揮できるため、3つすべてが貴社の環境でテストする価値があります。
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
EmployeeID INT NOT NULL,
Category VARCHAR(20) NOT NULL,
Amount MONEY NOT NULL
);
INSERT #TestData (EmployeeID, Category, Amount) VALUES
(11111, 'Vacation ', 4),
(11111, 'Personal ', 2),
(11111, 'Holiday ', 3),
(22222, 'Vacation ', 1),
(22222, 'Personal ', 3),
(22222, 'Holiday ', 2),
(33333, 'Personal ', 5),
(33333, 'Holiday ', 1),
(33333, 'Vacation ', 3),
(33333, 'Unspecified', 3);
-- add a covering index to improve performance of the various options
CREATE NONCLUSTERED INDEX ix_TestData ON #TestData (EmployeeID, Amount DESC) INCLUDE (Category);
------------------------------------------
IF OBJECT_ID('tempdb..#Employee', 'U') IS NOT NULL
DROP TABLE #Employee;
CREATE TABLE #Employee (
EmployeeID INT NOT NULL
);
INSERT #Employee (EmployeeID) VALUES
(11111), (22222), (33333);
-- SELECT * FROM #TestData td;
--============================================================
--============================================================
SELECT TOP 1 WITH TIES
td.EmployeeID, td.Category, td.Amount
FROM
#TestData td
ORDER BY
ROW_NUMBER() OVER (PARTITION BY td.EmployeeID ORDER BY td.Amount DESC);
--============================================================
SELECT
tdt.EmployeeID, tdt.Category, tdt.Amount
FROM
#Employee e
CROSS APPLY (
SELECT TOP 1
td.EmployeeID, td.Category, td.Amount
FROM
#TestData td
WHERE
e.EmployeeID = td.EmployeeID
ORDER BY
td.Amount DESC
) tdt;
--============================================================
WITH
cte_AddRN AS (
SELECT
td.EmployeeID, td.Category, td.Amount,
RN = ROW_NUMBER() OVER (PARTITION BY td.EmployeeID ORDER BY td.Amount DESC)
FROM
#TestData td
)
SELECT
ar.EmployeeID, ar.Category, ar.Amount
FROM
cte_AddRN ar
WHERE
ar.RN = 1;
すべて3はそうあなたがカテゴリごとに右、従業員一人当たりの行をしたい
EmployeeID Category Amount
----------- -------------------- ---------------------
11111 Vacation 4.00
22222 Personal 3.00
33333 Personal 5.00
EmployeeID Category Amount
----------- -------------------- ---------------------
11111 Vacation 4.00
22222 Personal 3.00
33333 Personal 5.00
EmployeeID Category Amount
----------- -------------------- ---------------------
11111 Vacation 4.00
22222 Personal 3.00
33333 Personal 5.00
...同じ結果を生みますか?あなたの結果に「Personal」カテゴリの「11111」は含まれていないので、私は尋ねます – Jaya
なぜあなたはあまり答えを受け入れないのですか? – scsimon
Jaya、最高額の従業員1つのカテゴリ –