473,396 Members | 2,154 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.

UNION to implement default values

before i go flapping my gums and get deeper in alligators, wondering
whether the following makes sense.

i have a database which provides picklists for input. it started out with a
2 column key in the cross-reference table to the values table, call
them company and state. it's a transitive closure. along the way,
naturally, more columns have been added, call them region and division.
so that now, the unique identifier from the cross-reference to the
values would be: company + division + region + state. the transitive
closure just got a whole lot bigger.

my colleagues are complaining. they want to have a "default" row, which
is read, then make an attempt to read on the full key values, which if
it fails, use the previously read default. i don't like this, since the
backend accepts the data blindly, on the assumption that it has been
pre-edited. the pick-list values are modifiable in real-time, so i object
to doing multiple selects to derive these pre-edited values.

they claim, which may be true, that, while possible, there is rarely in
fact any difference among the pick-lists for a given column based upon
the values in the cross-reference key. so, they don't like having to
maintain the TC.

i'm not even sure that the syntax is permited, which (psuedo-codely) i
think is this:

select * from CrossReference as cr where cr.blah = default
join Values vl on cr.vlId = vl.id
UNION
select * from CrossReference as cr2 where cr2.blah = specific
join Values vl2 on cr2.vlId = vl2.id

in most cases, the second select is expected to fail. i can't find any
explanation in my reference material whether one can still get a result
set, nor whether join-union-join is permited.

this seems a reasonable way to implement pick-lists. has anyone done it
this way?

thanks,
robert
Nov 12 '05 #1
4 3591
robert <gn*****@rcn.com> wrote:
before i go flapping my gums and get deeper in alligators, wondering
whether the following makes sense. [...] i'm not even sure that the syntax is permited, which (psuedo-codely) i
think is this:

select * from CrossReference as cr where cr.blah = default
join Values vl on cr.vlId = vl.id
UNION
select * from CrossReference as cr2 where cr2.blah = specific
join Values vl2 on cr2.vlId = vl2.id
You might want to have a look at the syntax for a "subselect" and the "from
clause" in particular. The JOIN itself is syntactically incorrect.

select *
from CrossReference as cr JOIN Values v12 ON cr.vlId = vl.id
where cr.blah = default
UNION
select *
from CrossReference as cr2 join Values vl2 on cr2.vlId = vl2.id
where cr2.blah = specific

in most cases, the second select is expected to fail.


You mean that the second select does not return any row, right? (Fail
implies an error to me, no-row is a success condition.) A union between
something and the empty set is possible and just returns "something" again.

What you have to keep in mind, however, is that set as in SQL are not
ordered. So there is per-definition no "first" or "second" row. If you
want to have a specific order like the rows from the first subselect should
come first, then you will need an ORDER BY clause like this:

select *
from ( select 1, *
from CrossReference as cr JOIN Values v12 ON cr.vlId = vl.id
where cr.blah = default
UNION
select 2, *
from CrossReference as cr2 join Values vl2 on cr2.vlId = vl2.id
where cr2.blah = specific ) AS t(ord, ...)
order by ord

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
Knut Stolze <st****@de.ibm.com> wrote in message news:<bq**********@fsuj29.rz.uni-jena.de>...
robert <gn*****@rcn.com> wrote:
before i go flapping my gums and get deeper in alligators, wondering
whether the following makes sense.

[...]
i'm not even sure that the syntax is permited, which (psuedo-codely) i
think is this:

select * from CrossReference as cr where cr.blah = default
join Values vl on cr.vlId = vl.id
UNION
select * from CrossReference as cr2 where cr2.blah = specific
join Values vl2 on cr2.vlId = vl2.id


You might want to have a look at the syntax for a "subselect" and the "from
clause" in particular. The JOIN itself is syntactically incorrect.

select *
from CrossReference as cr JOIN Values v12 ON cr.vlId = vl.id
where cr.blah = default
UNION
select *
from CrossReference as cr2 join Values vl2 on cr2.vlId = vl2.id
where cr2.blah = specific

in most cases, the second select is expected to fail.


You mean that the second select does not return any row, right? (Fail
implies an error to me, no-row is a success condition.) A union between
something and the empty set is possible and just returns "something" again.

What you have to keep in mind, however, is that set as in SQL are not
ordered. So there is per-definition no "first" or "second" row. If you
want to have a specific order like the rows from the first subselect should
come first, then you will need an ORDER BY clause like this:

select *
from ( select 1, *
from CrossReference as cr JOIN Values v12 ON cr.vlId = vl.id
where cr.blah = default
UNION
select 2, *
from CrossReference as cr2 join Values vl2 on cr2.vlId = vl2.id
where cr2.blah = specific ) AS t(ord, ...)
order by ord


dang. i always get the where clause out of place. compiler does remind me,
though. and by error, SQL-100 was the issue. that isn't an issue. i'm
still puzzled why this technique isn't in any of my "standard" texts,
which made/makes me nervous that there is some syntactic or logical
hidey-hole into which i'll fall. it would seem to be a fairly common
application.

if Mr. Celko is lurking: would this be a useful solution to the TC
sections? <G>

thanks,

robert
Nov 12 '05 #3
>> I have a database which provides picklists for input ... the unique
identifier from the cross-reference to the values would be: company +
division + region + state. <<

Have you looked at using a nested sets model for the hierarchy?
Nov 12 '05 #4
jo*******@northface.edu (--CELKO--) wrote in message news:<a2**************************@posting.google. com>...
I have a database which provides picklists for input ... the unique

identifier from the cross-reference to the values would be: company +
division + region + state. <<

Have you looked at using a nested sets model for the hierarchy?


yes...but my colleagues haven't been able to digest adjaceny list, and
would rather just write lots of separate queries. life is still seen
through the lens of the static copybook; occurs and redefines literally
implemented in the datastore (it's an insult to say DataBase).
it's a struggle.
Nov 12 '05 #5

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

Similar topics

0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
2
by: Jeff Massung | last post by:
I am having a syntax issue that I hope someone else here knows how to rectify... I am loading an INI file and have a simple function to load values from it. The function is overloaded with the...
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
7
by: urban.widmark | last post by:
Hello We are having some problems with triggers, sequences and union all in V8 on code that worked fine in V7. Was wondering if someone else has seen this and/or knows what to do. A trigger...
25
by: balasam | last post by:
I want what is the application of union in C.Please explain
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
7
by: KoliPoki | last post by:
Hello every body. I have a small issue. Problem: I have a table with 4 descriptor columns (type). I need to formulate a query to retrieve a count for each type so I can group by...etc. The...
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
8
by: zacariaz | last post by:
typedef unsigned long long uint64 // just so we dont get confused ;) ok, the problem is this. I have 1 + n number of uint64 variables and the 1 should at all times contain a value equal to the...
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
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
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
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...

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.