我需要根据布尔值在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
答案 0 :(得分:1)
根据伪代码,您可能需要编辑
IF boolean = true
ConfirmedVolume > ExpectedValue
变成类似
( (boolean = true and ConfirmedVolume > ExpectedValue) OR (boolean = false) )