473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating total amounts per clientid

MT
Hi,

I have an invoicing system using postgresql. Each time a customer makes a purchase, a new record is inserted into the "cart" table. The "cart" table, among other things, contains a customer_id field and a total for the amount purchased on the particular invoice. Now what I'd like to do is to calculate the total invoices for each customer in a given time interval, then list those customers in descending order from most paying to least paying. Could someone give me some pointers on how to construct an sql statement that would flush out this data.

Thanks for your help,

Mark

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #1
2 2644
El Sáb 13 Dic 2003 16:21, MT escribió:
Hi,

I have an invoicing system using postgresql. Each time a customer makes a

purchase, a new record is inserted into the "cart" table. The "cart" table,
among other things, contains a customer_id field and a total for the amount
purchased on the particular invoice. Now what I'd like to do is to calculate
the total invoices for each customer in a given time interval, then list
those customers in descending order from most paying to least paying. Could
someone give me some pointers on how to construct an sql statement that would
flush out this data.
First, lets see how to get the info you want:
SELECT sum(invoice) AS total, customer_id FROM cart
GROUP BY cuotomer_id;

Now you can order it by the total column, and maybe give it a LIMIT, in case
you have to many costumers. :-)

--
select 'mmarques' || '@' || 'unl.edu.ar' AS email;
-----------------------------------------------------------------
Martín Marqués | mm******@unl.ed u.ar
Programador, Administrador, DBA | Centro de Telemática
Universidad Nacional
del Litoral
-----------------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #2
MT
Thanks for responding. That was the tip I needed.

Regards,

Mark

On Sat, 13 Dec 2003 16:36:32 -0300
Martin Marques <ma****@bugs.un l.edu.ar> wrote:
El Sáb 13 Dic 2003 16:21, MT escribió:
Hi,

I have an invoicing system using postgresql. Each time a customer makes a

purchase, a new record is inserted into the "cart" table. The "cart" table,
among other things, contains a customer_id field and a total for the amount
purchased on the particular invoice. Now what I'd like to do is to calculate
the total invoices for each customer in a given time interval, then list
those customers in descending order from most paying to least paying. Could
someone give me some pointers on how to construct an sql statement that would
flush out this data.
First, lets see how to get the info you want:
SELECT sum(invoice) AS total, customer_id FROM cart
GROUP BY cuotomer_id;

Now you can order it by the total column, and maybe give it a LIMIT, in case
you have to many costumers. :-)

--
select 'mmarques' || '@' || 'unl.edu.ar' AS email;
-----------------------------------------------------------------
Martín Marqués | mm******@unl.ed u.ar
Programador, Administrador, DBA | Centro de Telemática
Universidad Nacional
del Litoral
-----------------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #3

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

Similar topics

4
4867
by: The Bit Bandit | last post by:
Hopefully someone can help me create a query that I'm having some trouble with. I have three tables: invoices, invoicedetails, invoicepayments The fields are: invoices -------- InvoiceNo
4
4146
by: jeff brubaker | last post by:
Hello, Currently we have a database, and it is our desire for it to be able to store millions of records. The data in the table can be divided up by client, and it stores nothing but about 7 integers. | table | | id | clientId | int1 | int2 | int 3 | ... | Right now, our benchmarks indicate a drastic increase in performance if we divide the data into different tables. For example,...
9
1498
by: Abby Lee | last post by:
http://www.ncsa.uiuc.edu/Divisions/Admin/reimb.asp There is just too much code to place here. onChange you trigger a function to add the numbers in the the column you entered an amount in (scripts have been made for only the first 2 columns). The information is passed to a second function that adds all the numbers in the row you are in and puts a total at the end. This part works great unless you place an "8", "88", "888", etc. in the...
2
5648
by: phaddock4 | last post by:
Please help. GOAL: I hope to calculate a total amount in a TotalQuery (or field in the SubQuery?), based upon three field amounts in a SubQuery. STATUS: When I create an expression in the TotalQuery based upon SubQuery fields, e.g: Total: + +
2
2330
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. There are also several other subreports in the main report's footer. The rptTrustDetails subreport has two grouping levels MasterClientID and ClientID, with headers and footers for each grouping level. In the ClientID footer, I have an...
9
11295
parshupooja
by: parshupooja | last post by:
Hi I am using asp.net C# and javascript. I have a gridview with 5 columns and 14 row column1--column2--column3--column4--column5 Column1 to column4 is textboxes wheresa column5 is label I am trying to calculate column 5 which is (column2-column1)+(column4-column3) Here is javascript i am using. When i use this it just calculates for first row of gridview. <script language =javascript type="text/javascript">
2
1590
by: dablyz | last post by:
Hi! I was wondering if I could get some more excellent advice from this site. I'm designing a project that ultimately, among other functions, calculates a download time based upon a file size (which is loaded at run time by the user), a specified percentile of compression (selected by the user - 0-10%) and a download rate numerical up down box (also user specified - 1-10kb/s). I figured that this just might be a glorified version of a VAT...
28
3660
by: CindySue | last post by:
I found something similar, but can't get it to work--maybe because I'm working in a report rather than a query? I have a main report that has three subreports in it. Each subreport has a total for items in that subreport. If all three have entries, I can get a grand total, but if one or more doesn't, it just says error. I first tried using IIf/IsNull, but again, I get totals only when all three subreports have amounts. I then found nz, and...
2
1679
by: tony.stevens5 | last post by:
Hi Apologies if this is not in the right area but first time for posting here. I know how to add up amounts on a form to create a total for a line but the database i am working on needs the total to be unique for each row i put amounts in. What is happening is that when i put in amounts in row 2 then it adds that along with row 1...etc etc.
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.