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

Sparse Hierarchy T-SQL Help...PLEASE?

Greetings!

I could really use some suggestions on how to improve on the
following, it at all possible:

Table 'Customer'
---------------------
ID GUID PK
....

Table 'Facility'
-----------------
ID GUID PK
CustomerID GUID FK (FK to Customer GUID)
....

Table 'Rate'

----------------
ID PK
OwnerID GUID Nullable FK (FK to Customer, Facility GUID PK)
OwnerLevel INT Contraint 1-3
<Rate Data>

Table 'Rate' is a sparse hierarchy of data. There are 3 possible
levels in the hierarchy as follows:

OwnerID <NULL>
OwnerLevel 1

This indicates Global rate data.

OwnerID <Customer.ID>
OwnerLevel 2

This indicates Customer-specific rate data.

OwnerID <Facility.ID>
OwnerLevel 3

This indicates Facility-specific rate data.

Now, a given Customer need not have an entry in the Rate table. If a
Customer does not have an entry, it is supposed to 'inherit' Global
rate data. A given Facility need not have an entry in the Rate table.
If a Facility does not have an entry, it is supposed to inherit
Customer-specific rate data, and in the absence of an entry for the
Facility's parent Customer, it is supposed to inherit Global rate
data.

The challenge is that I want to write a view to give me back the
appropriate rate record for Customer and Facility. Here's what I've
done so far.

View _Rate
--------------
SELECT
Rate.*,
NULL AS TargetID
FROM
Rate
WHERE
Rate.OwnerID IS NULL

UNION

SELECT
Rate.*,
Customer.ID AS TargetID
FROM
Rate
CROSS JOIN
Customer
WHERE
Rate.OwnerID IS NULL
OR Rate.OwnerID = Customer.ID

UNION

SELECT
Rate.*,
Facility.ID AS TargetID
FROM
Rate
CROSS JOIN
Facility
WHERE
Rate.OwnerID IS NULL
OR Rate.OwnerID IN (Facility.CustomerID, Facility.ID)

View view_Rate
--------------------

SELECT
_Rate.*
FROM
_Rate
INNER JOIN
(
SELECT
TargetID,
MAX(OwnerLevel) AS OwnerLevel
FROM
_Rate
GROUP BY
TargetID
) AS Filtered_Rate
ON
_Rate.TargetID = Filtered_Rate.TargetID
AND _Rate.OwnerLevel = Filtered_Rate.OwnerLevel

The combination of these two views gives a resultset that contains 1
record for every Target ID as follows:

TargetID <NULL>
OwnerID <NULL>
OwnerLevel 1

This indicates Global rate data established at the Global level.

TargetID <Customer.ID>
OwnerID <NULL>
OwnerLevel 1

This indicates Customer rate data for the specific Customer identified
by Customer.ID is inherited from the Global rate data.

TargetID <Customer.ID>
OwnerID <Customer.ID>
OwnerLevel 2

This indicates Customer-specific rate data for the specific Customer
identified by Customer.ID (not inherited).

TargetID <Facility.ID>
OwnerID <NULL>
OwnerLevel 3

This indicates Facility rate data is inherited from the Global rate
data.

TargetID <Facility.ID>
OwnerID <Customer.ID>
OwnerLevel 2

This indicates Facility rate data for the specific Facility identified
by Facility.ID is inherited from the Facility's parent Customer's
Customer-specific rate data.

TargetID <Facility.ID>
OwnerID <Facility.ID>
OwnerLevel 3

This indicates Facility-specific rate data for the specific Facility
identified by Facility.ID (not inherited).

I know this is a lengthy post, and a complicted query scenario, but
I'm not willing to accept that my solution is the best solution just
yet. Please consider that I really need this functionality in a VIEW
as much as possible.

Thank you for your learned consideration.

I eagerly await your replies.

Darryll
Jul 20 '05 #1
2 3634
>> I could really use some suggestions on how to improve on the
following, it at all possible: <<

I just wrote a book on TREES & HIERARCHIES IN SQL (look at
Amazon.com). This might help.

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. I gave up trying to read it. It looks like you are
using GUIDs for keys in violation of all relational design principles,
that data elements are changing names from table to table and you have
a vague universal, meaningless attribute called "id" floating
everywhere.

Would please post something that makes sense? Nobody wants to figure
out your DDL, insert the data and then test the code for you for free.
And you do not want to pay my rates :)
Jul 20 '05 #2
This is some stuff we did off-line, but I like to post to newsgroups:

Please read ISO-11179 for the rules for naming data elements. Tables
should be plural, class or collective nouns. Keys by definition should
not be exposed physical locators, like GUIDs, IDENTITY, autoincrement,
ROWID and all those other proprietary things based on the state of the
computer hardware at some point in time or the layout of a hard drive.
They are a subset of attributes of the entity being modeled in a table.
Look at anything in the Universe; there is no GUID on it; God uses a
17-letter Hebrew word that only special rabbis can see :)

Data element names do not change from table to table. Keys are not
NULL-able. There is no magic, universal "id" in the relational model.
That was record numbers in sequential files in the 1950's.

I am assuming that customers show up a facility and get billed a dollar
amount rate. I assume that the facility_rate is between cust_rate and
global_rate.

Start by modeling entities; sin e we can see and touch these two things,
they are probably entities:

CREATE TABLE Customers
(customer_id INTEGER NOT NULL PRIMARY KEY,
..);

CREATE TABLE Facilities
(facility_id INTEGER NOT NULL PRIMARY KEY,
..);

Now show the relationship between these two entities. I picked a hotel
term.

CREATE TABLE Occupancies
(facility_id INTEGER NOT NULL PRIMARY KEY,
REFERENCES Facilities(facility_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
customer_id INTEGER NOT NULL
REFERENCES Customers (customer_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
PRIMARY KEY (facility_id, customer_id),
global_rate DECIMAL(12,4) NOT NULL,
facility_rate DECIMAL(12,4),
cust_rate DECIMAL(12,4),
CHECK (facility_rate < global_rate),
CHECK (cust_rate < facility_rate),
CHECK (CASE WHEN cust_rate IS NOT NULL
AND facility_rate IS NULL
THEN 0 ELSE 1 END = 1),

..);

You can re-write the last constraint with some COALESCE() expressions,
but the CASE is clearer to read. It forces a facility or global rate to
exist when there is no customer rate. Likewise if the facility rate
does not exist, the Global rate always exists.

Your query is now simply this:

SELECT facility_id, customer_id,
COALESCE (cust_rate, facility_rate, global_rate) AS rate
FROM Occupancies;
If you want to raise the rates on someone, use an explicit column
instead of the COALESCE(). Most hard queries are the result of errors
in the DDL.

--CELKO--
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

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

Similar topics

0
by: archway | last post by:
Hi, I would like to build a hierarchy of ProductNode objects like so ProductNode ---Product Node ---ProductNode ------ProductNode ------ProductNode ---ProductNode
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
10
by: draghuram | last post by:
Hi, Is there any special support for sparse file handling in python? My initial search didn't bring up much (not a thorough search). I wrote the following pice of code: options.size =...
0
by: George Sakkis | last post by:
Is there any sparse matrix package compatible with Numeric/Numarray ? Ideally, the implementation of a matrix (dense/sparse) should be transparent to the application. However the APIs of the only...
2
by: Yaro | last post by:
Hi All (UDB 8.1 FP8, Win) In my simple test database, I have two tables CREATE TABLE "DB2ADMIN"."AAA" ( "F1" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, "F2" VARCHAR(10), PRIMARY KEY...
0
by: David | last post by:
I've just recently read several articles that state that the only way to write "sparse zeros" to an NTFS5 sparse file is to use Win32 DeviceIoControl with FSCTL_SET_ZERO_DATA in order to specify...
7
by: mariaczi | last post by:
Hi, I code class to storage sparse matrix row compressed and i have a problem with implements method to setVal and addVal. I will replace later this methods overloaded operator(). Please, can You...
4
by: deLenn | last post by:
Hi, Does scipy have an equivalent to Matlab's 'find' function, to list the indices of all nonzero elements in a sparse matrix? Cheers.
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.