表a 苹果价格表
名称 数量 金额
苹果 200 50
苹果 500 100
苹果 800 150
苹果 1000 200
苹果 1200 250
苹果 1500 300
表b 达到数量的折扣表
名称 数量 折扣
苹果 500 0.95
苹果 1000 0.90
苹果 1500 0.85
现在我想要的结果就是
名称 数量 金额 折扣
苹果 200 50 0.95
苹果 500 100 0.95
苹果 800 150 0.90
苹果 1000 200 0.90
苹果 1200 250 0.85
苹果 1500 300 0.85
望各位大大帮忙解决,小弟感激不尽
--创建价格表
CREATE TABLE #ApplePrice (
[Name] NVARCHAR(10)
,[Quantity] INT
,[Amount] MONEY
)
GO
INSERT INTO #ApplePrice (
[Name]
,[Quantity]
,[Amount]
)
SELECT '苹果',200,50 UNION ALL
SELECT '苹果',500,100 UNION ALL
SELECT '苹果',800,150 UNION ALL
SELECT '苹果',1000,200 UNION ALL
SELECT '苹果',1200,250 UNION ALL
SELECT '苹果',1500,300
GO
--创建折扣表
CREATE TABLE #AppleRebate (
[Name] NVARCHAR(10)
,[Quantity] INT
,[Rebate] FLOAT
)
GO
INSERT INTO #AppleRebate (
[Name]
,[Quantity]
,[Rebate]
)
SELECT '苹果',500,0.95 UNION ALL
SELECT '苹果',1000,0.90 UNION ALL
SELECT '苹果',1500,0.85
GO
--
SELECT * FROM #ApplePrice
SELECT * FROM #AppleRebate
--
SELECT
T1.Name
,T1.Quantity
,T1.Amount
,(SELECT TOP 1 Rebate
FROM #AppleRebate
WHERE #AppleRebate.Name = T1.Name
AND #AppleRebate.Quantity >= T1.Quantity
ORDER BY #AppleRebate.Quantity ASC) AS Rebate
FROM #ApplePrice AS T1
/*删除临时表
DROP TABLE #ApplePrice
DROP TABLE #AppleRebate
*/--望求交流啊.~睡了.~
create table t1
(
[name] varchar(10),
num int,
price float
)
insert into t1 values('苹果',200,50)
insert into t1 values('苹果',500,100)
insert into t1 values('苹果',800,150)
insert into t1 values('苹果',1000,200)
insert into t1 values('苹果',1200,250)
insert into t1 values('苹果',1500,300)
create table t2
(
[name] varchar(10),
num int,
discount float
)
insert into t2 values('苹果',500,0.95)
insert into t2 values('苹果',1000,0.90)
insert into t2 values('苹果',1500,0.85)
-------------------------------------
select *,
(select max(discount) from t2 where num>=t1.num)
from t1
苹果 200 50 0.95
苹果 500 100 0.95
苹果 800 150 0.9
苹果 1000 200 0.9
苹果 1200 250 0.85
苹果 1500 300 0.85
看看这个是不是你想要的
--创建价格表
CREATE TABLE #ApplePrice
(
[Name] NVARCHAR(10) ,
[Quantity] INT ,
[Amount] MONEY
)
GO
INSERT INTO #ApplePrice ( [Name] ,[Quantity] ,[Amount])
SELECT '苹果',200,50
UNION ALL SELECT '苹果',500,100
UNION ALL SELECT '苹果',800,150
UNION ALL SELECT '苹果',1000,200
UNION ALL SELECT '苹果',1200,250
UNION ALL SELECT '苹果',1500,300
GO
--创建折扣表
CREATE TABLE #AppleRebate
(
[Name] NVARCHAR(10) ,
[QuantityMin] INT ,
[QuantityMax] INT,
[Rebate] FLOAT
)
GO
INSERT INTO #AppleRebate ([Name],[QuantityMin],[QuantityMax],[Rebate])
SELECT '苹果',0,500,0.95
UNION ALL
SELECT '苹果',501,1000,0.90
UNION ALL
SELECT '苹果',1001,1500,0.85GO
SELECT * FROM #ApplePrice
SELECT * FROM #AppleRebate
/*查询结果*/
select p.Name, p.Quantity, p.Amount,r.Rebate
from #ApplePrice p
inner join #AppleRebate r on p.Name=r.Name and p.Quantity between r.QuantityMin and r.QuantityMax
GO
/*删除临时表*/
DROP TABLE #ApplePrice
DROP TABLE #AppleRebate
GO
我觉得你的折扣表,应该是针对一个数量区间而言的,譬如数量在1-500之间的,我给你0.95的折扣,依此类推,所以我在你的折扣表上加了一个字段,这样就可以表示一个数量区间了,这样的话查询就很简单,而且性能也会非常的好。