2017-04-15 104 views
1

我有一個包含3列id,訪問日期和約會日期的表。 我想編寫一個sql查詢來查找以下內容 對於每個id,有多少個條目在哪裏訪問日期與約會日期匹配。請注意,比較必須在每個ID的多行上進行。 例子:用於比較2列的SQL查詢

id Visit-date Appointment-date 
1  20-1-2016 20-2-2016 
1  20-2-2016 30-3-2016 
1  04-04-2016 05-05-2016 

非常感謝您的幫助。 普拉薩德

+0

你是什麼意思與「跨越多行」? –

+0

要更加清楚,對於ID中的每個約會日期,查詢必須報告是否存在任何匹配的日期。在上例中,匹配組合是約會日期:20-2-2016,訪問日期:20-2-2016 .. – prasad

回答

0

您可以使用左與聚集到一起獲得期望的結果:

select a.id, 
    count(b.appointment_date) as count 
from your_table a 
left join your_table b on a.id = b.id 
    and a.visit_date = b.appointment_date 
group by a.id; 
0

你可以指望通過記錄ID分組。

select id, count(*) num_records 
from  your_table 
where  visit_date = appointment_date 
group by id