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. SQL counting

SQL counting

Scheduled Pinned Locked Moved Database
databasehelp
7 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.
  • Q Offline
    Q Offline
    Qazzy64
    wrote on last edited by
    #1

    I want to get a count of an ID where the ID is either = -999 or its not. So i have 2 counts in total. I used union all to get that but i think there would be better way to get the count from the same field. any help would be appreciated. select count(ID) as Id_999 from table_A where ID='-999' union all select count(ID) from table_A where ID<>'-999' there is no null values allowed so it either a number or -999 if null when table is loaded.

    S L 2 Replies Last reply
    0
    • Q Qazzy64

      I want to get a count of an ID where the ID is either = -999 or its not. So i have 2 counts in total. I used union all to get that but i think there would be better way to get the count from the same field. any help would be appreciated. select count(ID) as Id_999 from table_A where ID='-999' union all select count(ID) from table_A where ID<>'-999' there is no null values allowed so it either a number or -999 if null when table is loaded.

      S Offline
      S Offline
      scottgp
      wrote on last edited by
      #2

      You could turn the counts into separate columns: select SUM ( CASE WHEN ID = '-999' THEN 1 ELSE 0 END) AS ID999_count, SUM ( CASE WHEN ID <> '-999' THEN 1 ELSE 0 END) AS nonID999_count from table_A Scott

      Q 1 Reply Last reply
      0
      • S scottgp

        You could turn the counts into separate columns: select SUM ( CASE WHEN ID = '-999' THEN 1 ELSE 0 END) AS ID999_count, SUM ( CASE WHEN ID <> '-999' THEN 1 ELSE 0 END) AS nonID999_count from table_A Scott

        Q Offline
        Q Offline
        Qazzy64
        wrote on last edited by
        #3

        Thanks Scott it works great. I was close to that but i was putting ID, then the sum(CASE statement). Thanks again.

        1 Reply Last reply
        0
        • Q Qazzy64

          I want to get a count of an ID where the ID is either = -999 or its not. So i have 2 counts in total. I used union all to get that but i think there would be better way to get the count from the same field. any help would be appreciated. select count(ID) as Id_999 from table_A where ID='-999' union all select count(ID) from table_A where ID<>'-999' there is no null values allowed so it either a number or -999 if null when table is loaded.

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

          Qazzy64 wrote:

          there is no null values allowed so it either a number or -999 if null when table is loaded.

          Just curious; does that mean that you replace a null-test with a test for -999, and act according? Why was null disallowed anyway?

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          Q 1 Reply Last reply
          0
          • L Lost User

            Qazzy64 wrote:

            there is no null values allowed so it either a number or -999 if null when table is loaded.

            Just curious; does that mean that you replace a null-test with a test for -999, and act according? Why was null disallowed anyway?

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            Q Offline
            Q Offline
            Qazzy64
            wrote on last edited by
            #5

            When they load the table it has a number(ID) or if its null they load -999 so they know to reprocess it later on when other matching data shows up. Hope that helps.

            L 1 Reply Last reply
            0
            • Q Qazzy64

              When they load the table it has a number(ID) or if its null they load -999 so they know to reprocess it later on when other matching data shows up. Hope that helps.

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

              The reason I asked is because it does not make any sense to me; you'd still have to deal with the same type of checks. The only difference being that the new "strategy" will be alien to new developers, and will cause weirder and harder to debug-exceptions than a null-reference would. I might be missing some obvious advantage of the approach. So, where is it? What's the 'added value' of using a marker-value above a null-value?

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              Q 1 Reply Last reply
              0
              • L Lost User

                The reason I asked is because it does not make any sense to me; you'd still have to deal with the same type of checks. The only difference being that the new "strategy" will be alien to new developers, and will cause weirder and harder to debug-exceptions than a null-reference would. I might be missing some obvious advantage of the approach. So, where is it? What's the 'added value' of using a marker-value above a null-value?

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                Q Offline
                Q Offline
                Qazzy64
                wrote on last edited by
                #7

                I agree with you that doing anything causes more problems eventually. Myself i would not have brought the data forward until all fields are present from source file. But i do not make that choice and i got involved after project was started. This is more of a transactional system then data warehouse like they said. Hate to say it but it goes back to the way it was architected and going forward from there has been to get it to work. Very complex data modeling which i would have simplified by bringing it in and leveling data types. They chose to bring it forward anyway and then leave just before the DW level. Makes no sense but i wasnt in on those meetings. I get to deal with it and figure out if the data is accurate. Fun times. :)

                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