0

I have a query that looks like this:

SELECT
B.Company,
COUNT(*) AS QTY
FROM Table1 A
INNER JOIN Table2 B
ON A.CompanySk = B.EDWCompanySK
WHERE A.StartedDate >= '20171128'
GROUP BY B.Company
ORDER BY QTY DESC 

However, there is a field name "Ticket" in table1 that has duplicate values, that I need to exclude. How can I return the count with excluding duplicate "ticket" values.

3
  • Can table1.Ticket be NULL? Dec 4, 2018 at 13:09
  • There are zero NULL values in Table1.Ticket Dec 4, 2018 at 13:11
  • Use COUNT(DISTINCT Table1.Ticket)
    – Akina
    Dec 4, 2018 at 13:13

1 Answer 1

4

Use the next query

SELECT
B.Company,
COUNT(DISTINCT A.Ticket) AS QTY
FROM Table1 A
INNER JOIN Table2 B
ON A.CompanySk = B.EDWCompanySK
WHERE A.StartedDate >= '20171128'
GROUP BY B.Company
ORDER BY QTY DESC
2
  • It is returning the error: "The multi-part identifier Table1.Ticket could not be bound." Dec 4, 2018 at 13:20
  • I've corrected my answer Dec 4, 2018 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.