求一SQL,给跪了

config_man 2013-09-02 01:18:05

如上图,分别为A、B表

希望获取如下结果:


文字说明:
A表:价格规则表 (每种房型在某天的价格规则表)
B表:房价表 (每种房型每天的原始价格)

他们的房型Code都是一样。
现在需要按照房型以及他们的日期来计算销售价格。

A中的EffectiveDate对应B中的RoomDate。
可以看出:
B在20130913、20130914两天,用的是A中20130913的价格规则。
B在20130915那天,用的是A中20130915那天的价格规则。
B在20130916那天,用的是A中20130916那天的价格规则

如果某天价格规则没有设置,那么按照它前面的那个(那个规则)计算。
...全文
419 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
config_man 2013-09-05
  • 打赏
  • 举报
回复
原来你说的是那种视图....
config_man 2013-09-05
  • 打赏
  • 举报
回复
引用 6 楼 jiang_qi123 的回复:
这很好弄的,在SQL Server里有个试图,你把2张表都拖进去,然后弄个链接,想查询哪一列就在前面得checkbox里打个对号就行。
找了半天,没弄出来。能费下心不?
紫菱之悠 2013-09-04
  • 打赏
  • 举报
回复
我赞同一楼和四楼的做法
ggw128 2013-09-02
  • 打赏
  • 举报
回复
赞同一楼的做法。
Shawn 2013-09-02
  • 打赏
  • 举报
回复
引用 8 楼 config_man 的回复:
一楼的改成下面这样是可以的,但不知道是否有逻辑漏洞:
一楼只是看错了一个字段。你明天可以不用来上班了,哈哈~~
select B.PMSRoomTypeCode,B.PMSRoomTypeName,B.RoomDate,B.price
    ,PriceRule=(select top 1 PriceRule from A 
        where A.PMSRoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc) 
    ,PayAmount=(select top 1 PayAmount from A 
        where A.PMSRoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc)
    ,PriceRuleType = (select top 1 PriceRuleType from A 
        where A.PMSRoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc)
from B
sunlaji008 2013-09-02
  • 打赏
  • 举报
回复
你怎么不问我,放着大神不问问其他人是不,嫌我们公司没实力呗,明天你不用来上班了。
config_man 2013-09-02
  • 打赏
  • 举报
回复
一楼的改成下面这样是可以的,但不知道是否有逻辑漏洞:

select distinct B.PMSRoomTypeCode,B.RoomDate,B.price
    ,PriceRule=(select top 1 PriceRule from A 
        where A.RoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc) 
    ,PayAmount=(select top 1 PayAmount from A 
        where A.RoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc)
    ,A.PriceRuleType 
from A,B where A.RoomTypeCode=B.PMSRoomTypeCode
config_man 2013-09-02
  • 打赏
  • 举报
回复
额,3、4楼的测试,都可以。 1楼的不行额:消息 4104,级别 16,状态 1,第 6 行 无法绑定由多个部分组成的标识符 "A.PriceRuleType"。
dota2perfectword 2013-09-02
  • 打赏
  • 举报
回复
这很好弄的,在SQL Server里有个试图,你把2张表都拖进去,然后弄个链接,想查询哪一列就在前面得checkbox里打个对号就行。
jack_110_jack 2013-09-02
  • 打赏
  • 举报
回复
1楼回答的比较好,
唐诗三百首 2013-09-02
  • 打赏
  • 举报
回复

select b.PMSRoomTypeCode,b.PMSRoomTypeName,b.RoomDate,b.Price,
       a.PriceRule,a.PayAmount,a.PriceRuleType
 from B表 b
 cross apply
 (select top 1 * from A表 a
   where b.PMSRoomTypeCode=a.RoomTypeCode 
   and cast(b.RoomDate as date)>=cast(a.EffectiveDate as date)
   order by cast(a.EffectiveDate as date) desc) a
Shawn 2013-09-02
  • 打赏
  • 举报
回复
SELECT
	b.*, t.PriceRule, t.PayAmount, t.PriceRuleType
FROM B表 b
OUTER APPLY
(
	SELECT TOP(1) PriceRule, PayAmount, PriceRuleType
	FROM A表 a
	WHERE a.RoomTypeCode = b.PMSRoomTypeCode
		AND a.EffectiveDate <= b.RoomDate
	ORDER BY a.EffectiveDate DESC
) t
---涛声依旧--- 2013-09-02
  • 打赏
  • 举报
回复
瞧标题,不用跪啊,提出问题就行了,这里的人很热心的,一样会给解答的
Andy__Huang 2013-09-02
  • 打赏
  • 举报
回复
select B.PMSRoomTypeCode,B.PMSRoomTypeName,B.RoomDate,B.price
	,PriceRule=(select top 1 PriceRule from A 
		where A.PMSRoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc) 
	,PayAmount=(select top 1 PayAmount from A 
		where A.PMSRoomTypeCode=B.PMSRoomTypeCode and A.EffectiveDate<=B.RoomDate order by A.EffectiveDate desc)
	,B.PriceRuleType 
from B

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧