SQL Grouping SUM


 
Thread Tools Search this Thread
Top Forums Programming SQL Grouping SUM
# 1  
Old 01-04-2012
SQL Grouping SUM

Hello people, I'm scratching my head to find a solution to this query.

I have a simple SQL table:
Code:
data        | tot
==================
111201/0000 | 3
111201/0001 | 5 
111201/0002 | 7 
111201/0003 | 2 
.....
111201/0059 | 1 
111201/0100 | 5 
111201/0101 | 8 
111201/0102 | 6
111201/0103 | 2
....

What, what I need to do is make a 'tot' SUM based on data column. 0000, 0001, 0002, is the time (00:00, 00:01, 00:02).

So if I make this select::
Code:
SELECT SUM(`tot`) FROM `data` WHERE `data` like '%/00%'

The output will be the 'tot' SUM from the first hour (0000 to 0059), so for the next hour I should make another select like:
Code:
SELECT SUM(`tot`) FROM `data` WHERE `data` like '%/01%'

Is it possible to make only one select to show me the SUM of each hours?

Thanks!

Last edited by vbe; 01-04-2012 at 12:28 PM..
# 2  
Old 01-04-2012
if the result of you SQL extract is stored in file mySQLextractFile.txt:
Code:
nawk -F'[/|]' '/^[0-9]/{a[$1"/" int($2/100)]+=$NF}END{for(i in a) print i,a[i]}' OFS='|' mySQLextractFile.txt

# 3  
Old 01-04-2012
Nope, I'll elaborate the result after the select. I need directly the SUM for each hour from output SQL select!
Maybe using grouping (and SUM) by partial, but I don't know how to implement yet!

---------- Post updated at 11:48 AM ---------- Previous update was at 11:40 AM ----------

Well, I found myself a solution, it was so simple!!! :-)
Code:
SELECT data, SUM(`tot`) as SUM, COUNT(tot) As HOUR FROM `data` GROUP BY MID(data, 8,2)

Maybe can help some other user! Smilie
# 4  
Old 01-04-2012
-- deleted --

(Ooops cross post)

Last edited by ctsgnb; 01-04-2012 at 12:56 PM.. Reason: (Ooops cross post)
# 5  
Old 01-04-2012
Quote:
Originally Posted by ctsgnb
What is your table name ?
What could you please provide a description (need to know the type of the field)

Code:
desc yourtable

Simple text, but I've already found a solution Smilie
# 6  
Old 01-04-2012
Not sure if this is strictly a unix issue, but in any case,......

What database are you using? There is likely a date/time function we can call in your query to produce your results straight off the bat.

Your logic of using a where clause based on being like '%/00', thrn '%/01' etc. could generate you spurious values.


Something like:
Code:
SELECT to_char(to_date(data,'yymmdd/hhmi'),'yymmdd/hh') timestamp , SUM(tot) sumtot
FROM table
GROUP BY timestamp
ORDER BY timestamp ;

Might get you started and perhaps use of a trimming function to ignore the minutes value will get the actul result you want straight from the SQL query, but like I say, it depends on the database server you are using.


Wholly untested bit of theory, but I hope that this helps a bit. If you respond with the database software, you may get a complete response. In my opinion you would save processing time to get the database to perform the work within the initial query rather than get the data to a file then post-process it to create the output you want. You can use the to_char function of many databases to give you date/time output in the format you want too.



Robin
Liverpool/Blackburn,
UK
# 7  
Old 01-04-2012
@Lord Spectre
I had this (in Oracle):
Code:
SQL> select substr(data,0,9), sum(tot) from mytable group by substr(data,0,9);
SUBSTR(DA   SUM(TOT)
--------- ----------
111201/01          5
111201/00         15

(I guess your SQL is MySQL?)
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grouping and counting

Hello, I would like to group/sort a file of records by a particular field and then count how many records belong in that grouping. For example say I have the following data: 1234|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA ABCD|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA EFGH|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|BB... (14 Replies)
Discussion started by: Nik44
14 Replies

2. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Grouping output with sum

Hi Gurus, I would like to do in a script something that is really easy in sql. Basically, I have a file with a fomat like the following: name1_test 501 name 1 33 510 test2 900 name1_test 300 So, I would like to group them as... (3 Replies)
Discussion started by: ocramas
3 Replies

4. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

5. UNIX for Dummies Questions & Answers

Grouping in grep

How do you do grouping in grep? Here's how I tried it at first: egrep 'qualit(y|ies)' /usr/share/dict/words -bash: syntax error near unexpected token `(' I'm using GNUgrep, and I found this on their site. grep regular expression syntax So I tried this: egrep 'qualit\(y\|ies\)'... (2 Replies)
Discussion started by: sudon't
2 Replies

6. Shell Programming and Scripting

PERL : SQL array bind issue while grouping

Hi, I have an array of strings. Each string has 4 comma separated values. I am binding this array to a SQL where I am required to do an INSERT after grouping. The binding is done as : $insertADWSth->bind_param_array(1,A_CONSTANT_STRING);... (1 Reply)
Discussion started by: sinpeak
1 Replies

7. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

8. UNIX for Dummies Questions & Answers

grouping and sorting??

how would I write a command line that creates a new file named stuff.txt in the current working directory which contains the number of directories in the current working directory, followed by the number of empty files in the current working directory, followed by the name of the working directory? (3 Replies)
Discussion started by: jorogon0099
3 Replies
Login or Register to Ask a Question