473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

"Complicated" SQL query

Hello all-

Fairly new to SQL and I need to issue a pretty complex query (complex
being a relative term here :) ).

To dumb down my example for display purposes, I have two tables in my
schema

a Customer Table :

ID // unique primary key
Firstname
Lastname

and an Invoices table

ID // unique primary key
CustomerId // foreign key related to customer table
Total Price
There can be many Invoices per customer.

What I need to know, is how can I keep a running sum of 'Total Price'
for each invoice PER customer? My ultimate goal is to display the
results on a webpage in the following format :
CustomerName InvoiceNum TotalPrice
1. Joe Blow 1 10.00
2 15.00
3 20.00
-----
Total 45.00

2. Sally Fields 4 5.00
5 2.00
----
Total 7.00
....
....

I would prefer to tally the total using the mysql query, not to do a
running total as I display the results to the web page. I know
there's a GROUP BY clause that can be used in my query, but I'm not
quite sure how to apply it.

Any help would be greatly appreciated!

Sincerely,
Chris
Jul 20 '05 #1
3 6145

SELECT concat(cus.firstname,' ',cus.lastname), inv.ID, inv.TotalPrice,
(SELECT SUM(TotalPrice) FROM Invoices inv2
WHERE inv2.CustomerID=cus.ID
AND inv2.ID <= inv.ID) runningTotal
FROM Customer cus,
Invoices inv
WHERE cus.ID=inv.CustomerID

"Chris" <c_*******@hotmail.com> wrote in message
news:cf**************************@posting.google.c om...
Hello all-

Fairly new to SQL and I need to issue a pretty complex query (complex
being a relative term here :) ).

To dumb down my example for display purposes, I have two tables in my
schema

a Customer Table :

ID // unique primary key
Firstname
Lastname

and an Invoices table

ID // unique primary key
CustomerId // foreign key related to customer table
Total Price
There can be many Invoices per customer.

What I need to know, is how can I keep a running sum of 'Total Price'
for each invoice PER customer? My ultimate goal is to display the
results on a webpage in the following format :
CustomerName InvoiceNum TotalPrice
1. Joe Blow 1 10.00
2 15.00
3 20.00
-----
Total 45.00

2. Sally Fields 4 5.00
5 2.00
----
Total 7.00
...
...

I would prefer to tally the total using the mysql query, not to do a
running total as I display the results to the web page. I know
there's a GROUP BY clause that can be used in my query, but I'm not
quite sure how to apply it.

Any help would be greatly appreciated!

Sincerely,
Chris


Jul 20 '05 #2
Chris wrote:
What I need to know, is how can I keep a running sum of 'Total Price'
for each invoice PER customer?


select c.id, sum(i.totalprice)
from customer c inner join invoices i on c.id = i.customerid

Then loop through each row returned from that query, and run this one
within the loop:

select i.invoicenum, i.totalprice
from invoices i
where i.customerid = ?

and pass the id from the first query as a parameter.

Regards,
Bill K.
Jul 20 '05 #3
Worked perfect!

Thanks for the help Bill!

Bill Karwin <bi**@karwin.com> wrote in message news:<ce*********@enews1.newsguy.com>...
Chris wrote:
What I need to know, is how can I keep a running sum of 'Total Price'
for each invoice PER customer?


select c.id, sum(i.totalprice)
from customer c inner join invoices i on c.id = i.customerid

Then loop through each row returned from that query, and run this one
within the loop:

select i.invoicenum, i.totalprice
from invoices i
where i.customerid = ?

and pass the id from the first query as a parameter.

Regards,
Bill K.

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Alan McIntyre | last post by:
Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my...
3
by: Chris | last post by:
Hello all- Fairly new to SQL and I need to issue a pretty complex query (complex being a relative term here :) ). To dumb down my example for display purposes, I have two tables in my schema...
10
by: Jay Chan | last post by:
I keep getting the following error message when I run a serie of SQL commands: Server: Msg 9002, Level 17, State 6, Line 15 The log file for database 'tempdb' is full. Back up the transaction...
9
by: dover | last post by:
For the code, outputfile << *p << endl; Someone suggests: Don't use endl here; it is flushing output stream every time. Use plain '\n'. What's the difference of using "endl" and "\n" above?...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
1
by: Niranjan | last post by:
I have a relatively straight forward report. The query that populates data for this report is complicated. The report opens without any problem and I can print it as well. However, If I have...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
12
by: M G Henry | last post by:
I have a tabbed form that contains 12 different "pages" and when I try and run the form I get the error message too many fields defined --- which I believe is the 255 field limit in the record...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.