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

Difficult query help

I have a table that stores billing rates for our employees by client.
Each employee can have a different billing rate for each client for a
specified period. Here are the columns in the table.

eid - Employee ID#
cid - Client ID#
startdt - start date of billing rate
enddt - end date of billing rate
brate - billing rate

I need to create a script that will verify that for a given eid, and cid
that either the startdt or enddt for one billing rate, the periods do
not overlap.

For example, I need to be able to detect overlaps such as this:
eid cid startdt enddt brate
001 001 1/1/2003 12/31/2003 $50
001 001 11/01/2003 04/01/2004 $75

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
2 1571
I assume you've already got a unique constraint on (eid,cid,startdt) and a
check constraint to ensure that (startdt<enddt).

CREATE TABLE BillingRates (eid INTEGER, cid INTEGER, startdt DATETIME, enddt
DATETIME NOT NULL, CHECK (startdt<enddt), brate NUMERIC(10,2) NOT NULL,
PRIMARY KEY (cid,eid,startdt))

INSERT INTO BillingRates VALUES (001, 001, '20030101', '20031231', 50)
INSERT INTO BillingRates VALUES (001, 001, '20031101', '20040401', 75)

This query will find any overlaps:

SELECT B1.*
FROM BillingRates AS B1
JOIN BillingRates AS B2
ON B1.eid = B2.eid
AND B1.cid = B2.cid
AND B1.startdt <= B2.enddt
AND B1.enddt >= B2.startdt
AND B1.startdt <> B2.startdt

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
Thanks, that's exactly what I needed.
*** 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

7
by: M Wells | last post by:
Hi All, I have what seems to me to be a difficult query request for a database I've inherited. I have a table that has a varchar(2000) column that is used to store system and user messages...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
5
by: Bob | last post by:
Hi Everybody Difficult question Has anyone else used the "Using the Tab control as a container" database example that comes with the above book chapter 7 on the accompanying disc. It is a...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
5
by: Maxi | last post by:
I have 162 tables in my database. Names of the Tables are 1, 2, ...... so on till 162. Every table has only one field (field name = Expr2) of type NUMBER (DOUBLE) with 352716 records in each table....
1
by: mithril | last post by:
I cannot get my head around this query... I think I need a nested query but here's the problem. I promise i've exerted my meager brain power on this problem & ask this as a last resot! 3...
5
by: jmartineau | last post by:
Hello, Here is a brief summary: Table 1 = All Accounts - with fields such as Customer ID and Account # Table 2 = Deposit Balance Table - with fields such as Account #, Balance
1
by: Claudio Magno | last post by:
Hi all, in short here it is my problem: I have to monitor the prices of a product sold by different shops and I need a query to retrieve a list of shops with the associated CURRENT price of the...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
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
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: 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
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
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
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.