473,789 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Custo merID, 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.T argetID
AND _Rate.OwnerLeve l = Filtered_Rate.O wnerLevel

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 3654
>> 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(faci lity_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
1413
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
4240
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
5683
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 = 6442450944 options.ranges = fd = open("testfile", "w") fd.seek(options.size-1)
0
1814
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 packages I'm aware of -- the Sparse module in Scipy and PySparse --are both pretty incomplete compared to (dense) numeric arrays. Any alternatives ? George
2
1930
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 ("F1") )
0
1386
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 what ranges in the file are sparse zeros. However, it appears that the following steps also work when creating a new sparse file. First, using Win32 (via .NET 1.1 pinvoke), I ... CreateFile (returns a handle)
7
14248
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 help me? Thanks in advance. I write this, but it's not so good :(
4
7648
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
3333
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 class hierarchy but only objects of some of these types can be a parent. Example: class CFruit : public CBase { }; class CBranch : public CBase { };
5
9716
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 implement the sparse matrices as a doubly-linked list, in which each non-zero cell is stored roughly as follows: int rownum int colnum
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.