SQL语句中的If逻辑
我想做的应该很简单,但不知何故,我无法找到问题的正确答案。 我以前也问过类似的问题,但以前给我的答案不再符合要求。 下面是发生的事情-我需要以不同于通常的方式从数据库中的表中有条件地选择值,如下所示: 表:SQL语句中的If逻辑,sql,select,Sql,Select,我想做的应该很简单,但不知何故,我无法找到问题的正确答案。 我以前也问过类似的问题,但以前给我的答案不再符合要求。 下面是发生的事情-我需要以不同于通常的方式从数据库中的表中有条件地选择值,如下所示: 表: Id int(非空) ParentId int(非空) EventOn日期时间(非空) 用户int(空) 通过以下方式选择: SELECT RST.* FROM RangeSheetTime RST WHERE RST.[User] is not null (在上面的例子中,我获取用户
- Id int(非空)
- ParentId int(非空)
- EventOn日期时间(非空)
- 用户int(空)
SELECT RST.* FROM RangeSheetTime RST
WHERE RST.[User] is not null
(在上面的例子中,我获取用户不为null的所有行)
(在上述情况下,我采用用户为null的所有行)
那么我想做什么呢?我想构建一个select语句,当给定一个条件(例如EventOn然后仅显示该StartEnd的非空行。但如果此条件没有非空行,则会显示空值。我希望我现在已经清楚了。您应该研究一下
情况…WHEN
构造,这相当于SQL中的IF…THEN
:
如果使用sqlserver,请查看case语句,它可能会执行您想要的操作。 选择a,b, 如果c不为空,则为“不为空条件” else的值是空条件 完[丙] 从…起 表T 哪里 状况 如果使用oracle,我认为您可以使用decode 干杯。这可能有用:
SELECT RST.* FROM RangeSheetTime RST
WHERE RST.[User] IS NOT NULL
UNION ALL
SELECT RST.* FROM RangeSheetTime RST
WHERE RST.[User] IS NULL
AND NOT EXISTS (
SELECT Id FROM RangeSheetTime
WHERE [User] IS NOT NULL
)
目前我没有办法对此进行测试,因此无法保证。哈拉根,你是在询问检索条件还是SQL结果的操作/显示,如果你是在询问结果的操作或显示,那么案例将有助于你的其他方面。在我看来,Union看起来不错。id,parentid用户,StartOrEnd 1,31,null,1 2,31,90,1 3,31,null,0 现在你想说,如果startorEnd对一个父id有多个值,你只想看到user不为null的行,否则user为null的行就会出现
如果是这样的话,杰夫给出的答案应该行得通这个问题的最终解决方案并不容易,但在我们的头撞到墙上之后,我和一个队友把它弄下来了。虽然本主题的每一篇帖子都对我们有所帮助,但没有一篇能够真正获得我们想要的价值观,总是有缺陷的,但我们非常感谢所有提供的帮助,没有这些帮助,我们现在很难找到答案 因此,解决方案如下:
select
P.ParentId RangeSheet
,coalesce(max(P.[End]), max(P.Start)) EventOn
,case when isnull(max(P.[End]), 0) = 0 then 1 else 0 end StartOrEnd
from
(
select
RST.ParentId
,case RST.StartOrEnd when 1 then RST.EventOn else null end Start
,case RST.StartOrEnd when 0 then RST.EventOn else null end [End]
from
(
select
coalesce(MAN.ParentId, AUT.ParentId) ParentId
,coalesce(MAN.StartOrEnd, AUT.StartOrEnd) StartOrEnd
,coalesce(max(MAN.CreatedOn), max(AUT.CreatedOn)) CreatedOn
from
(
-- Obter os manuais
select
RST.ParentId
,RST.StartOrEnd
,MAX(RST.CreatedOn) CreatedOn
from RangeSheetTime RST
where RST.[User] is not null
group by RST.ParentId, RST.StartOrEnd
) MAN
full outer join
(
-- Obter os automáticos
select
RST.ParentId
,RST.StartOrEnd
,MAX(RST.CreatedOn) CreatedOn
from RangeSheetTime RST
where RST.[User] is null
group by RST.ParentId, RST.StartOrEnd
) AUT on MAN.ParentId=AUT.ParentId and MAN.StartOrEnd=AUT.StartOrEnd
group by coalesce(MAN.ParentId, AUT.ParentId), coalesce(MAN.StartOrEnd, AUT.StartOrEnd)
) FOJ
inner join RangeSheetTime RST on FOJ.ParentId=RST.ParentId and FOJ.StartOrEnd=RST.StartOrEnd and FOJ.CreatedOn=RST.CreatedOn
) P
group by P.ParentId
+1,您的链接是针对MS SQL的,但这也是Oracle中最直接的方式,因为8i或9i(
decode
使live变得更难:))。顺便说一句,其他RDBMS实现了什么情况?理论上,每个RDBMS都支持它,因为它是SQL-92中定义的标准(理论上-是:)。Oracle支持是从2002年开始的(9i),我尝试过做一些类似于您的事情,但我无法获得正确的语法。以下是我到目前为止所做的:选择RST.ParentId,RST.EventOn,CASE WHEN RST.[User]不为null,则RST.[User]不为null,否则RST.[User]为null,来自RangeSheetTime RST,其中RST.ParentId=3550我现在正在测试它,杰夫,但到目前为止我已经喜欢它了,谢谢。我在我的主要帖子中对此进行了更好的解释:它不适合评论。
select
P.ParentId RangeSheet
,coalesce(max(P.[End]), max(P.Start)) EventOn
,case when isnull(max(P.[End]), 0) = 0 then 1 else 0 end StartOrEnd
from
(
select
RST.ParentId
,case RST.StartOrEnd when 1 then RST.EventOn else null end Start
,case RST.StartOrEnd when 0 then RST.EventOn else null end [End]
from
(
select
coalesce(MAN.ParentId, AUT.ParentId) ParentId
,coalesce(MAN.StartOrEnd, AUT.StartOrEnd) StartOrEnd
,coalesce(max(MAN.CreatedOn), max(AUT.CreatedOn)) CreatedOn
from
(
-- Obter os manuais
select
RST.ParentId
,RST.StartOrEnd
,MAX(RST.CreatedOn) CreatedOn
from RangeSheetTime RST
where RST.[User] is not null
group by RST.ParentId, RST.StartOrEnd
) MAN
full outer join
(
-- Obter os automáticos
select
RST.ParentId
,RST.StartOrEnd
,MAX(RST.CreatedOn) CreatedOn
from RangeSheetTime RST
where RST.[User] is null
group by RST.ParentId, RST.StartOrEnd
) AUT on MAN.ParentId=AUT.ParentId and MAN.StartOrEnd=AUT.StartOrEnd
group by coalesce(MAN.ParentId, AUT.ParentId), coalesce(MAN.StartOrEnd, AUT.StartOrEnd)
) FOJ
inner join RangeSheetTime RST on FOJ.ParentId=RST.ParentId and FOJ.StartOrEnd=RST.StartOrEnd and FOJ.CreatedOn=RST.CreatedOn
) P
group by P.ParentId