Our company has a table that contains one row for every period in every day. There are 48 periods per day so there are 48 rows per day in the table. The only exception is daylight savings days. On one day in March there is a day with 50 periods, and on one day in October there is a day with 46 periods. This is reflected by the table (calendar) which has a structure like so:
myDate myPeriod
1-mar-2006 1
1-mar-2006 2
...
1-mar-2006 48
2-mar-2006 1
etc..
Now I have another table (myTable) with a structure like so:
myDate myPeriod myArea myValue
1-mar-2006 1 AAA 45.67
1-mar-2006 1 AAA 23.34
1-mar-2006 1 BBB 67.23
1-mar-2006 1 CCC 12.78
1-mar-2006 2 AAA 18.95
etc...
This table does not recognise daylight savings so has exactly 48 periods per day even if the day is a daylight savings day.
This table can have numerous rows per day, per period and per area.
What I need is a select statement that selects between a start and end date, but if one of the days included is a daylight savings day then I need to adjust that day repectively. i.e. a) if one of the days is only supposed to have 46 periods then dispose of periods 47-48, and vice versa b) if the day is supposed to have 50 periods then return the rows for period 48 duplicated to make rows for periods 49 and 50.
I think I have done part a):
but I have no idea how to do part b)
anyone know?
myDate myPeriod
1-mar-2006 1
1-mar-2006 2
...
1-mar-2006 48
2-mar-2006 1
etc..
Now I have another table (myTable) with a structure like so:
myDate myPeriod myArea myValue
1-mar-2006 1 AAA 45.67
1-mar-2006 1 AAA 23.34
1-mar-2006 1 BBB 67.23
1-mar-2006 1 CCC 12.78
1-mar-2006 2 AAA 18.95
etc...
This table does not recognise daylight savings so has exactly 48 periods per day even if the day is a daylight savings day.
This table can have numerous rows per day, per period and per area.
What I need is a select statement that selects between a start and end date, but if one of the days included is a daylight savings day then I need to adjust that day repectively. i.e. a) if one of the days is only supposed to have 46 periods then dispose of periods 47-48, and vice versa b) if the day is supposed to have 50 periods then return the rows for period 48 duplicated to make rows for periods 49 and 50.
I think I have done part a):
Code:
select a.myDate, a.myPeriod, myArea, myValue from
(
SELECT * FROM dbo.calendar
WHERE mydate BETWEEN @StartDate AND @EndDate
) a
left outer join
(
SELECT * FROM dbo.myTable
WHERE mydate BETWEEN @StartDate AND @EndDate and buysell = 'Sell'
) b
on a.mydate = b.mydate and a.myperiod = b.myperiod
order by a.mydate, myperiod
but I have no idea how to do part b)
anyone know?