Assuming that:
- All records are duplicates.
- Duplicates always form a pair having a "Dial" and a "Hangup" in column
lastapp
with same uniqueid
.
- All elements of the Hangup that are filled in are the same as in the "Dial" (with the exception of
lastapp
).
So this should work:
SELECT
a.calldate AS calldate,
a.clid, AS clid,
a.src AS src,
a.dst AS dst,
a.dcontext AS dcontext,
a.channel AS channel,
a.dstchannel AS dstchannel,
'Dial + Hangup' AS lastapp,
a.lastdata AS lastdata,
(a.duration + b.duration) AS duration,
(a.billsec + b.billsec) AS billsec,
a.disposition AS disposition,
a.amaflags AS amaflags,
a.accountcode AS accountcode,
a.uniqueid AS uniqueid
FROM
tabela a
INNER JOIN
tabela b ON a.uniqueid = b.uniqueid
WHERE a.lastapp = 'Dial'
AND b.lastapp = 'Hangup'
If there are records that are not duplicates too and you want to display them also along with the duplicate pair (which should be considered as a single record), try this:
SELECT
a.calldate AS calldate,
a.clid, AS clid,
a.src AS src,
a.dst AS dst,
a.dcontext AS dcontext,
a.channel AS channel,
a.dstchannel AS dstchannel,
IF(ISNULL(b.uniqueid), a.lastapp, 'Dial + Hangup') AS lastapp,
a.lastdata AS lastdata,
(a.duration + COALESCE(b.duration, 0)) AS duration,
(a.billsec + COALESCE(b.billsec, 0)) AS billsec,
a.disposition AS disposition,
a.amaflags AS amaflags,
a.accountcode AS accountcode,
a.uniqueid AS uniqueid
FROM
tabela a
LEFT OUTER JOIN
tabela b ON a.uniqueid = b.uniqueid
WHERE a.lastapp = 'Dial'
AND (ISNULL(b.uniqueid) OR b.lastapp = 'Hangup')