Preciso criar uma condição no where
de acordo com um boolean
.
Se boolean = true
então selecionarei as linhas com valores finais maiores que os esperados (ConfirmedVolume > ExpectedVolume
, etc) se for falso
eu seleciono tudo.
Dei uma olhada em varios links mas nao consegui passar a ideia pra minha query, por exemplo:
https://stackoverflow.com/questions/18104884/conditional-where-clause-with-case-statement-in-oracle
https://stackoverflow.com/questions/29027425/oracle-conditional-where-clause
Código:
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
Obrigado