Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Distinct + Count

Distinct + Count

Scheduled Pinned Locked Moved Database
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I want to find distinct country and count of each country from a country table. How should I do? Result: Country | Count Japan | 10 UK | 8 Canada | 12

    ***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

    R 1 Reply Last reply
    0
    • L Lost User

      I want to find distinct country and count of each country from a country table. How should I do? Result: Country | Count Japan | 10 UK | 8 Canada | 12

      ***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

      R Offline
      R Offline
      Ravi Sant
      wrote on last edited by
      #2

      select country, count(country) from countrytable group by country

      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

      L 2 Replies Last reply
      0
      • R Ravi Sant

        select country, count(country) from countrytable group by country

        // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thank You, I missed group by.

        ***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

        1 Reply Last reply
        0
        • R Ravi Sant

          select country, count(country) from countrytable group by country

          // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Is is not working for null country and does not give correct count Hepl ?

          ***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

          B 1 Reply Last reply
          0
          • L Lost User

            Is is not working for null country and does not give correct count Hepl ?

            ***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            Use isnull for your count field. e.g

            select country, count(isnull(country,0)) from countrytable
            group by country


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

            S 1 Reply Last reply
            0
            • B Blue_Boy

              Use isnull for your count field. e.g

              select country, count(isnull(country,0)) from countrytable
              group by country


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

              S Offline
              S Offline
              SilimSayo
              wrote on last edited by
              #6

              If country is null, it need not be shown. I would say

              select country, count(country) from countrytable
              where country is not null
              group by country

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups