Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,437 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,341 people online right now. Registration is fast and FREE... Join Now!




How to Sum records on Field and Export to new Field

 
Reply to this topicStart new topic

How to Sum records on Field and Export to new Field, vb.net 2003

itayzoro
20 Aug, 2008 - 07:20 AM
Post #1

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 20

What i need is to sum all the [IN] Rows
Example:

Table 1 :
ID TIME_END IN
-------------------
1 00:01 5
2 00:31 8
3 05:01 3
4 06:01 7

so i need new table with new Field with 24 column that sum all the day from the first Fields on Table 1
and when Table 1 is update the "SUM_DAY" on Table2 is also update

Table 2 :
SUM_DAY
13 -- > its the sum of the Hour 00:00 - 00:59
0
0
0
0
3 -- > its the sum of the Hour 05:00 - 05:59
0
7 -- > its the sum of the Hour 07:00 - 07:59
till 23:59
User is offlineProfile CardPM
+Quote Post

AdaHacker
RE: How To Sum Records On Field And Export To New Field
20 Aug, 2008 - 09:52 AM
Post #2

D.I.C Head
**

Joined: 17 Jun, 2008
Posts: 176



Thanked: 27 times
My Contributions
If the problem is getting all those zeroes in between your actual data, one relatively easy way to do that is with an auxiliary table and an outer join. You could create a sequence table for the 24 hour values like this:
SQL
CREATE TABLE hours (hour SMALLINT NOT NULL PRIMARY KEY);
INSERT INTO hours VALUES (0), (1), (2), ..., (23);

You can then do an outer join on that table to get 1 row per hour and use COALESCE() to turn the NULLs into zeroes. However, you will also need to use some function to extract the hour portion from your time so that you can join on it. What that is will depend on your database product. The following works in MySQL.
SQL
SELECT hour, COALESCE(SUM(`in`), 0)
FROM Table1 RIGHT JOIN hours ON HOUR(Table1.time_end) = hours.hour
GROUP BY hour;

If it's important that you be able to access this as a constantly-updated table, as you describe, you can simply turn it into a view:
SQL
CREATE VIEW Table2 (hour, sum_day) AS SELECT hour, COALESCE(SUM(`in`), 0)
FROM Table1 RIGHT JOIN hours ON HOUR(Table1.time_end) = hours.hour
GROUP BY hour;

User is online!Profile CardPM
+Quote Post

itayzoro
RE: How To Sum Records On Field And Export To New Field
21 Aug, 2008 - 02:07 AM
Post #3

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 20

10nx AdaHacker but i dont know how to put this in my code this is Global hlp so.. its not hlp me

This is the way buT??
How can i sum time? lets say from 12:00 till 12:59

CODE

Dim sConnectionString, sSQL As String
        Dim DateFix As String = DateTime.Now().ToString("yyyyMMdd")
        Dim mstrfile As String = "sys" & DateFix & ".mdb"
        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\127.0.0.1\DataBase\" & mstrfile

        sSQL = "SELECT SUM([IN]) AS Expr1 FROM DATA_COUNTER WHERE SUM(TIME_END) > 00:01:00 "
        ' sSQL = "SELECT SUM([IN]) AS Expr1 FROM DATA_COUNTER WHERE ID = 2"


        Dim Sconn As New System.Data.OleDb.OleDbConnection(sConnectionString)
        Dim Scmd As New System.Data.OleDb.OleDbCommand(sSQL, Sconn)
        Dim dr As System.Data.OleDb.OleDbDataReader

        Sconn.Open()
        dr = Scmd.ExecuteReader()
        dr.Read()
        MsgBox(dr.Item("Expr1"))
        dr.Close()
        Sconn.Close()

User is offlineProfile CardPM
+Quote Post

itayzoro
RE: How To Sum Records On Field And Export To New Field
21 Aug, 2008 - 07:07 AM
Post #4

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 20

Took me all Day but this is the Answer :

If some1 know a better way (cause now i need 24 Queries) i am love 2 C

CODE


sSQL = "SELECT SUM([IN]) AS Expr1 FROM MyNewTable WHERE HOUR(TIME_END) & MINUTE(TIME_END) <= 059 AND HOUR(TIME_END) & MINUTE(TIME_END) >= 000"


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 02:05PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month