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. counting in SQL server 2000??

counting in SQL server 2000??

Scheduled Pinned Locked Moved Database
tutorialdatabasesql-serversysadminquestion
4 Posts 3 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.
  • K Offline
    K Offline
    Karan_TN
    wrote on last edited by
    #1

    currently i m using sql server 2000. my column consist of mixture of country values. i need to take the count of each countries. example.... colA : india,japan,india,china,japan,USA,india,singapore,china,india the output should be..... india japan china USA singapore 4 2 2 1 1 how to achieve it? - KARAN

    K 1 Reply Last reply
    0
    • K Karan_TN

      currently i m using sql server 2000. my column consist of mixture of country values. i need to take the count of each countries. example.... colA : india,japan,india,china,japan,USA,india,singapore,china,india the output should be..... india japan china USA singapore 4 2 2 1 1 how to achieve it? - KARAN

      K Offline
      K Offline
      Krish KP
      wrote on last edited by
      #2

      SELECT ColA, Count(*) FROM TblA GROUP BY ColA

      Regards KP

      K A 2 Replies Last reply
      0
      • K Krish KP

        SELECT ColA, Count(*) FROM TblA GROUP BY ColA

        Regards KP

        K Offline
        K Offline
        Karan_TN
        wrote on last edited by
        #3

        thx dude! ;)

        1 Reply Last reply
        0
        • K Krish KP

          SELECT ColA, Count(*) FROM TblA GROUP BY ColA

          Regards KP

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          This will not actuall give the required results. The original question wanted india japan china USA singapore 4 2 2 1 1 This will give india 4 japan 2 china 2 USA 1 singapore 1 You actually need to pivot the data. This code should show you how to do it /* create test table and populate */ create table #b1(cola varchar(20)) insert into #b1 select 'India' union all select 'USA' union all select 'India' union all select 'UK' /* now the actual code */ DECLARE @SQL nvarchar(4000) SET @SQL='' SELECT @SQL= @SQL + 'SUM(CASE WHEN cola=''' + a.cola + ''' THEN 1 ELSE 0 END) AS [' + a.cola + '],' FROM (select distinct cola from #b1) as a select @SQL = left(@SQL,len(@SQL)-1) SET @SQL='SELECT ' + @SQL + ' FROM #b1' EXEC(@SQL) Hope this helps

          Bob Ashfield Consultants Ltd

          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