SELECT *
FROM HistoricCall h
INNER JOIN Responses r
ON h.projectid = r.projectId
AND h.caseId = r.caseId
The HistoricCall
table has a column for number of attempts for each time that we've called someone (h.callNumber
).
I'm trying to write a query that would display:
SELECT
h.callDate,
r.cellmap,
COUNT(*)
but only the h.callDate
that corresponds to the highest callNumber
for each record.
HistoricCall
:
projectId | caseId | callNumber | callDate |
---|---|---|---|
100018 | 0000000001 | 1 | 2014-09-11 16:32:11.000 |
100018 | 0000000001 | 2 | 2014-09-11 20:43:01.000 |
100018 | 0000000001 | 3 | 2014-09-12 21:09:55.000 |
100018 | 0000000001 | 4 | 2014-09-13 14:58:14.000 |
100018 | 0000000001 | 5 | 2014-09-14 16:42:27.000 |
100018 | 0000000001 | 6 | 2014-09-16 21:54:09.000 |
100018 | 0000000001 | 7 | 2014-09-16 22:30:24.000 |
100018 | 0000000002 | 1 | 2014-09-11 16:36:17.000 |
100018 | 0000000002 | 2 | 2014-09-13 18:05:44.000 |
100018 | 0000000002 | 3 | 2014-09-14 16:39:03.000 |
Responses
:
projectId | caseId | cellmap |
---|---|---|
637850 | 0000000001 | 9101 |
637850 | 0000000002 | 9052 |
637850 | 0000000003 | 5071 |
637850 | 0000000004 | 5173 |
637850 | 0000000005 | |
637850 | 0000000006 | 8062 |
637850 | 0000000007 | 0012 |
637850 | 0000000008 | 5292 |
637850 | 0000000009 | 9230 |
637850 | 0000000010 | 5187 |
Desired result:
projectId | callDate | cellmap | Count |
---|---|---|---|
637850 | 2014-09-11 | 0012 | 3 |
637850 | 2014-09-11 | 0014 | 7 |
637850 | 2014-09-12 | 0012 | 4 |
637850 | 2014-09-12 | 0014 | 2 |