![]() | |
#11
| |||
| |||
|
|
Pif <nos... (AT) laposte (DOT) net> writes: here is a short description of the schema : I don't use Oracle, but your question doesn't seem to have anything Oracle-specific. I'll answer using PostgreSQL. - I've orders with an amount pif_orders=> CREATE TABLE stock_order ( * * * * code VARCHAR NOT NULL, * * * * amount NUMERIC(10,2) NOT NULL, * * * * PRIMARY KEY (code) * * * * ); pif_orders=> INSERT INTO stock_order (code, amount) VALUES * * * * ('A', 100.00), * * * * ('B', 200.00), * * * * ('C', 120.00); - An order can be associated with several deliveries, each one having also an amount (a delivery has a FK referencing the order). pif_orders=> CREATE TABLE stock_delivery ( * * * * order_code VARCHAR NOT NULL * * * * * * REFERENCES stock_order (code), * * * * sequence INTEGER NOT NULL, * * * * amount NUMERIC(10,2), * * * * PRIMARY KEY (order_code, sequence) * * * * ); pif_orders=> INSERT INTO stock_delivery (order_code, sequence, amount) * * * * VALUES * * * * ('A', 1, 50.00), * * * * ('A', 2, 30.00), * * * * ('A', 3, 20.00), * * * * ('B', 1, 200.00), * * * * ('C', 1, 70.00), * * * * ('C', 2, 50.00); I would like to produce following result that joins both tables without repeating order amounts : Order A * *$100 * * * Develiery1 * *$50 * * * * * * * * * * * Develiery2 * *$30 * * * * * * * * * * * Develiery3 * *$20 Order B * *$200 * * * Develiery4 * *$200 Order C * *$120 * * * Develiery5 * *$70 * * * * * * * * * * * Develiery6 * *$50 That's not a relation (some tuples are incomplete), so it's not something you should expect the relational DBMS to produce. Sometimes a particular DBMS product might have something that can be distorted to do that kind of thing, but it will always be limited and usually lead to frustration since it's not the role of the DBMS to produce generic reports. Instead, use a SELECT to produce the relation: pif_orders=> SELECT DISTINCT * * * * order_code, * * * * order_amount, * * * * delivery_sequence, * * * * delivery_amount * * FROM * * * * ( * * * * * * SELECT * * * * * * * * code AS order_code, * * * * * * * * amount AS order_amount * * * * * * FROM stock_order * * * * * * ) AS o * * * * NATURAL JOIN * * * * ( * * * * * * SELECT * * * * * * * * order_code, * * * * * * * * sequence AS delivery_sequence, * * * * * * * * amount AS delivery_amount * * * * * * FROM stock_delivery * * * * * * ) AS d * * ORDER BY order_code, delivery_sequence; *order_code | order_amount | delivery_sequence | delivery_amount ------------+--------------+-------------------+----------------- *A * * * * *| * * * 100.00 | * * * * * * * * 1 | * * * * * 50.00 *A * * * * *| * * * 100.00 | * * * * * * * * 2 | * * * * * 30.00 *A * * * * *| * * * 100.00 | * * * * * * * * 3 | * * * * * 20.00 *B * * * * *| * * * 200.00 | * * * * * * * * 1 | * * * * *200.00 *C * * * * *| * * * 120.00 | * * * * * * * * 1 | * * * * * 70.00 *C * * * * *| * * * 120.00 | * * * * * * * * 2 | * * * * * 50.00 (6 rows) and then use a function or user application to process the relation into whatever output text you like for the report. Turning relations into reports isn't the job of the DBMS. -- *\ * * *It's up to the masses to distribute [music] however they want | * `\ * * The laws don't matter at that point. People sharing music in | _o__) * * * *their bedrooms is the new radio. Neil Young, 2008-05-06 | Ben Finney |
#12
| |||
| |||
|
|
Yet it does have something Oracle-specfic: the ability, through SQL*Plus, to suppress repeating output, and analytic functions to compute the sum in-line so the stock_order table isn't necessary (columns are renamed, as necessary, to remove reserved words): |
#13
| |||||||
| |||||||
|
|
On Nov 27, 3:12*pm, Ben Finney <bignose+hates-s... (AT) benfinney (DOT) id.au wrote: and then use a function or user application to process the relation into whatever output text you like for the report. Turning relations into reports isn't the job of the DBMS. So what is the job of the DBMS? |
|
From wikipedia (just 'cause it was the first thing that popped up in google, you are welcome to cite others): |
|
"A Database Management System (DBMS) is a set of computer programs that controls the creation, maintenance, and the use of the database with computer as a platform or of an organization and its end users." |
|
Turning relations into reports may or may not be the job of the database |
|
but by definition it quite well can be the job of the Database Management System. |
|
There are some things apps may do better, and some things the DBMS may do better. Which things may be debatable, but saying either should do all or neither is wrong. |
|
Can't a function be part of the DBMS? |
#14
| ||||
| ||||
|
|
But you have a good point about the stock_order table not being necessary: […] |
|
Runs on PostgreSQL and DB2 just as well. |
|
| 2 | 30.00 | 3 | 20.00 B | 200.00 | 1 | 200.00 |
|
| 2 | 50.00 (6 rows) |
#15
| |||
| |||
|
|
joel garry <joel-garry (AT) home (DOT) com> writes: |
|
Can't a function be part of the DBMS? A non-relational function shouldn't be part of a relational DBMS, no. An application can *store* its non-relational functions in the *database*, of course, and access them there; but database ? DBMS. |
#16
| |||
| |||
|
|
On Tue, 01 Dec 2009 08:50:34 +1100, Ben Finney bignose+hates-spam (AT) benfinney (DOT) id.au> wrote: joel garry <joel-garry (AT) home (DOT) com> writes: [snip] Can't a function be part of the DBMS? A non-relational function shouldn't be part of a relational DBMS, no. |
|
Addition of integral values is a function mapping two integral values to an integral value. |
#17
| |||
| |||
|
|
Gene Wirchenko <genew (AT) ocis (DOT) net> writes: On Tue, 01 Dec 2009 08:50:34 +1100, Ben Finney bignose+hates-spam (AT) benfinney (DOT) id.au> wrote: joel garry <joel-garry (AT) home (DOT) com> writes: [snip] Can't a function be part of the DBMS? A non-relational function shouldn't be part of a relational DBMS, no. This was far too broad, I now see. Originally it was in the context of a *query* returning a non-relational result, which is really as far as I should have taken it. |
|
Addition of integral values is a function mapping two integral values to an integral value. Right; of course, there are heaps of functions operating on attribute values that can be used *within* relational operations to modify the relation that will be returned. Such functions definitely belong as part of the relational DBMS. What I was trying to express was that relational operations like the various relational operations that SELECT implements should only return data as relations (they might also return status responses). They should never return non-relation data. |
|
Transforming relational data into a non-relation is not the job of the relational DBMS, but the job of applications that receive relational data from the DBMS. |
#18
| |||
| |||
|
|
On Tue, 01 Dec 2009 13:45:44 +1100, Ben Finney bignose+hates-spam (AT) benfinney (DOT) id.au> wrote: What I was trying to express was that relational operations — like the various relational operations that ‘SELECT’ implements — should only return data as relations (they might also return status responses). They should never return non-relation data. A relation does not have order. This would not be a relation select clicode,cliname from clients order by clicode because of the order by clause. |
|
Having to go through a sort stage when the DBMS might well have been able to easily handle it would be counterproductive. |
#19
| |||
| |||
|
|
Interesting. But you've changed it from a relational to a non-relational result (some tuples now contain nulls). |
|
(referencing names that weren't defined). |

#20
| |||
| |||
|
|
Thomas Kellerer <OTPXDAJCS... (AT) spammotel (DOT) com> writes: And even if the "blank" stuff should be in there I don't understand the complicated sub-query you were using ![]() For didactic purposes. I prefer to have attribute names that make sense in the context of the relation; and then use attribute renames in the select so that a natural join will just work. Join conditions give me hives; every join should be natural anyway :-) -- *\ * * * *I fly Air Bizarre. You buy a combination one-way round-trip | * `\ * *ticket. Leave any Monday, and they bring you back the previous | _o__) * * Friday. That way you still have the weekend. Steven Wright | Ben Finney |
![]() |
| Thread Tools | |
| Display Modes | |
| |