Oracle Where 案例
Posted
技术标签:
【中文标题】Oracle Where 案例【英文标题】:Oracle Where Case 【发布时间】:2018-08-31 13:48:30 【问题描述】:我需要根据布尔值在where子句中创建一个条件。
如果为 true,则当最终值与预期值不同时(ConfirmedVolume > ExpectedVolume 等)我选择行,如果为 false,我选择所有内容。
我查看了一些链接,但无法将这个想法应用于我的查询。
例如。
Conditional WHERE clause with CASE statement in Oracle
Oracle Conditional where clause
代码:
SELECT RouteId,
Id,
RouteStatusId,
Address,
Latitude,
Longitude,
ExpectedQty,
ConfirmedQty,
FinalQty,
ExpectedWeight,
ConfirmedWeight,
FinalWeight,
ExpectedVolume,
ConfirmedVolume,
FinalVolume,
Comm,
MAX(EndTime) - MAX(StartTime),
MAX(StartTime),
MAX(EndTime),
WaitTime,
MAX(CAST(EndTime AS DATE)),
UOM,
BPName
FROM
(
SELECT
RouteStop.[RouteId] RouteId,
RouteStop.[Id] Id,
RouteStop.[RouteStatusId] RouteStatusId,
SiteAddress.[Address] Address,
SiteAddress.[Latitude] Latitude,
SiteAddress.[Longitude] Longitude,
SUM(RouteStop_Product.[ExpectedQty]) ExpectedQty,
SUM(RouteStop_Product.[ConfirmedQty]) ConfirmedQty,
SUM(RouteStop_Product.[FinalQty]) FinalQty,
SUM(RouteStop_Product.[ExpectedWeight]) ExpectedWeight,
SUM(RouteStop_Product.[ConfirmedWeight]) ConfirmedWeight,
SUM(RouteStop_Product.[FinalWeight]) FinalWeight,
SUM(RouteStop_Product.[ExpectedVolume]) ExpectedVolume,
SUM(RouteStop_Product.[ConfirmedVolume]) ConfirmedVolume,
SUM(RouteStop_Product.[FinalVolume]) FinalVolume,
RouteStop.[Comment] Comm,
CASE WHEN GPS.[EventTypeId] = @RouteStopStarted
THEN GPS.[DateTime]
ELSE CAST(NULL AS DATE)
END StartTime,
CASE WHEN GPS.[EventTypeId] = @RouteStopCompleted
OR GPS.[EventTypeId] = @RouteStopFailed
THEN GPS.[DateTime]
ELSE CAST(NULL AS DATE)
END EndTime,
GPS.[EventTypeId] EventTypeId,
RouteStop.[WaitTime] WaitTime,
Order.[UOM] UOM,
BusinessPartner.[CardName] BPName
FROM RouteStop
LEFT JOIN RouteStop_Product ON RouteStop.[Id] = RouteStop_Product.[RouteStopId]
LEFT JOIN GPS ON RouteStop.[Id] = GPS.[RouteStopID]
LEFT JOIN SiteAddress ON RouteStop.[SiteAddress] = SiteAddress.[Id]
LEFT JOIN BusinessPartner ON SiteAddress.[CardCode] = BusinessPartner.[Id]
LEFT JOIN Order ON RouteStop_Product.[OrderId] = Order.[Id]
LEFT JOIN SitePlanningArea ON SiteAddress.[Id] = SitePlanningArea.[SiteAddressId]
WHERE RouteStop.[Id] = @RouteStopId
AND (SitePlanningArea.[PlanningAreaId] = @PlanningAreaId OR @PlanningAreaId = 0)
GROUP BY RouteStop.[RouteId],
RouteStop.[Id],
RouteStop.[RouteStatusId],
SiteAddress.[Address],
SiteAddress.[Latitude],
SiteAddress.[Longitude],
RouteStop.[Comment],
GPS.[EventTypeId],
GPS.[DateTime],
RouteStop.[WaitTime],
Order.[UOM],
BusinessPartner.[CardName]
)
GROUP BY RouteId,
Id,
RouteStatusId,
Address,
Latitude,
Longitude,
ExpectedQty,
ConfirmedQty,
FinalQty,
ExpectedWeight,
ConfirmedWeight,
FinalWeight,
ExpectedVolume,
ConfirmedVolume,
FinalVolume,
Comm,
WaitTime,
UOM,
BPName
我想在哪里做类似的事情:
WHERE RouteStop.[Id] = @RouteStopId
AND (SitePlanningArea.[PlanningAreaId] = @PlanningAreaId OR @PlanningAreaId = 0)
AND IF boolean = true
ConfirmedVolume > ExpectedValue
AND ConfirmedQuantity > ExpectedQuantity
【问题讨论】:
您确定您使用的是 Oracle 吗?那些可怕的方括号看起来更像微软的 T-SQL 我正在使用 Outsystems。 outsystems 不是数据库产品。 尝试至少了解它是什么。 我明白它是什么——一个编程环境。它不是数据库。但是您的问题是寻求 SQL 语句的帮助,因此您的 IDE 或框架大多是无关紧要的(我假设所有那些无效的花括号都将在运行时被替换)。为了让您获得对您有帮助的答案,您必须告诉我们您正在使用哪种数据库产品。如果您得到一个针对 Oracle 但您使用的是 SQL Server 的答案,这对您没有帮助。这是两种截然不同的产品 【参考方案1】:伪代码推理,你可能需要编辑
IF boolean = true
ConfirmedVolume > ExpectedValue
变成类似
( (boolean = true and ConfirmedVolume > ExpectedValue) OR (boolean = false) )
【讨论】:
以上是关于Oracle Where 案例的主要内容,如果未能解决你的问题,请参考以下文章