943,917 Members | Top Members by Rank

Ad:
Jun 14th, 2008
0

Return the sum of elements in a row

Expand Post »
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.
Similar Threads
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jun 14th, 2008
0

Re: Return the sum of elements in a row

Click to Expand / Collapse  Quote originally posted by tuse ...
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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Database Design Forum Timeline: preSolved problem, I need a link
Next Thread in Database Design Forum Timeline: Nationality Lookup table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC