Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get the correct Return_Amt from a table? Expand / Collapse
Author
Message
Posted Tuesday, September 10, 2013 9:47 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 12, 2013 4:17 AM
Points: 2, Visits: 17
Please can anyone help me?

Table Name: tableSTK
ItemID varchar(8)
ItemDesc varchar(100)
Price money

Table Name: tableABC
ItemID varchar(8)
ConditionAmt money
Return_Amt money

The ConditionAmt and Return_Amt in the tableABC can be some other values based on the ItemID.

Here is the sample with different values.

ItemID ItemDesc Price
04400110 A4 Paper 30.00
08800220 Bic Pen 5.0 20.00

ItemID ConditionAmt Return_Amt
04400110 100.00 28.50
04400110 200.00 27.00
04400110 300.00 25.50
08800220 50.00 19.00
08800220 80.00 18.00
08800220 150.00 15.00
08800220 200.00 14.00

I need a query to get the correct value from the Return_Amt based on the ConditionAmt of a total purchase amt on the particular ItemID.

Examples
A.
John have a total purchase amt of 105.00.
When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 100.00 and below 200 and below 300 the Return_Amt is 28.50 as SellingPrice.

B.
John have a total purchase amt of 250.00.
When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 200.00 and below 300 the Return_Amt is 27.00 as SellingPrice.

C.
John have a total purchase amt of 400.00.
When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 300.00 the Return_Amt is 28.50 as SellingPrice.

D.
John have a total purchase amt of 50.00.
When he purchase ItemID 04400110 while total purchase amt is less than ConditionAmt 100.00 therefore the ItemID 04400110 Price 30.00 as SellingPrice.

E.
Mary have a total purchase amt of 95.00.
When she purchase ItemID 08800220 while total purchase amt is more than ConditionAmt 80.00 and below 150 and below 200 the Return_Amt is 18.00 as SellingPrice.

Thanks.
jwlooi
Post #1493433
Posted Wednesday, September 11, 2013 2:34 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 22, 2014 12:13 AM
Points: 2,631, Visits: 4,723
One of the ways to achieve it..

DECLARE	@tableSTK TABLE
(
ItemID VARCHAR(8),
ItemDesc VARCHAR(100),
Price MONEY
)

DECLARE @tableABC TABLE
(
ItemID VARCHAR(8),
ConditionAmt MONEY,
Return_Amt MONEY
)

INSERT @tableSTK
( ItemID, ItemDesc, Price )
SELECT '04400110', 'A4 Paper', 30.00 UNION ALL
SELECT '08800220', 'Bic Pen 5.0', 20.00

INSERT @tableABC
( ItemID, ConditionAmt, Return_Amt )
SELECT '04400110', 100.00, 28.50 UNION ALL
SELECT '04400110', 200.00, 27.00 UNION ALL
SELECT '04400110', 300.00, 25.50 UNION ALL
SELECT '08800220', 50.00, 19.00 UNION ALL
SELECT '08800220', 80.00, 18.00 UNION ALL
SELECT '08800220', 150.00, 15.00 UNION ALL
SELECT '08800220', 200.00, 14.00

DECLARE @ConditionAmt INT,
@ItemID VARCHAR(8)

SELECT @ConditionAmt = 95.00,
@ItemID = '08800220'

; WITH cte_table AS
(
SELECT ROW_NUMBER() OVER( PARTITION BY ItemID ORDER BY ConditionAmt ) AS RN, * FROM
(
SELECT ItemID, 0 AS ConditionAmt, Price
FROM @tableSTK
UNION ALL
SELECT ItemID, ConditionAmt, Return_Amt
FROM @tableABC
) AS T
)

SELECT T1.ItemID, T1.Price
FROM cte_table AS T1
LEFT JOIN cte_table AS T2 ON T1.ItemID = T2.ItemID AND T1.RN = T2.RN - 1
WHERE T1.ItemID = @ItemID AND T1.ConditionAmt <= @ConditionAmt AND ( T2.ConditionAmt > @ConditionAmt OR T2.ConditionAmt IS NULL )




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1493499
Posted Wednesday, September 11, 2013 7:40 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 12, 2013 4:17 AM
Points: 2, Visits: 17
WOW! Thanks for your help. It solve my problem.

jwlooi

Kingston Dhasian (9/11/2013)
One of the ways to achieve it..

DECLARE	@tableSTK TABLE
(
ItemID VARCHAR(8),
ItemDesc VARCHAR(100),
Price MONEY
)

DECLARE @tableABC TABLE
(
ItemID VARCHAR(8),
ConditionAmt MONEY,
Return_Amt MONEY
)

INSERT @tableSTK
( ItemID, ItemDesc, Price )
SELECT '04400110', 'A4 Paper', 30.00 UNION ALL
SELECT '08800220', 'Bic Pen 5.0', 20.00

INSERT @tableABC
( ItemID, ConditionAmt, Return_Amt )
SELECT '04400110', 100.00, 28.50 UNION ALL
SELECT '04400110', 200.00, 27.00 UNION ALL
SELECT '04400110', 300.00, 25.50 UNION ALL
SELECT '08800220', 50.00, 19.00 UNION ALL
SELECT '08800220', 80.00, 18.00 UNION ALL
SELECT '08800220', 150.00, 15.00 UNION ALL
SELECT '08800220', 200.00, 14.00

DECLARE @ConditionAmt INT,
@ItemID VARCHAR(8)

SELECT @ConditionAmt = 95.00,
@ItemID = '08800220'

; WITH cte_table AS
(
SELECT ROW_NUMBER() OVER( PARTITION BY ItemID ORDER BY ConditionAmt ) AS RN, * FROM
(
SELECT ItemID, 0 AS ConditionAmt, Price
FROM @tableSTK
UNION ALL
SELECT ItemID, ConditionAmt, Return_Amt
FROM @tableABC
) AS T
)

SELECT T1.ItemID, T1.Price
FROM cte_table AS T1
LEFT JOIN cte_table AS T2 ON T1.ItemID = T2.ItemID AND T1.RN = T2.RN - 1
WHERE T1.ItemID = @ItemID AND T1.ConditionAmt <= @ConditionAmt AND ( T2.ConditionAmt > @ConditionAmt OR T2.ConditionAmt IS NULL )

Post #1493969
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse