473,434 Members | 1,544 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,434 software developers and data experts.

Can/how sql sum returned rows then work out % of total on them?

I am using a propriatetry product like MS reporting services to create an invoice.
Users enter an invoice into the system, specify the customer & invoice lines (each line can be tax exempt or assigned one of several tax codes).
Then they run the print process which queries the invoice header table and invoice detail table and retrieves all matching rows for that order/job number (created when you save your invoice entry).
The returned data is then inserted into the report template and produces a nice pdf invoice, showing customer details at the top, then listing the invoice lines showing tax amount, tax rate & net line total, at the bottom the tax is summed and an invoice total displayed.

Now, this is where my abilities end.

I now need to add a tax rate breakdown which breaks down the invoice by tax code and produces a percentage of the total attributable to that tax code. So that the customer can see how much they are spending in each tax bracket.

eg.

TC_____% of invoice total
0______0
1______27
2______64
3______9

My initial thinking was I could run two queries on the invoice detail table;
1. To return the total invoice amount
2. To return the total invoice amount grouped by tax code

Then to do a calculation which does 2/1 for each tax code returned.

But I can't figure out how to assign names to the grouped tax codes returned in the query AND then feed them into the calculation - if its possible to do with assigned field/column names.

So I would very much appreciate any help you can offer on this.

Is my thinking actually possible to do
OR
Is my approach incorrect.

Thanks,

Mark.
May 19 '10 #1
3 2171
ck9663
2,878 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. ;With SummaryInvoice(TotalInvoice)
  3. as
  4. (
  5. select count(*) from YourInvoiceTable
  6. )
  7. select TC, count(*), (count(*)/s.TotalInvoice) * 100.00
  8. from YourInvoiceTable i
  9. inner join SummaryInvoice s on 1 =1 
  10. group by TC
  11.  
  12.  
Happy Coding!!!

~~ CK
May 20 '10 #2
Hi ck9663,

Thanks for your response, but that code is way too advanced for me without an explanation.

My best guess is that your code creates a temporary table of all invoice data producing some sort of totals count?
Then it attempts the % calcualtion on everything?
And finally returns results only on invoices (invoiceID) also in the header table?

I have a feeling its good but its beyond my abilities (currently) to adapt it into our structure. :(

I will have a go and see if I get any results.
May 21 '10 #3
ck9663
2,878 Expert 2GB
It's called Common Table Expression. It's a way of re-writing sub-queries. If I rewrite that in a sub-query, it'll look something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select TC, count(*), (count(*)/s.TotalInvoice) * 100.00
  3. from YourInvoiceTable i
  4.    inner join (select count(*) cnt from YourInvoiceTable) s on 1 = 1 
  5. group by TC
  6.  
  7.  
Happy Coding!!!

~~ CK
May 21 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris E | last post by:
Hi, I need to print a message when the number of rows returned from repeating groups is > 3. How is this best done? I have two repeating groups filtered to display only 3 rows. When either one...
1
by: beckham | last post by:
I have a large table with approx 250000 rows in sql 2000. I need to return this from an asp page but the query to return this amount of data causes the asp to time out before the query completes. ...
9
by: Wang, Jay | last post by:
I try to group several rows in a table into a div and show/hide them by click on a button somewhere with a javascript link. When clicked, the link will toggle the style of the div section's style...
1
by: RA | last post by:
Hi I want to have a FAQ so that when the user clicks the question the answer will be shown below the questions. How do I do it? I use c# with asp.net. Thanks
4
by: anand | last post by:
Hello Group, How can i obtain the total no of records selected with the help of Datareader class. rowsaffected does not seem to work. Thanks Anand
1
by: david | last post by:
MY purpose is that develop a web form with textboxes to display database contents. In design time, I follow the .Net document and click data binding property field in each TextBox, select the...
3
by: Sonnich | last post by:
Hi! How do I limit the amount of returned rows? Lets say, that I want only the latest 400 rows (of e.g. 4034634 rows) - is there a parameter for that like rownum in Oracle? e.g. select *...
2
by: Daz | last post by:
Hi everyone. I am trying to make a program which handles about 70 different objects, of the same type. The properties are as follows: std::string ItemName int ItemType int Calories int...
2
by: Oystein | last post by:
Hi! I've got a DropDownlist with predefined numbers(5,10,15,20 etc.) which I want to do a postback to the GridView pagesize, so that the user manually can change returned rows. Is there a way to...
8
by: cycleops | last post by:
Can anyone please help? I'm trying to combine 2 or more similar rows into one row, accumulating a field for quantity. E.g.: index, field1, field2, quantity 1, ccc, bbb, 5 2, ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.