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

Alternate values

I have no idea where to look for this.

Database about wines.
One table is `product` (one particular wine from one particular estate)
with a link to a second table `appellation` (legal classification).
Another table (say `bottle`) links to one `product` and has additional
fields, most notably the vintage.

Now one problem is that some years, the same product might be refused
the usual category (decision of a tasting commission).
Another possibility is that the appellation might change at some point
in time (creation of a new category, or upgrade or downgrade).
And finally, the name of the product itself might change for some
year(s) (e.g. Cuvée Tchernobyl for the 1986 vintage).

Still, in all these cases, I want to look at all `bottle` items as one
single product, but display the correct appellation. And I want the
link to `appellation` to be at the `product` level, not `bottle`.

How can I manage to get the right category when I create a new `bottle`
item for an existing product? Or should I just forget about it?

Thanks in advance
--
Eric Lafontaine

Feb 9 '06 #1
5 1326
"ventre-à-pattes" <ne**@ventre-a-pattes.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
One table is `product` (one particular wine from one particular estate)
with a link to a second table `appellation` (legal classification).
Another table (say `bottle`) links to one `product` and has additional
fields, most notably the vintage.

[description of exception cases]

Well, one solution would be to introduce a new table in between bottle and
product. Maybe call it `product_year` or something. This table would store
multiple rows that each reference a given product, and differ in the year
and in the other attributes that may vary per year, like the appellation and
the product name. The problem with this solution is that there's a lot of
duplication, because I assume the exceptions occur more or less rarely.

A different solution would be to create a new table similar to the one
above, but store in it only records for the exception cases. Most years
there would be no record in this new table. Perhaps call it
`product_exception`.

You can use an outer join and the COALESCE function to apply these
exceptions to override the value in the product table. COALESCE returns the
first non-NULL argument, so in cases where there is no matching record in
the exception table, it uses the default product name and appellation in the
`product` table.

SELECT b.year, COALESCE(pe.product_name, p.product_name),
COALESCE(pe.product_appellation, p.product_appellation),
FROM bottle AS b
INNER JOIN product AS p
ON b.product_id = p.product_id
LEFT OUTER JOIN product_exception AS pe
ON b.year = pe.year AND p.product_id = pe.product_id

Regards,
Bill K.
Feb 9 '06 #2

Bill Karwin wrote:
[...]
You can use an outer join and the COALESCE function to apply these
exceptions to override the value in the product table. COALESCE returns the
first non-NULL argument, so in cases where there is no matching record in
the exception table, it uses the default product name and appellation in the
`product` table.


Thanks for the pointer, I'll give it a try
--
Eric Lafontaine

Feb 9 '06 #3

Bill Karwin wrote:
[...]

Since you have been so kind...
...COALESCE...


This seems to work fine for individual exceptions.
Now how can I proceed for the following case: SomeWine belonged to
appellation Old until some date, and then changed to appellation New.
I am much more likely to purchase recent vintages, so New would be my
default.

Is there any way to specify a range of vintages without having
somewhere a record for each of them? And of course I'd like it to be in
the database itself...
--
Eric Lafontaine

Feb 9 '06 #4
"ventre-à-pattes" <ne**@ventre-a-pattes.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Now how can I proceed for the following case: SomeWine belonged to
appellation Old until some date, and then changed to appellation New.
I am much more likely to purchase recent vintages, so New would be my
default.

Is there any way to specify a range of vintages without having
somewhere a record for each of them? And of course I'd like it to be in
the database itself...


This is getting to be more complex. One could change the product_exception
table I described before by listing a year_start and a year_end, and then do
your queries as follows:

SELECT b.year, COALESCE(pe.product_name, p.product_name),
COALESCE(pe.product_appellation, p.product_appellation)
FROM bottle AS b
INNER JOIN product AS p
ON b.product_id = p.product_id
LEFT OUTER JOIN product_exception AS pe
ON (b.year BETWEEN pe.year_start AND pe.year_end) AND p.product_id =
pe.product_id

But what to list for year_end if the product has changed? Maybe move the
old default values into a record in the exception table, because for those,
we do know an end date. Then change the default values in the product
table.

But this is not a comfortable design. If another programmer comes to
maintain the system two years from now, he or she will be confused.

We might as well take the changeable properties out of the product table
altogether, and force _all_ product names and appellations to be looked up
in the second table. This table is no longer for rare exceptions, it stores
year-dependent attributes for all years.

SELECT b.year, py.product_name, py.product_appellation
FROM bottle AS b
INNER JOIN product AS p
ON b.product_id = p.product_id
INNER JOIN product_year_attribute AS py
ON (b.year BETWEEN py.year_start AND py.year_end
OR b.year >= py.year_start AND py.year_end IS NULL)
AND p.product_id = py.product_id

I have an additional join condition term in there, to support a state where
year_end is NULL. This indicates the state where the end date is not known,
i.e. it's the current and ongoing name and appellation for the wine.

Another caveat: there's no way in this design for the database to enforce
that there are no overlaps. If you put in a couple of records with
overlapping periods, you'll get multiple records back. If you have a year
with an exception case, you'd have to split the record for the default
attributes into two, one ending before the exception year, and the second
beginning after the exception year.

You could also combine this structure with the exception-case table and
COALESCE that we discussed before. That would allow you to have single-year
exceptions without having to split long durations of "default" attributes
into multiple records.

SELECT b.year, COALESCE(pe.product_name, py.product_name),
COALESCE(pe.product_appellation, py.product_appellation)
FROM bottle AS b
INNER JOIN product AS p
ON b.product_id = p.product_id
INNER JOIN product_year_attribute AS py
ON (b.year BETWEEN py.year_start AND py.year_end
OR b.year >= py.year_start AND py.year_end IS NULL)
AND p.product_id = py.product_id
LEFT OUTER JOIN product_exception AS pe
ON b.year = pe.year AND p.product_id = pe.product_id

One thing I've learned about modelling real-world industries with software:
in the real world, exception cases are common, and they're not very
expensive to track. Humans are pretty good at visualizing exception cases.
In software, exception cases are bothersome because they necessarily force
complexity into _all_ your computations.

Regards,
Bill K.
Feb 9 '06 #5

Bill Karwin wrote:
This is getting to be more complex. [...] One thing I've learned about modelling real-world industries with software:
in the real world, exception cases are common, and they're not very
expensive to track. Humans are pretty good at visualizing exception cases.
In software, exception cases are bothersome because they necessarily force
complexity into _all_ your computations.


So maybe I will try to stick to the KISS rule and just add one
`exception` field in my `bottle` table that would be null by default,
and use COALESCE.
Thanks again for making me aware of that function.
--
Eric Lafontaine

Feb 10 '06 #6

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

Similar topics

9
by: madsgormlarsen | last post by:
Hi I am making a html table builder, and would like every second row to be blue. So for that purpose I need a number that changes on every iteration of a while loop, for exampel so that a value...
13
by: Toby A Inkster | last post by:
www.authoring.stylesheets] For ages I have provided links to alternate stylesheets from my pages as per W3C recommendations: <link rel="stylesheet" href="baz" type="text/css" title="Baz"...
5
by: kjaggi | last post by:
-- tested schema below -- -- create tables -- create table tbl_test (serialnumber char(12)) go create table tbl_test2 (serialnumber char(12), exportedflag int) go --insert data --
1
by: Carl Draus | last post by:
I was reading the FixPackReadme.txt file in preparation for installing v8.1 Alternate FP7. The prerequisites section says that all DB2 processes must be stopped and doesn't differentiate between...
4
by: Mark A | last post by:
Trying to upgrade alternate FP9 to alternate FP9a on Red Hat Linux. After running the following command: ../installAltFixPak -y it says it was successful, but no new directory is created in...
1
by: Eric Lindsay | last post by:
I am trying to understand the differences between and uses of persistent, default and alternate styles. I have read http://www.w3.org/TR/REC-html40/present/styles.html section 14.3.2 on...
5
by: Michael R | last post by:
Searching the net I've found a simple technique to add row numbers and alternate colors (for the even and the uneven row) to a continuous form. 1st step: Create a textbox, send it to background...
4
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I need to post a form to an alternate technology. I have created pop up windows that on post back will open a new window, but this uses get. I need to encode variables and POST to a new window on...
9
by: johkar | last post by:
I need some browser implementation clarification. In the below example, the alternate stylesheet could be invoked by user agents that support alternate stylesheets or by script. Are there any...
10
by: Debajit Adhikary | last post by:
I'm writing this little Python program which will pull values from a database and generate some XHTML. I'm generating a <tablewhere I would like the alternate <tr>'s to be <tr class="Even">...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.