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

Silly query question

How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
Jul 26 '06 #1
6 1226
Bob Stearns wrote:
How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
.
.
.
primary key(invoice_no))
create table details
invoice_no integer not null
line_no smallint not null
item_no integer not null
qty smallint not null
each decimal(6,2) not null
.
.
.
primary key(invoice_no, line_no))

select a.invoice_no, a.whatever_other_invoice_data_you_need,
b.amount_due
from invoices a,
( select invoice_no, sum( each * qty ) as amount_due
from details
group by invoice_no ) b
where a.invoice_no = b.invoice_no
;

Jul 27 '06 #2
Hi, Bob:

Assuming you want the sums grouped by all columns in the select list
(i.e. sum by invoice by customer by created...), there's the
conventional query:

SELECT
I.INVOICE_NO,
<OTHER COLUMNS IF NEEDED>,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
INVOICES I,
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO,
<OTHER COLUMNS IF NEEDED>

Assuming you wanted all invoice data, and the sum at the invoice number
(only) level along for the ride:

SELECT
I.*,
INV_AGGR.AMT_DUE
FROM
INVOICES I,
TABLE
(
SELECT
I.INVOICE_NO INV_NO,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO
) INV_AGGR
WHERE
INV_AGGR.INV_NO = I.INVOICE_NO

Here's what I got, using your structure and my dummy data:

WITH INVOICES
(
INVOICE_NO,
CUST_NO,
CREATED,
PAID
)
AS (
VALUES
(1,1,'06/26/2006','07/26/2006'),
(2,2,'05/26/2006','06/26/2006')
),
DETAILS
(
INVOICE_NO,
LINE_NO,
ITEM_NO,
QTY,
EACH)
AS
(
VALUES
(1,1,435,10,25.00),
(1,2,890,25,.10),
(2,1,2345,1000,45.24),
(2,2,67657,45000,.02)
)
SELECT
I.*,
INV_AGGR.AMT_DUE
FROM
INVOICES I,
TABLE
(
SELECT
I.INVOICE_NO INV_NO,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO
) INV_AGGR
WHERE
INV_AGGR.INV_NO = I.INVOICE_NO;

INVOICE_NO CUST_NO CREATED PAID AMT_DUE

----------- ----------- ---------- ----------
---------------------------------
1 1 06/26/2006 07/26/2006
252.50
2 2 05/26/2006 06/26/2006
46140.00

2 record(s) selected.

Let me know if I've misinterpreted your requirements.

HTH,

--Jeff
Bob Stearns wrote:
How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
.
.
.
primary key(invoice_no))
create table details
invoice_no integer not null
line_no smallint not null
item_no integer not null
qty smallint not null
each decimal(6,2) not null
.
.
.
primary key(invoice_no, line_no))
Jul 27 '06 #3
jefftyzzer wrote:
Hi, Bob:

Assuming you want the sums grouped by all columns in the select list
(i.e. sum by invoice by customer by created...), there's the
conventional query:

SELECT
I.INVOICE_NO,
<OTHER COLUMNS IF NEEDED>,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
INVOICES I,
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO,
<OTHER COLUMNS IF NEEDED>

Assuming you wanted all invoice data, and the sum at the invoice number
(only) level along for the ride:

SELECT
I.*,
INV_AGGR.AMT_DUE
FROM
INVOICES I,
TABLE
(
SELECT
I.INVOICE_NO INV_NO,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO
) INV_AGGR
WHERE
INV_AGGR.INV_NO = I.INVOICE_NO

Here's what I got, using your structure and my dummy data:

WITH INVOICES
(
INVOICE_NO,
CUST_NO,
CREATED,
PAID
)
AS (
VALUES
(1,1,'06/26/2006','07/26/2006'),
(2,2,'05/26/2006','06/26/2006')
),
DETAILS
(
INVOICE_NO,
LINE_NO,
ITEM_NO,
QTY,
EACH)
AS
(
VALUES
(1,1,435,10,25.00),
(1,2,890,25,.10),
(2,1,2345,1000,45.24),
(2,2,67657,45000,.02)
)
SELECT
I.*,
INV_AGGR.AMT_DUE
FROM
INVOICES I,
TABLE
(
SELECT
I.INVOICE_NO INV_NO,
SUM(D.QTY*D.EACH) AMT_DUE
FROM
DETAILS D
WHERE
D.INVOICE_NO = I.INVOICE_NO
GROUP BY
I.INVOICE_NO
) INV_AGGR
WHERE
INV_AGGR.INV_NO = I.INVOICE_NO;

INVOICE_NO CUST_NO CREATED PAID AMT_DUE

----------- ----------- ---------- ----------
---------------------------------
1 1 06/26/2006 07/26/2006
252.50
2 2 05/26/2006 06/26/2006
46140.00

2 record(s) selected.

Let me know if I've misinterpreted your requirements.

HTH,

--Jeff
Bob Stearns wrote:
>>How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
.
.
.
primary key(invoice_no))
create table details
invoice_no integer not null
line_no smallint not null
item_no integer not null
qty smallint not null
each decimal(6,2) not null
.
.
.
primary key(invoice_no, line_no))

You got it exactly right. I have to map it into my case where I have two
different types of detail rows, one of which might not exist for any
particular invoice, so my FROM is extended with a LEFT OUTER JOIN TABLE
(SELECT ...) INV_AGGR2.

While having dinner and reading my latest SF book I came up with a
similar solution using WITH to generate the two tables of SUMs. If I
work out the details of that solution, I will post it.
Jul 27 '06 #4
Private Pyle wrote:
Bob Stearns wrote:
>>How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
.
.
.
primary key(invoice_no))
create table details
invoice_no integer not null
line_no smallint not null
item_no integer not null
qty smallint not null
each decimal(6,2) not null
.
.
.
primary key(invoice_no, line_no))

select a.invoice_no, a.whatever_other_invoice_data_you_need,
b.amount_due
from invoices a,
( select invoice_no, sum( each * qty ) as amount_due
from details
group by invoice_no ) b
where a.invoice_no = b.invoice_no
;
How would this work if I had two types of details, one optional? Is the
comma form of the FROM equivalent to a LEFT OUTER JOIN or a JOIN?
Jul 27 '06 #5
Bob Stearns wrote:
>select a.invoice_no, a.whatever_other_invoice_data_you_need,
b.amount_due
from invoices a,
( select invoice_no, sum( each * qty ) as amount_due
from details
group by invoice_no ) b
where a.invoice_no = b.invoice_no
;
How would this work if I had two types of details, one optional? Is the
comma form of the FROM equivalent to a LEFT OUTER JOIN or a JOIN?
The comma performs a cross join (with the join condition placed in the WHERE
clause).

You could also combine the two detail tables in the subselect:

SELECT ...
FROM invoices AS a JOIN
( SELECT d1.invoice_no,
COALESCE(SUM( d1.each * d1.qty ), SUM( d2.each * d2.qty ))
FROM details AS d1 LEFT OUTER JOIN details2 AS d2 ON ...
GROUP BY ... ) AS b ON ...

It depends on your join criteria for the details tables if you can do that.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jul 27 '06 #6

Bob Stearns wrote:
How do I (syntax please) write a query which returns the invoice header
data and the amount due which is the SUM(qty*each) from the details rows
with the same invoice number for all invoices in the header table?

create table invoices(
invoice_no integer not null
cust_no integer not null
created timestamp not null
paid timestamp not null
.
.
.
primary key(invoice_no))
create table details
invoice_no integer not null
line_no smallint not null
item_no integer not null
qty smallint not null
each decimal(6,2) not null
.
.
.
primary key(invoice_no, line_no))
Either a join, or a sub-select should do it.

SELECT
invoice_no,
cust_no,
created,
paid,
(
SELECT
SUM(qty * each)
FROM
details
WHERE
details.invoice_no = invoices.invoice_no
) Bill_Amount
FROM
invoices
B.

Jul 27 '06 #7

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

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
2
by: michaaal | last post by:
I feel a bit silly asking this because I use this code all the time, but what does the ",,129" mean? Are there other parameters that one might use (I only do fairly simple SQL commands). ...
10
by: Mr. B | last post by:
I've been seeking the solution to this. But can't figure it out (sounds simple enough). When you Populat a ComboBox, you can select the initial displayed items on startup simply by setting the...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
1
by: Alberto A. Villa | last post by:
Dear SQL MS-Access Programmers, can You help me? I must append a table in MS-Access 2000 to another, because the new table has new data with same formats imported from an excel file, that...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
6
by: jeddiki | last post by:
Hi, I am stuck again over something silly that I can not resolve. I am using a link to create a dynamic page. the link is itself written from the same same table that the dynamic page is...
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: 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
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
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...
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
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...
0
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...
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,...

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.