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. using aggregates on NULL

using aggregates on NULL

Scheduled Pinned Locked Moved Database
databasehelpquestion
9 Posts 6 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.
  • R Offline
    R Offline
    Rob Philpott
    wrote on last edited by
    #1

    Hi database people, If I have a table with one columm (col) and these rows:

    A
    A
    B
    C
    C
    C
    NULL
    NULL

    and I run the query:

    select col, count(col) group by col

    I get:

    A 2
    B 1
    C 3
    NULL 0

    How would I change it so that I got the count of nulls (ie. 2) rather than 0? Any help appreciated! Regards, Rob Philpott.

    M A S 3 Replies Last reply
    0
    • R Rob Philpott

      Hi database people, If I have a table with one columm (col) and these rows:

      A
      A
      B
      C
      C
      C
      NULL
      NULL

      and I run the query:

      select col, count(col) group by col

      I get:

      A 2
      B 1
      C 3
      NULL 0

      How would I change it so that I got the count of nulls (ie. 2) rather than 0? Any help appreciated! Regards, Rob Philpott.

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Give NULL a value that can be counted.

      SELECT
          ISNULL(col,'NULL') AS col, 
          COUNT(col) 
      GROUP BY 
          col
      
      R 1 Reply Last reply
      0
      • R Rob Philpott

        Hi database people, If I have a table with one columm (col) and these rows:

        A
        A
        B
        C
        C
        C
        NULL
        NULL

        and I run the query:

        select col, count(col) group by col

        I get:

        A 2
        B 1
        C 3
        NULL 0

        How would I change it so that I got the count of nulls (ie. 2) rather than 0? Any help appreciated! Regards, Rob Philpott.

        A Offline
        A Offline
        albCode
        wrote on last edited by
        #3

        select count(col) from urtable where col=NULL _____________________ Proud to be Albanian _____________________

        1 Reply Last reply
        0
        • M Michael Potter

          Give NULL a value that can be counted.

          SELECT
              ISNULL(col,'NULL') AS col, 
              COUNT(col) 
          GROUP BY 
              col
          
          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          yeah, that would do it. Thanks for the reply but if I want to see the count of null alongside the other results? eg. A 2 B 1 C 3 NULL 2 ? Regards, Rob Philpott.

          M 1 Reply Last reply
          0
          • R Rob Philpott

            Hi database people, If I have a table with one columm (col) and these rows:

            A
            A
            B
            C
            C
            C
            NULL
            NULL

            and I run the query:

            select col, count(col) group by col

            I get:

            A 2
            B 1
            C 3
            NULL 0

            How would I change it so that I got the count of nulls (ie. 2) rather than 0? Any help appreciated! Regards, Rob Philpott.

            S Offline
            S Offline
            Steve Schaneville
            wrote on last edited by
            #5

            this will do it: SELECT tt.col, Count(isnull(tt.col, 'NULL')) FROM TempTesting tt GROUP BY tt.col ~Steve www.roundpolygons.com

            C 1 Reply Last reply
            0
            • S Steve Schaneville

              this will do it: SELECT tt.col, Count(isnull(tt.col, 'NULL')) FROM TempTesting tt GROUP BY tt.col ~Steve www.roundpolygons.com

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              I think you need to change

              SELECT tt.col, Count(isnull(tt.col, 'NULL'))

              to

              SELECT isnull(tt.col, 'NULL'), Count(tt.col)

              and the group by needs the isnull check also. Chris Meech I am Canadian. [heard in a local bar] When I want privacy, I'll close the bathroom door. [Stan Shannon] BAD DAY FOR: Friendly competition, as Ford Motor Co. declared the employee parking lot at its truck plant in Dearborn, Mich., off limits to vehicles built by rival companies. Workers have to drive a Ford to work, or park across the street. [CNNMoney.com] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me] -- modified at 13:23 Wednesday 22nd March, 2006

              J 1 Reply Last reply
              0
              • R Rob Philpott

                yeah, that would do it. Thanks for the reply but if I want to see the count of null alongside the other results? eg. A 2 B 1 C 3 NULL 2 ? Regards, Rob Philpott.

                M Offline
                M Offline
                Michael Potter
                wrote on last edited by
                #7

                I believe it does put it alongside.

                R 1 Reply Last reply
                0
                • M Michael Potter

                  I believe it does put it alongside.

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  Quite right! Genious. Perhaps I should have tried it first... Thanks for your help. :) Regards, Rob Philpott.

                  1 Reply Last reply
                  0
                  • C Chris Meech

                    I think you need to change

                    SELECT tt.col, Count(isnull(tt.col, 'NULL'))

                    to

                    SELECT isnull(tt.col, 'NULL'), Count(tt.col)

                    and the group by needs the isnull check also. Chris Meech I am Canadian. [heard in a local bar] When I want privacy, I'll close the bathroom door. [Stan Shannon] BAD DAY FOR: Friendly competition, as Ford Motor Co. declared the employee parking lot at its truck plant in Dearborn, Mich., off limits to vehicles built by rival companies. Workers have to drive a Ford to work, or park across the street. [CNNMoney.com] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me] -- modified at 13:23 Wednesday 22nd March, 2006

                    J Offline
                    J Offline
                    Jorge Novo
                    wrote on last edited by
                    #9

                    select sum case when tt.coll is null then 1 else 0 end There are many ways as we can see:^) Hear,See,Learn,Understand,Practice Many can be good,but only ONE can be the best keep practicing

                    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