我有两张桌子:
表1 :activity history
activity id activity date
1 10/12/13
1 12/12/13
1 23/1/14
2 22/12/13
2 01/01/14
2 04/03/14
表2 :activity log
activity id activity done
1 call sent to client
2 client is not picking
我是否有一个SQL查询,它给我带来如下结果:
activity id | activity done | latest date of activity
1 call sent to client 23/1/14
2 client is not picking 04/03/14
我被困在必须获取最新活动日期以及其他字段的地方。请帮忙。
答案 0 :(得分:0)
你可以写这样的东西;
SELECT b.ActivityId, b.ActivityDone, MAX(a.ActivityDate) as 'ActivityDate'
FROM Table2 b
INNER JOIN Table1 a ON b.ActivityId = a.ActivityId
GROUP BY b.ActivityId, b.ActivityDone
答案 1 :(得分:0)
SELECT x.ActivityId, MAX(x.ActivityDate), y.ActivityDone
FROM Table2 y
INNER JOIN Table1 x ON y.ActivityId = x.ActivityId
GROUP BY b.ActivityId, b.ActivityDone
答案 2 :(得分:0)
试试这个
SELECT T2.ActivityID, T2.ActivityLog, MAX(CAST(T1.ActivityDate AS DAte)) AS 'ActivityDate'
FROM Table2 T2 INNER JOIN Table1 T1 ON T2.ActivityId = T1.ActivityId
GROUP BY T1.ActivityID,T2.ActivityID, T2.ActivityLog
<强> Fiddle Demo 强>
<强> OP:强>
ACTIVITYID ACTIVITYLOG ACTIVITYDATE
1 call sent to client 2014-01-23
2 client is not picking 2014-03-04