SQL SERVER - count unique records - rows from two and more Tables

Today, I need to unique count city in 6 tables so I have search so many websites and spend so much time and learn so many things then I create a query and get the exact out in single out using  “UNION ALL” keyword. That query is given below.

Query:-




SELECT
    city,
    COUNT(city) AS [citycount]
FROM(
    SELECT city FROM table1

    UNION ALL
    SELECT city  FROM table2
              UNION ALL
          SELECT city FROM table3
   
)neeraj
GROUP BY city HAVING ( COUNT(city) > 1 ) order by [citycount] desc



Out-Put:-


SQL SERVER - count unique records - rows from two and more Tables SQL SERVER - count unique records - rows from two and more Tables Reviewed by NEERAJ SRIVASTAVA on 4:33:00 PM Rating: 5

No comments:

Powered by Blogger.