Try:
declare @d datetime
set @d = '20070205'
select
c.ContractID,
c.Lastname,
c.Firstname,
c.ContractValue +
isnull(
(
select sum(a.ContractValue)
from [dbo].[Amendments] as a
where a.ContractID = c.ContractID and a.EffectivityDate <= @d
), 0) as current_ContractValue
from
[dbo].[Contracts] as c
where
c.DateCreated <= @d
GO
BTW, why are you duplicating contract data in the amendments table, like LastName and FirstName
AMB
I think creating a view is very expensive here.
BTW, if you want to create the view you have to use the following tasks.
- Create a calendar table
- Update the data on calendar table periodically
- Join with the original tables
Create table MyCalendar ( Date Datetime ) Declare @date as Datetime Set @date='2007-01-01' While @date <= '2007-12-31' Begin Insert Into MyCalendar Select @date where not exists(select 1 from MyCalendar where date=@date) Set @date = dateadd(dd,1,@date) End
create view myview as select ContractID, lastName, Firstname, date, Sum(case when aefd <= date then v end) as ContractValue from ( select C.ContractID, C.lastName, C.Firstname, a.EffectivityDate aefd, a.ContractValue v from contracts c left outer Join amendments a on a.ContractID = c.ContractID Union All select C.ContractID, C.lastName, C.Firstname, c.[DateCreated] aefd, c.ContractValue v from contracts c ) as D cross join MyCalendar group By ContractID, lastName, Firstname, date
> Views do not allow the "DECLARE" keyword.
I just gave you the statement. Can you create the view
Try:
CREATE
PROCEDURE dbo.usp_p1@d
DATETIMEAS
SET
NOCOUNT ON
SELECT
COALESCE(t.ContractID, c.ContractID) AS ContractID, COALESCE(t.Lastname, c.Lastname) AS Lastname, COALESCE(t.Firstname, c.Firstname) AS Firstname,
c
.ContractValue + isnull( ( SELECT sum(b.ContractValue) FROM [dbo].[Amendments] as b WHERE b.ContractID = c.ContractID and b.EffectivityDate <= @d ), 0) as current_ContractValueFROM
.[Contracts] as c OUTER APPLY ( SELECT TOP (1) a.ContractID, a.Lastname, a.Firstname FROM [dbo].[Amendments] as a WHERE a.ContractID = c.ContractID AND a.EffectivityDate <= @d ORDER BY a.EffectivityDate DESC ) AS t[dbo]
WHERE
.DateCreated <= @dc
GO
CREATE VIEW dbo.vw_v1
AS
SELECT
COALESCE(t.ContractID, c.ContractID) AS ContractID, COALESCE(t.Lastname, c.Lastname) AS Lastname, COALESCE(t.Firstname, c.Firstname) AS Firstname,
c
.ContractValue + isnull( ( select sum(b.ContractValue) from [dbo].[Amendments] as b where b.ContractID = c.ContractID and b.EffectivityDate <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) ), 0) as current_ContractValuefrom
.[Contracts] as c OUTER APPLY ( SELECT TOP (1) a.ContractID, a.Lastname, a.Firstname FROM [dbo].[Amendments] as a WHERE a.ContractID = c.ContractID AND a.EffectivityDate <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) ORDER BY a.EffectivityDate DESC ) AS t[dbo]
WHERE
.DateCreated <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)c
GO
AMB
Mr Hunchback you are almost there. In the stored procedure i need to use the view instead of the table. some thing like this.
CREATE PROCEDURE dbo.usp_p1
@d DATETIME
AS
SET NOCOUNT ON
SELECT
Column1,
Column2
FROM
WHEREdbo.vw_v1 c
.DateCreated <= @dGOc
You can not parameterize a view, so you will have to use a table-valued user defined funcion instead. Use the code from sp and create the function, then use the function inside the sp.
AMB