Michael Cole
2013-05-28 08:42:44 UTC
/****** Object: Table [dbo].[Order] Script Date: 05/28/2013
18:24:36 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[Order]') AND type in (N'U'))
DROP TABLE [dbo].[Order]
GO
CREATE TABLE [dbo].[Order](
[OrderNo] [varchar](50) NULL,
[OrderDate] [date] NULL,
[CancelledDate] [date] NULL,
[Amount] [money] NULL
) ON [PRIMARY]
GO
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('007', '1/01/2013 12:00:00 AM', '1/02/2013 12:00:00 AM',
300.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('063', '1/01/2013 12:00:00 AM', NULL, 6000.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('063', '1/02/2013 12:00:00 AM', '1/03/2013 12:00:00 AM',
6200.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('065', '1/01/2013 12:00:00 AM', NULL, 450.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('065', '1/02/2013 12:00:00 AM', NULL, 480.0000);
Note - this is data from another system - please no complaints about
the structure.
The way that this should work is that if an order is reissued, then a
new record is created with a new order date and a new amount, but when
providing a summation for a date range, the date of the first issue is
used regardless of the date of the reissue. The Cancellation date is
recorded on its actual date.
Some examples (with notes on reasons for each line): -
Range of 1/1/2013 to 1/4/2013
007 - [No record] [Issued and cancelled in the date range]
063 - [No record] [Issued and cancelled in the date range]
065 - 1/1/2013 - 480 [Issued in the date range, date is first issued
date, amount is last issued amount]
Range of 1/1/2013 to 1/2/2013
007 - 1/1/2013 - 300 [Issued in the date range, date is first
issued date, amount is last issued amount, not cancelled in the date
range]
063 - 1/1/2013 - 6200 [Issued in the date range, date is first
issued date, amount is last issued amount, not cancelled in the date
range]
065 - 1/1/2013 - 480 [Issued in the date range, date is first issued
date, amount is last issued amount]
Range of 1/2/2013 to 1/3/2013
007 - 1/2/2013 - -300 [Not issued in the date range but was
cancelled, date is cancelled date, amount is negative issued amount]
063 - [No record] [Issued prior - cancelled after - no changes
in this period. The reissue within this period doesn't count as it
goes to the original issue date]
065 - [No record] [Not issued in the date range (that it was
reissued in the date range is not counted)]
Range of 1/3/2013 to 1/4/2013
007 - [No record]
063 - 4/2/2013 - -6200 [Not issued in the date range but was
cancelled, date is cancelled date, amount is negative issued amount]
065 - [No record]
Note that records with a 0 amount do not need to (and probably
shouldn't) be listed in the resultset.
Does this make sense? I'm sue the solution should be simple, but it's
not clicking for me.
18:24:36 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[Order]') AND type in (N'U'))
DROP TABLE [dbo].[Order]
GO
CREATE TABLE [dbo].[Order](
[OrderNo] [varchar](50) NULL,
[OrderDate] [date] NULL,
[CancelledDate] [date] NULL,
[Amount] [money] NULL
) ON [PRIMARY]
GO
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('007', '1/01/2013 12:00:00 AM', '1/02/2013 12:00:00 AM',
300.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('063', '1/01/2013 12:00:00 AM', NULL, 6000.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('063', '1/02/2013 12:00:00 AM', '1/03/2013 12:00:00 AM',
6200.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('065', '1/01/2013 12:00:00 AM', NULL, 450.0000);
INSERT INTO [Order] ([OrderNo], [OrderDate], [CancelledDate], [Amount])
VALUES ('065', '1/02/2013 12:00:00 AM', NULL, 480.0000);
Note - this is data from another system - please no complaints about
the structure.
The way that this should work is that if an order is reissued, then a
new record is created with a new order date and a new amount, but when
providing a summation for a date range, the date of the first issue is
used regardless of the date of the reissue. The Cancellation date is
recorded on its actual date.
Some examples (with notes on reasons for each line): -
Range of 1/1/2013 to 1/4/2013
007 - [No record] [Issued and cancelled in the date range]
063 - [No record] [Issued and cancelled in the date range]
065 - 1/1/2013 - 480 [Issued in the date range, date is first issued
date, amount is last issued amount]
Range of 1/1/2013 to 1/2/2013
007 - 1/1/2013 - 300 [Issued in the date range, date is first
issued date, amount is last issued amount, not cancelled in the date
range]
063 - 1/1/2013 - 6200 [Issued in the date range, date is first
issued date, amount is last issued amount, not cancelled in the date
range]
065 - 1/1/2013 - 480 [Issued in the date range, date is first issued
date, amount is last issued amount]
Range of 1/2/2013 to 1/3/2013
007 - 1/2/2013 - -300 [Not issued in the date range but was
cancelled, date is cancelled date, amount is negative issued amount]
063 - [No record] [Issued prior - cancelled after - no changes
in this period. The reissue within this period doesn't count as it
goes to the original issue date]
065 - [No record] [Not issued in the date range (that it was
reissued in the date range is not counted)]
Range of 1/3/2013 to 1/4/2013
007 - [No record]
063 - 4/2/2013 - -6200 [Not issued in the date range but was
cancelled, date is cancelled date, amount is negative issued amount]
065 - [No record]
Note that records with a 0 amount do not need to (and probably
shouldn't) be listed in the resultset.
Does this make sense? I'm sue the solution should be simple, but it's
not clicking for me.
--
Michael Cole
Michael Cole