请问这三条语句能否用一条存储语句搞定

unicode 2009-11-17 11:18:28
想把下面的三条语句合并为一条存储语句,本人对sql就懂这么点,请帮忙合并一下。还有该怎么设计参数。 多谢

select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where A.Type = 1 and ( B.Type = 2 or B.Type = 3)

select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where (A.Type = 2 or A.Type = 3) and ( B.Type = 4)

select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where A.Type = 1 and B.Type = 5
...全文
98 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiequanqin 2009-11-17
  • 打赏
  • 举报
回复
select A.Id,(case when A.Type = 1 and ( B.Type = 2 or B.Type = 3) then datediff(n,A.CreatedOn, B.CreatedOn) end) as Duration1,
(case when (A.Type = 2 or A.Type = 3) and ( B.Type = 4) then datediff(n,A.CreatedOn, B.CreatedOn) end) as Duration2,
(case when A.Type = 1 and B.Type = 5 then datediff(n,A.CreatedOn, B.CreatedOn) end) as Duration3
from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where (( B.Type = 2 or B.Type = 3) and (A.Type = 1 or B.Type = 4)) or (A.Type = 1 and B.Type = 5 )
icelovey 2009-11-17
  • 打赏
  • 举报
回复


直接这样? OR的效率好像不高.
select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where A.Type = 1 and ( B.Type = 2 or B.Type = 3)
union all
select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where (A.Type = 2 or A.Type = 3) and ( B.Type = 4)
union all
select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where A.Type = 1 and B.Type = 5

zqtoo 2009-11-17
  • 打赏
  • 举报
回复
select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where (A.Type = 1 and ( B.Type = 2 or B.Type = 3)) or ((A.Type = 2 or A.Type = 3) and ( B.Type = 4))
or (A.Type = 1 and B.Type = 5)
--小F-- 2009-11-17
  • 打赏
  • 举报
回复
Create proc [dbo].[UserAdd] 
  (
  @datetime1 datetime,     
  @datetime2 datetime,
  @datetime3 datetime
  )
  as
begin
...
end
unicode 2009-11-17
  • 打赏
  • 举报
回复
我那三条语句每条都返回一个时间段,所以我想要合并后的语句同样能返回三个对应的时间段
--小F-- 2009-11-17
  • 打赏
  • 举报
回复
存储过程需要返回值啊

你具体需要返回的值是什么???

--小F-- 2009-11-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 unicode 的回复:]
minute
[/Quote]

呵呵 我错了 确实是n

unicode 2009-11-17
  • 打赏
  • 举报
回复
minute
--小F-- 2009-11-17
  • 打赏
  • 举报
回复
datediff(n,A.CreatedOn, B.CreatedOn)

n是代表什么?
unicode 2009-11-17
  • 打赏
  • 举报
回复
ls,可以用啊,我在sql 2005上是可以的。 另外,你这只返回一个值啊,那三条语句是分别返回三个值的,所以新的语句也需要返回三个值
jiangshun 2009-11-17
  • 打赏
  • 举报
回复
datediff(n,A.CreatedOn, B.CreatedOn) datediff 能这样用吗?

select A.Id, datediff(n,A.CreatedOn, B.CreatedOn) as Duration from Table A with (nolock)
left join Table B with (nolock) on A.Id = B.Id
where (A.Type = 1 and ( B.Type = 2 or B.Type = 3)) or ((A.Type = 2 or A.Type = 3) and ( B.Type = 4))
or (A.Type = 1 and B.Type = 5)


27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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