テーブル内の 1 つのフィールド (queryresolutiondate) の最大値を含むレコードを選択する必要があり、そのサブセット内で、別のフィールド (query Establishdate) の最小値を含むレコードのみを選択する必要があります。単一のサブ選択で最小値と最大値を選択しようとしましたが、残念ながら、絶対的な最小値と最大値が常に同じレコードで発生するとは限らないため、レコードが返されません。
次のクエリは機能しますが、このコードは、約 45 列を返す非常に大きなクエリの一部であり、44 列をグループ化しないことをお勧めします。
select
q.[SourceCustomerId],
q.[SourceProductCode],
q.[SourceProductIssueNum],
min (q.[QueryEstablishDate]),
q.[SourceQueryCode],
q.[SourceQueryStatus],
q.[QueryResolutionDate]
from [dbo]..[dQueryAll] q with (nolock)
inner join (select [SourceCustomerId], [SourceProductCode], [SourceProductIssueNum], [SourceQueryCode],
max([QueryResolutionDate]) as maxQueryResolutionDate
from [dbo]..[dQueryAll] with (nolock)
where [SourceQueryCode] = 311
group by [sourceCustomerId], [SourceProductCode], [SourceProductIssueNum], [SourceQueryCode]) qg
on (qg.[SourceCustomerId] = q.[SourceCustomerId] and qg.[SourceProductCode] = q.[SourceProductCode]
and qg.[SourceProductIssueNum] = q.[SourceProductIssueNum]
and qg.maxQueryResolutionDate = q.[QueryResolutionDate])
group by
q.[SourceCustomerId],
q.[SourceProductCode],
q.[SourceProductIssueNum],
q.[SourceQueryCode],
q.[SourceQueryStatus],
q.[QueryResolutionDate]
上記のサブセレクト内に別のサブセレクトを作成して、最大解決日を含むrocrdsから最小確立日を選択できるかどうか疑問に思いました。これが可能であれば、私はいくつかの助けが欲しい
Excel に貼り付けたテーブルのデータの例がありますが、ここに読み込む方法が見つかりません。