この SQL 選択を LINQ に変換する方法を見つけようとしていますが、まだ理解できていません。
Id(PersonId) ごと、テストごと、月ごと、年ごとの最後のテスト レコードを取得しています。基本的には、1年ごとに各人の月の最終スコアを取得します。
CREATE TABLE #MyTable
(
Id INT,
Score INT,
TestName VARCHAR(50),
TestDate DATETIME
)
INSERT INTO #MyTable
VALUES
(1, 10, 'Math', '2011-12-16 00:00:00.000')
,(1, 25, 'Math', '2011-12-26 00:00:00.000')
,(1, 100, 'Math', '2011-12-06 00:00:00.000')
,(1, 10, 'Reading', '2011-12-16 00:00:00.000')
,(1, 25, 'Reading', '2011-12-26 00:00:00.000')
,(1, 100, 'Reading', '2011-12-06 00:00:00.000')
,(2, 10, 'Math', '2011-12-16 00:00:00.000')
,(2, 25, 'Math', '2011-12-26 00:00:00.000')
,(2, 100, 'Math', '2011-12-06 00:00:00.000')
,(2, 10, 'Reading', '2011-12-16 00:00:00.000')
,(2, 25, 'Reading', '2011-12-26 00:00:00.000')
,(2, 100, 'Reading', '2011-12-06 00:00:00.000')
,(1, 10, 'Math', '2011-12-16 00:00:00.000')
,(1, 25, 'Math', '2012-12-26 00:00:00.000')
,(1, 100, 'Math', '2012-12-06 00:00:00.000')
,(1, 10, 'Reading', '2012-12-16 00:00:00.000')
,(1, 25, 'Reading', '2012-12-26 00:00:00.000')
,(1, 100, 'Reading', '2012-12-06 00:00:00.000')
,(2, 10, 'Math', '2012-12-16 00:00:00.000')
,(2, 25, 'Math', '2012-12-26 00:00:00.000')
,(2, 100, 'Math', '2012-12-06 00:00:00.000')
,(2, 10, 'Reading', '2012-12-16 00:00:00.000')
,(2, 25, 'Reading', '2012-12-26 00:00:00.000')
,(2, 100, 'Reading', '2012-12-06 00:00:00.000')
SELECT DISTINCT
M.Id,M.Score,M.TestName, M.TestDate
FROM
#MyTable M
WHERE
M.TestDate IN (
SELECT MAX(m.TestDate)
FROM #MyTable m
GROUP BY MONTH(TestDate), YEAR(TestDate)
)
DROP TABLE
#MyTable
SubQuery を使用した後の FINAL RESULT では、結果 8 件のレコードが返されました。
私が持っているもの:
from a in MyTable
where a.TestDate == from b in MyTable
group b.TestDate.Value.Month,
a.TestDate.Value.Year