<!--#include file="./includes/sql.asp"-->
<%
Response.Write("<h1>Check for Duplicate Printer Machines</h1>")

Dim strSQL, rs

' Find duplicates by machinenumber
strSQL = "SELECT machinenumber, COUNT(*) as cnt " &_
         "FROM machines " &_
         "WHERE machinenumber LIKE '%-PRINTER' " &_
         "GROUP BY machinenumber " &_
         "HAVING COUNT(*) > 1 " &_
         "ORDER BY cnt DESC, machinenumber"
set rs = objConn.Execute(strSQL)

Response.Write("<h3>Duplicate Machine Numbers:</h3>")
Response.Write("<table border='1' style='border-collapse:collapse'>")
Response.Write("<tr><th>Machine Number</th><th>Count</th></tr>")
Dim hasDuplicates
hasDuplicates = False
While Not rs.EOF
    hasDuplicates = True
    Response.Write("<tr>")
    Response.Write("<td>" & Server.HTMLEncode(rs("machinenumber") & "") & "</td>")
    Response.Write("<td>" & rs("cnt") & "</td>")
    Response.Write("</tr>")
    rs.MoveNext
Wend
Response.Write("</table>")

If Not hasDuplicates Then
    Response.Write("<p style='color:green'>No duplicates found!</p>")
End If
rs.Close

' Show all printer machines grouped
Response.Write("<h3>All Printer Machines (grouped by name):</h3>")
strSQL = "SELECT machinenumber, COUNT(*) as cnt, GROUP_CONCAT(machineid) as ids " &_
         "FROM machines " &_
         "WHERE machinetypeid = 15 " &_
         "GROUP BY machinenumber " &_
         "ORDER BY machinenumber"
set rs = objConn.Execute(strSQL)
Response.Write("<table border='1' style='border-collapse:collapse'>")
Response.Write("<tr><th>Machine Number</th><th>Count</th><th>Machine IDs</th></tr>")
While Not rs.EOF
    Response.Write("<tr>")
    Response.Write("<td>" & Server.HTMLEncode(rs("machinenumber") & "") & "</td>")
    Response.Write("<td>" & rs("cnt") & "</td>")
    Response.Write("<td>" & Server.HTMLEncode(rs("ids") & "") & "</td>")
    Response.Write("</tr>")
    rs.MoveNext
Wend
Response.Write("</table>")
rs.Close

objConn.Close
%>
