Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 The closest record

Author  Topic 

josethegeek
Starting Member

45 Posts

Posted - 2004-09-09 : 13:47:32
Hello,

Maybe somebody can help me out.

We currently have 3 tables that we use to give discounts depending on the product and quantity a customer buys.

The tables are as follows.....

tbl_Products
Product_ID
Product_Name
Discount_ID

tbl_Discounts
Discount_ID
Discount_Name

tbl_DiscountOptions
DiscountOption_ID
Discount_ID
QTY
Discount_Percentage

Basically a product has a discount_ID assigned to it. Then depending on the total items they order we will give them the Discount_Percentage

Currently the tbl_DiscountOptions has this options

DiscountOption_ID - Discount_ID - QTY - Discount_Percentage
1 1 1 0
2 1 3 10
3 1 5 15
4 1 10 20

The problem that I am running to is that when someone enters in 8 as the qty I would like to give them the discount of DiscountOptionID # 3. If they order 12 I would like to give them the discount of # 4. If they order 2 I would like to give them the discount of DiscountOptionID #1. You get the idea.

Basically, I would like to get the nearest Option depending on the Quantity they order.

Thanks!

josethegeek
Starting Member

45 Posts

Posted - 2004-09-09 : 14:59:54
Thanks, but I found the answer. Decided on using Cursors.

Jose
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-09-09 : 15:30:15
If you say so....


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(DiscountOption_ID int, Discount_ID int, QTY int, Discount_Percentage int)
GO

INSERT INTO myTable99(DiscountOption_ID, Discount_ID, QTY, Discount_Percentage)
SELECT 1, 1, 1, 0 UNION ALL
SELECT 2, 1, 3, 10 UNION ALL
SELECT 3, 1, 5, 15 UNION ALL
SELECT 4, 1, 10, 20
GO

DECLARE @Order Int
SELECT @Order = 8

SELECT *
FROM ( SELECT Discount_ID, MAX(QTY) AS MAX_QTY
FROM myTable99
WHERE QTY < @Order
GROUP BY Discount_ID ) AS x
JOIN myTable99 y
ON x.Discount_Id = y.Discount_Id
AND x.MAX_QTY = y.QTY
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO



I'm sure there's a better solution....



Brett

8-)
Go to Top of Page
   

- Advertisement -