Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-07-2008, 09:50 PM   #1 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
how does one accomplish the work done by the "compute" below

Code:
select rep,cust,amount
 from orders 
order by rep,cust 
compute sum(amount) by rep,cust 
compute sum(amount),avg(amount) by rep;
From a book by Groff and Weinberg "SQL" BTW a superb book!

I am sure the SQL shown works somewhere...probably Oracle.

Doesn't work with MySQL!

HMM! maybe I need to specify innodb!

Last edited by landonmkelsey; 10-07-2008 at 09:51 PM. Reason: left out 1 sentence
landonmkelsey is offline   Reply With Quote
Old 10-08-2008, 08:49 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 741
DJMaze is on a distinguished road
Compute is MSSQL

There are many other ways like:
Code:
select
    rep,
    cust,
    sum(amount)
from orders 
group by rep, cust
Also found this for you: MySQL :: equivalent for 'compute' in sql server
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 10-08-2008, 09:58 AM   #3 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
the computes do a bigger job

thanks but the compute tokens produce more than this one:

select cust,rep,sum(amount) from orders group by rep,cust order by cust,rep;

I'd post it here but MySQL refuses!

I'll try MS SQL 2008!...later!

Thanks for the link!
landonmkelsey is offline   Reply With Quote
Old 10-08-2008, 10:25 AM   #4 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
appreciate the link...I just posted the same question on that forum

betting the answer has something to do with stored procedures
landonmkelsey is offline   Reply With Quote
Old 10-08-2008, 11:15 AM   #5 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
works great under MS SQL server:

results:

rep cust amount
----------- ----------- ----------------------
101 2102 3978

sum
----------------------
3978

rep cust amount
----------- ----------- ----------------------
101 2108 150

sum
----------------------
150

rep cust amount
----------- ----------- ----------------------
101 2113 22500

sum
----------------------
22500

sum avg
---------------------- ----------------------
26628 8876

rep cust amount
----------- ----------- ----------------------
102 2106 2130
102 2106 1896

sum
----------------------
4026

rep cust amount
----------- ----------- ----------------------
102 2114 15000

sum
----------------------
15000

rep cust amount
----------- ----------- ----------------------
102 2120 3750

sum
----------------------
3750

sum avg
---------------------- ----------------------
22776 5694

rep cust amount
----------- ----------- ----------------------
103 2111 600
103 2111 2100

sum
----------------------
2700

sum avg
---------------------- ----------------------
2700 1350

rep cust amount
----------- ----------- ----------------------
105 2103 3276
105 2103 702
105 2103 27500
105 2103 4104

sum
----------------------
35582

rep cust amount
----------- ----------- ----------------------
105 2111 3745

sum
----------------------
3745

sum avg
---------------------- ----------------------
39327 7865.4

rep cust amount
----------- ----------- ----------------------
106 2101 1458

sum
----------------------
1458

rep cust amount
----------- ----------- ----------------------
106 2117 31500

sum
----------------------
31500

sum avg
---------------------- ----------------------
32958 16479

rep cust amount
----------- ----------- ----------------------
107 2109 31350

sum
----------------------
31350

rep cust amount
----------- ----------- ----------------------
107 2124 2430
107 2124 652

sum
----------------------
3082

sum avg
---------------------- ----------------------
34432 11477.3333333333

rep cust amount
----------- ----------- ----------------------
108 2112 2925
108 2112 45000

sum
----------------------
47925

rep cust amount
----------- ----------- ----------------------
108 2114 7100

sum
----------------------
7100

rep cust amount
----------- ----------- ----------------------
108 2118 652
108 2118 760
108 2118 776
108 2118 1420

sum
----------------------
3608

sum avg
---------------------- ----------------------
58633 8376.14285714286

rep cust amount
----------- ----------- ----------------------
109 2108 1480
109 2108 5625

sum
----------------------
7105

sum avg
---------------------- ----------------------
7105 3552.5

rep cust amount
----------- ----------- ----------------------
110 2107 632
110 2107 22500

sum
----------------------
23132

sum avg
---------------------- ----------------------
23132 11566


(57 row(s) affected)
landonmkelsey is offline   Reply With Quote
Old 10-08-2008, 11:19 AM   #6 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 75
landonmkelsey is on a distinguished road
I do not believe in huge or complicated SQL statements.

I worked somewhere (Sierra Design in Reno) where there was a HUGE SQL command...pages and pages!

There was some small but effective bug (if you could call it that) concerning some sql code returning a NULL.

Talk about obfuscation and convoluted code!

Solution: use a stored procedue where the code is program not SQL!

Whew!
landonmkelsey is offline   Reply With Quote
Old 10-09-2008, 10:38 AM   #7 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 741
DJMaze is on a distinguished road
Doing the sum and avg can also be done outside the query in any programming language.
The only drawback is that you must order the data and check for rep and cust
Code:
int current_rep = 0, current_cust = 0, count_rep=0, count_cust=0, sum=0, avg=0;
while (query_result->row)
{
    if (current_cust != cust)
    {
        sum/count_cust;
        sum = count_cust = 0;
    }
    if (current_rep != rep)
    {
        avg/count_rep;
        avg = count_rep = 0;
    }
    avg += row.sum;
    sum += row.sum;
    ++count_rep;
    ++count_cust;
}
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid Suffix "n" on integer constant error Sarkoff Standard C, C++ 1 09-20-2007 03:16 PM
"Enter Network Password" when trying to send email through IE dazz williams Windows 0 05-16-2007 11:46 AM
"fo:flow must contain block-level children" doesnt specify which tag gompol Java 2 03-08-2005 11:14 AM
cant get "ZZZZZ" to terminate reading from text file...help lukmanpatel Standard C, C++ 1 02-14-2005 10:33 PM
Just bought "Advanced PHP Programming" by George Schlossnagle JeC PHP 2 06-23-2004 07:12 AM


All times are GMT -8. The time now is 03:21 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting