次のクエリから max(id) を持つ max(effdt) のみの sum(comprate) を取得しようとしています。
SELECT id, employee_code, effdt, comprate
FROM dbo.employees_compensation
WHERE (employee_code = '000321514')
ORDER BY effdt desc, id desc
上記のクエリの結果は次のとおりです。
id employee_code effdt comprate
1473433 000321514 2010-09-02 00:00:00.000 46000
1473432 000321514 2010-09-02 00:00:00.000 138000
1453627 000321514 2010-09-02 00:00:00.000 46000
1453626 000321514 2010-09-02 00:00:00.000 138000
1447165 000321514 2010-09-02 00:00:00.000 46000
1447164 000321514 2010-09-02 00:00:00.000 138000
1424948 000321514 2010-09-02 00:00:00.000 46000
1424947 000321514 2010-09-02 00:00:00.000 138000
1405789 000321514 2010-09-02 00:00:00.000 46000
1405788 000321514 2010-09-02 00:00:00.000 138000
多くのクエリを試しましたが、期待した結果が得られません。私が欲しいのは、上記の結果の最初の2つの値です。つまり、
id employee_code effdt comprate
1473433 000321514 2010-09-02 00:00:00.000 46000
1473432 000321514 2010-09-02 00:00:00.000 138000
次に、上記の結果から sum(comprate) を取得します。つまり、
sum(comprate)
184000
期待される結果を得るために私を助けてもらえますか? 前もって感謝します。