2

I'm completely new to SQL. I have a table with multiple rows for the same ID (Alpha_code).

I want to query this table to provide only 1 row for each Alpha_code.

I need it to first select the newest row if rows have been added on different (contact_date).

I have found the statement below does this no problem.

SELECT m.*
    FROM (SELECT Alpha_code, max(Contact_date) AS MaxDate FROM contactsNTH GROUP BY Alpha_code)  
    AS mm INNER JOIN contactsNTH AS m ON (mm.MaxDate = m.Contact_Date) AND (mm.Alpha_code = m.Alpha_code)

However there are other multiple rows that were entered on the same (contact_date). I'm not sure how to add more code to then only show 1 of the multiple rows remaining.

I've tried the Select Distinct statement but it didn't work.

Any assistance would be appreciated. If I can answer this myself in the meantime I'll post the answer

thanks, shaun

This thread is close to what I'm asking but I find the answer confusing.

How To Select Distinct Row Based On Multiple Fields 4

4

2 に答える 2

0

内部クエリを使用すると簡単に実行できます。SQL フィドルのデモ

SELECT Alpha_code, Contact_date AS MaxDate FROM contactsNTH AS t WHERE
Contact_date = (SELECT MAX(Contact_date) FROM contactsNTH
WHERE Alpha_code=t.Alpha_code)

単純にグループ化して最大日付を使用してクエリを作成しましたが、失敗しました。次に、これらの単語をグーグルで書きselect group by having max、結果を見つけました。これは、そのような問題を迅速に修正するためのサンプル ガイドです。

于 2013-10-17T07:08:22.297 に答える