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. addition having null values in between

addition having null values in between

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

    I am designing a database for students management. For flexibility reasons I have kept it on a monthly basis wherein the students are awarded the number of lectures they have attended and zero if not. the department doesn't follow a strict time table hence there are times when there is no lecture for that subject during the entire day. The default value is null for such scenario. In short for each subject a student gets as following: If present - number of lectures attended if absent - zero if no lectures for the day - NULL. The problem is when adding up the entire column(yes column) if there is a null value the final answer is null. I want the answer to be summation of all the numbers present and Not count in the NULL values.

    T Z 2 Replies Last reply
    0
    • U User 11806910

      I am designing a database for students management. For flexibility reasons I have kept it on a monthly basis wherein the students are awarded the number of lectures they have attended and zero if not. the department doesn't follow a strict time table hence there are times when there is no lecture for that subject during the entire day. The default value is null for such scenario. In short for each subject a student gets as following: If present - number of lectures attended if absent - zero if no lectures for the day - NULL. The problem is when adding up the entire column(yes column) if there is a null value the final answer is null. I want the answer to be summation of all the numbers present and Not count in the NULL values.

      T Offline
      T Offline
      Tomas Takac
      wrote on last edited by
      #2

      How do you add up the values? SUM should give you the correct result with NULL values. What database engine do you use?

      U 1 Reply Last reply
      0
      • U User 11806910

        I am designing a database for students management. For flexibility reasons I have kept it on a monthly basis wherein the students are awarded the number of lectures they have attended and zero if not. the department doesn't follow a strict time table hence there are times when there is no lecture for that subject during the entire day. The default value is null for such scenario. In short for each subject a student gets as following: If present - number of lectures attended if absent - zero if no lectures for the day - NULL. The problem is when adding up the entire column(yes column) if there is a null value the final answer is null. I want the answer to be summation of all the numbers present and Not count in the NULL values.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        If this is Microsoft SQL server you can easily use IsNull() or COALESCE().

        There are only 10 types of people in the world, those who understand binary and those who don't.

        U 1 Reply Last reply
        0
        • T Tomas Takac

          How do you add up the values? SUM should give you the correct result with NULL values. What database engine do you use?

          U Offline
          U Offline
          User 11806910
          wrote on last edited by
          #4

          create or replace view Total_TABLE as
          select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,29,30,31,1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+29+30+31 as Total
          from Add_attend;

          the problem is when there is a null value in the table the final result after adding is Null and I want the result of all all the numbers and NOT null value. I am using mysql wampserver

          T 1 Reply Last reply
          0
          • Z ZurdoDev

            If this is Microsoft SQL server you can easily use IsNull() or COALESCE().

            There are only 10 types of people in the world, those who understand binary and those who don't.

            U Offline
            U Offline
            User 11806910
            wrote on last edited by
            #5

            I got your point sir. I know i am an inexperienced newbie, this the first time I am doing this. It would be of great help if you can help me with this. I am using Mysql server

            Z 1 Reply Last reply
            0
            • U User 11806910

              I got your point sir. I know i am an inexperienced newbie, this the first time I am doing this. It would be of great help if you can help me with this. I am using Mysql server

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #6

              Member 11840363 wrote:

              It would be of great help if you can help me with this.

              I did. :confused:

              There are only 10 types of people in the world, those who understand binary and those who don't.

              U 1 Reply Last reply
              0
              • Z ZurdoDev

                Member 11840363 wrote:

                It would be of great help if you can help me with this.

                I did. :confused:

                There are only 10 types of people in the world, those who understand binary and those who don't.

                U Offline
                U Offline
                User 11806910
                wrote on last edited by
                #7

                10 is TWO in binary. can you please help me in framing the correct query? Please

                Z 1 Reply Last reply
                0
                • U User 11806910

                  10 is TWO in binary. can you please help me in framing the correct query? Please

                  Z Offline
                  Z Offline
                  ZurdoDev
                  wrote on last edited by
                  #8

                  Did you lookup how to use IsNull or COALESCE()? It's very simple. SELECT COALESCE(field1, 0) or SELECT IsNull(field1, 0) If field1 is null then you will get 0. So, if you are adding them up then adding 0 will not get counted.

                  There are only 10 types of people in the world, those who understand binary and those who don't.

                  1 Reply Last reply
                  0
                  • U User 11806910

                    create or replace view Total_TABLE as
                    select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,29,30,31,1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+29+30+31 as Total
                    from Add_attend;

                    the problem is when there is a null value in the table the final result after adding is Null and I want the result of all all the numbers and NOT null value. I am using mysql wampserver

                    T Offline
                    T Offline
                    Tomas Takac
                    wrote on last edited by
                    #9

                    Having a column per day is a poor design choice IMO. I'd strongly suggest you review it. As RyanDev already mentioned, you should use COALESCE[^], but you need to apply it to every value you are adding up.

                    COALESCE(1, 0) + COALESCE(2, 0) + ... + COALESCE(31, 0)

                    U 1 Reply Last reply
                    0
                    • T Tomas Takac

                      Having a column per day is a poor design choice IMO. I'd strongly suggest you review it. As RyanDev already mentioned, you should use COALESCE[^], but you need to apply it to every value you are adding up.

                      COALESCE(1, 0) + COALESCE(2, 0) + ... + COALESCE(31, 0)

                      U Offline
                      U Offline
                      User 11806910
                      wrote on last edited by
                      #10

                      Thanks a lot sir. it has been solved :)

                      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