From 01/01/2007 to 12/31/2011, the unit price for my products will be fixed for whatever the original unit price was. From 01/01/2012 to 12/31/2015, the unit price will be increased by 2% every year, so for 9/11/2012 should be unit price*1.02 , and 09/11/2013 should be (unit price*1.02)*1.02

My product table, PRODUCT_T, has information such as Product_ID and Unit_Price.

So far, I have:

CREATE TABLE PRODUCT_TIMESTAMP_T
(
PRODUCT_ID INT,
START_VALID_DATE DATE,
END_VALID_DATE DATE,
START_EXPONENTIAL_DATE DATE,
END_EXPONENTIAL_DATE DATE,
CUSTOM_DATE DATE,
UNIT_PRICE DECIMAL (13,4)
);

ALTER TABLE PRODUCT_TIMESTAMP_T ADD FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCT_T.PRODUCT_ID

INSERT INTO PRODUCT_TIMESTAMP_T (PRODUCT_ID)
VALUES (1),(2),(3),(4)

UPDATE PRODUCT_TIMESTAMP_T
SET START_VALID_DATE = '2007-01-01'

UPDATE PRODUCT_TIMESTAMP_T
SET END_VALID_DATE = '2011-12-31'

UPDATE PRODUCT_TIMESTAMP_T
SET START_EXPONENTIAL_DATE = '2012-01-01'

UPDATE PRODUCT_TIMESTAMP_T
SET END_EXPONENTIAL_DATE = '2015-12-31'

UPDATE PRODUCT_TIMESTAMP_T 
SET PRODUCT_T.UNIT_PRICE = PRODUCT_TIMESTAMP_T.UNIT_PRICE
WHERE CUSTOM_DATE < START_EXPONENTIAL_DATE

UPDATE PRODUCT_TIMESTAMP_T
SET REAL_PRICE = UNIT_PRICE*1.02
WHERE CUSTOM_DATE < '2013-01-01'`

My main error is if I need to create another column or even another table to store the exponential unit price, or if I can use a if else statement and keep just one column to store the unit price. the problem with the above code is that it does not let me update two tables at once (I have searched for how to update two tables but I am still not close to finishing this problem)

I am not sure if I need to use triggers/procedures for this, or if it can be solved some other way.

*UPDATE 03/13/2014 1:14 AM Thank you that really helped, but I have a few small errors I still need to correct. The final output I need is product id, Product Desc, (product name) and the Unit price. So instead of the unit price listed there I would want the one with no column name instead. The result when I execute all of this:

PRODUCT_ID    PRODUCT_DESCRIPTION    UNIT_PRICE    (no column name)
    1              Cleaner             4.99            5.089800
    2              Fire                3.99            4.149600
    3              Water               7.99            8.469400

I am guessing the no column name is because I do not have an alias for dbo.RealPrice? But I have never used CREATE FUNCTIONS at all so I am trying to look for what all of this means. I also found out that the numbers from the no column name is multiplying Unit price by 1.02 for the first product(Cleaner), 1.04 in Fire, and 1.06 in Water. What i want is 1.02 for everything, but I also was not sure where you set the values for the Custom Date. Mine is 2012-09-11 so it should be 1.02 for the multiplier.

And this is the code that I updated from the original, I wasn't sure if I needed everything here though.

CREATE TABLE PRODUCT_TIMESTAMP_T
(
PRODUCT_ID INT,
START_VALID_DATE DATE,
END_VALID_DATE DATE,
START_EXPONENTIAL_DATE DATE,
END_EXPONENTIAL_DATE DATE,
CUSTOM_DATE DATE,
UNIT_PRICE DECIMAL (13,4)
);


INSERT INTO PRODUCT_TIMESTAMP_T (PRODUCT_ID)
VALUES (1),(2),(3),(4)

UPDATE PRODUCT_TIMESTAMP_T
SET START_VALID_DATE = '2007-01-01'

UPDATE PRODUCT_TIMESTAMP_T
SET END_VALID_DATE = '2011-12-31'

UPDATE PRODUCT_TIMESTAMP_T
SET START_EXPONENTIAL_DATE = '2012-01-01'

UPDATE PRODUCT_TIMESTAMP_T
SET END_EXPONENTIAL_DATE = '2015-12-31'

GO
CREATE FUNCTION dbo.RealPrice
       (@START_EXPONENTIAL_DATE date,
        @CUSTOM_DATE date, 
        @START_UNIT_PRICE DECIMAL (13,4),
        @ANNUAL_INCREASE DECIMAL (13,4))
RETURNS DECIMAL (13,4)
AS BEGIN
DECLARE @CALCULATED_UNIT_PRICE DECIMAL (13,4)
IF @CUSTOM_DATE < @START_EXPONENTIAL_DATE RETURN @START_UNIT_PRICE 
SET @CALCULATED_UNIT_PRICE = @START_UNIT_PRICE * POWER( @ANNUAL_INCREASE , DATEDIFF(YY,@START_EXPONENTIAL_DATE,@CUSTOM_DATE)+1)
RETURN @CALCULATED_UNIT_PRICE
END
GO

SELECT dbo.RealPrice('2012-01-01','9/11/2012',10,1.02) AS REAL_PRICE

UPDATE PRODUCT_TIMESTAMP_T
SET UNIT_PRICE = 1,CUSTOM_DATE = DATEADD(yy,PRODUCT_ID-1,START_EXPONENTIAL_DATE)

SELECT  PRODUCT_T.PRODUCT_ID,
        PRODUCT_DESCRIPTION,
        PRODUCT_T.UNIT_PRICE
,CASE WHEN START_EXPONENTIAL_DATE < START_VALID_DATE THEN PRODUCT_T.UNIT_PRICE
ELSE PRODUCT_T.UNIT_PRICE * POWER( 1.02 , DATEDIFF(YY,START_EXPONENTIAL_DATE,CUSTOM_DATE)+1) END
 FROM PRODUCT_TIMESTAMP_T, PRODUCT_T
 WHERE PRODUCT_T.PRODUCT_ID = PRODUCT_TIMESTAMP_T.PRODUCT_ID
 AND PRODUCT_T.UNIT_PRICE IS NOT NULL
有帮助吗?

解决方案

I'm not sure how much your example has been simplified to post here, however I'd use the POWER function to resolve this - you can then use the output as a computed column or simply in an update statement. I've created a function to illustrate the generic case:

CREATE FUNCTION dbo.RealPrice (@START_EXPONENTIAL_DATE date,
        @CUSTOM_DATE date, 
        @START_UNIT_PRICE DECIMAL (13,4),
        @ANNUAL_INCREASE DECIMAL (13,4))
RETURNS DECIMAL (13,4)
AS BEGIN
DECLARE @CALCULATED_UNIT_PRICE DECIMAL (13,4)
IF @CUSTOM_DATE < @START_EXPONENTIAL_DATE RETURN @START_UNIT_PRICE -- Otherwise we can get calculated deflation
SET @CALCULATED_UNIT_PRICE = @START_UNIT_PRICE * POWER( @ANNUAL_INCREASE , DATEDIFF(YY,@START_EXPONENTIAL_DATE,@CUSTOM_DATE)+1)
RETURN @CALCULATED_UNIT_PRICE
END
GO

SELECT dbo.RealPrice('2012-01-01','9/11/2013',10,1.02)

Note, this logic can probably be made simple enough to use in a computed column definition without the function, which helps offset this potential performance issue, but I include it as a function as there are many places you may choose to use it. Alternatively, you may wish to convert this to a table valued function or view to improve performance.

The simple example in a select statement can be run if we add some additional values to your table:

UPDATE PRODUCT_TIMESTAMP_T
SET UNIT_PRICE = 1,CUSTOM_DATE = DATEADD(yy,PRODUCT_ID-1,START_EXPONENTIAL_DATE)

SELECT  PRODUCT_ID ,
        START_VALID_DATE ,
        END_VALID_DATE ,
        START_EXPONENTIAL_DATE ,
        END_EXPONENTIAL_DATE ,
        CUSTOM_DATE ,
        UNIT_PRICE
,CASE WHEN START_EXPONENTIAL_DATE < START_VALID_DATE THEN UNIT_PRICE
ELSE UNIT_PRICE * POWER( 1.02 , DATEDIFF(YY,START_EXPONENTIAL_DATE,CUSTOM_DATE)+1) END
 FROM PRODUCT_TIMESTAMP_T

So you can see I'm simply using the CASE statement to calculate your answer with POWER and DATEDIFF, although I've used a static value of 1.02 (not sure if yours is static or should be drawn from somewhere else).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top