Return the sum of elements in a row

Reply

Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Return the sum of elements in a row

 
0
  #1
Jun 14th, 2008
Is there is query with which I can get the sum of elements in one row?

I am trying to develop an atttendance application. Design as follows-

<date1> <date2> <date3> <date4>--- <attended>
<rollno1> 1 0 1 1 3
<rollno2> 0 0 1 1 2
<rollno3> 0 1 0 0 1

I am trying to get the attended field.

Is it possible, or is it a bad design?

Please suggest an alternate design if so.

I am absolutely new to database design.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Return the sum of elements in a row

 
0
  #2
Jun 14th, 2008
Originally Posted by tuse View Post
Design as follows-

<date1> <date2> <date3> <date4>--- <attended>
<rollno1> 1 0 1 1 3
<rollno2> 0 0 1 1 2
<rollno3> 0 1 0 0 1
How to design the appropriate select statement? Also, what if you want to add a further date <date5>? From a theoretical point of view, your design even violates the first normal form (1NF), because of the repeating date-x group. You will run into serious difficulties by that design. Your table might be considered to be badly designed.
Simple rule: if you are forced to expand the number of columns when new data need to be recorded, you must put this sort of data into rows, and not in columns! So a rather better design is:
table: roda
roll#     dates
-------------------
rollno1   date1
rollno1   date3
rollno1   date4
rollno2   date3
rollno2   date4
rollno3   date2
This arrangement is much more flexible (also fulfils 1NF, 2NF, 3NF, BCNF, where 3NF is the appropriate normal form a table always should meed. You need to learn something about Codd's normalization theorie if you want to design good tables).

For example: select attended:
select roll#, count(roll#) as attended from roda group by roll#
will give:
roll#       attended
-----------------
rollno1   3
rollno2   2
rollno3   1

Further advice: Tables always must have (yes, they must have that!) primary key, for example primary key(roll#, dates).

Hope this will help you.

krs,
tesu
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Database Design Forum
Thread Tools Search this Thread



Tag cloud for Database Design
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC