SELECT l.refname AS 'Account Name', avg(l.lskinid) AS 'lskin', COUNT(d.lednis)
FROM dnisconfig c
JOIN dnis d ON d.dnisid = c.frn_dnisid
JOIN lskin l ON l.lskinid = d.add_lskinid
WHERE d.lestatus = 1
AND l.isactive = 1
AND c.affectationLookup = 1
AND l.refname LIKE 'lag%'
AND d.leoutdial LIKE '%manual%'
GROUP BY refname
ORDER BY refname
SELECT l.lskinid, l.refname, d.lednis, c.affectationLookup
FROM dnisconfig c
JOIN dnis d ON d.dnisid = c.frn_dnisid
JOIN lskin l ON l.lskinid = d.add_lskinid
WHERE d.lestatus = 1
AND l.isactive = 1
AND c.affectationLookup = 1
AND l.refname LIKE 'lag%'
ORDER BY l.refname, d.lednis