Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Ensure Records with the Same Foreign Key have no overlapping dates

$
0
0

I have a project where I need to store rebate information of a particular merchant in order to calculate the total savings for a particular merchant.  Here is my current table design with some sample data.

CREATE TABLE [dbo].[MerchantDiscounts]
(
	[MerchantID] [int] NOT NULL,
	[BeginDate] [date] NOT NULL,
	[EndDate] [date] NULL,
	[LowTier] [money] NOT NULL,
	[HighTier] [money] NULL,
	[DiscountPct] [decimal](3, 2) NULL
) ON [PRIMARY]

GO

INSERT INTO MerchantDiscounts VALUES (1, '01-01-2013', '06-30-2013', 0, 10000, .25);
INSERT INTO MerchantDiscounts VALUES (1, '07-01-2013', null, 10000, null, .5);
INSERT INTO MerchantDiscounts VALUES (1, '01-01-2013', '08-01-2013', 0, 10000, .25);	-- overlapping date ranges
INSERT INTO MerchantDiscounts VALUES (1, '01-01-2013', '06-30-2013', 5000, 10000, .5);	-- overlapping tiers

GO

Take this example for instance, if a person spends $7000 with Merchant 1 between 1/1/2013-6/30/2013 he will receive a 25% discount which gives him a savings of $7000 x 25% = $1750.  The query will have to sum all transactions between t

There is a problem with the 3rd and 4th row.  In row 3, the BeginDate and EndDate overlap the BeginDate and EndDate of row 1, which is no good.  In row 4, the LowTier and HighTier overlap the LowTier and HighTier of row 2, which is no good.  How do I enforce data integrity which would not allow the 3rd and 4th row from being inserted?  Or should I change my data structure?

Thanks in advance!


Ryan


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>