SQL if function - sql-server

I have a view in SQL and I need to get an if function in there, below is my statement.
SELECT TOP (100) PERCENT
dbo.SorDetail.MLineShipDate,
dbo.SorMaster.SalesOrder,
dbo.SorDetail.SalesOrderLine,
dbo.SorMaster.CustomerPoNumber,
dbo.SorMaster.Customer,
dbo.ArCustomer.Name AS CustomerName,
dbo.SorMaster.Salesperson,
dbo.InvMaster.StockCode,
dbo.InvMaster.Description,
dbo.InvMaster.ProductClass,
dbo.ShortOrderOrigOrderQty.OrderUom,
dbo.ShortOrderOrigOrderQty.OrderQty AS OrderQtyOrig,
dbo.ShortOrderOrigOrderQty.LineValue AS LineValueOrig,
ISNULL(dbo.ShortOrderTrimQty.TrimQty, 0) AS OrderQtyTrim,
dbo.SorDetail.MPrice * ISNULL(dbo.ShortOrderTrimQty.TrimQty, 0) AS LineValueTrim,
dbo.SorDetail.MOrderQty AS OrderQtyCurrent,
dbo.SorDetail.MPrice * dbo.SorDetail.MOrderQty AS LineValueCurrent,
dbo.ShortInvoiced.QtyInvoiced AS OrderQtyPicked,
ISNULL(dbo.ShortOrderPicked.NetSalesValue, 0) AS LineValuePicked,
ISNULL(dbo.ShortInvoiced.QtyInvoiced, 0) / dbo.ShortOrderOrigOrderQty.OrderQty * 100 AS [InFull%],
ISNULL(dbo.ShortInvoiced.QtyInvoiced, 0) / dbo.SorDetail.MOrderQty * 100 AS [StockAvailability%],
dbo.SorDetail.MWarehouse,
dbo.SorDetail.MStockDes,
dbo.SorMaster.OrderStatus,
dbo.SorMaster.InterWhSale
FROM dbo.ShortOrderTrimQty
RIGHT OUTER JOIN dbo.SorMaster
INNER JOIN dbo.ShortInvoiced ON dbo.SorMaster.SalesOrder = dbo.ShortInvoiced.SalesOrder
RIGHT OUTER JOIN dbo.SorDetail ON dbo.ShortInvoiced.SalesOrderLine = dbo.SorDetail.SalesOrderLine
AND dbo.ShortInvoiced.SalesOrder = dbo.SorDetail.SalesOrder
AND dbo.SorMaster.SalesOrder = dbo.SorDetail.SalesOrder
LEFT OUTER JOIN dbo.ShortOrderPicked ON dbo.SorDetail.SalesOrderLine = dbo.ShortOrderPicked.SalesOrderLine
AND dbo.SorDetail.SalesOrder = dbo.ShortOrderPicked.SalesOrder
ON dbo.ShortOrderTrimQty.SalesOrderLine = dbo.SorDetail.SalesOrderInitLine
AND dbo.ShortOrderTrimQty.SalesOrder = dbo.SorDetail.SalesOrder
LEFT OUTER JOIN dbo.ShortOrderOrigOrderQty ON dbo.SorDetail.SalesOrderInitLine = dbo.ShortOrderOrigOrderQty.SalesOrderLine
AND dbo.SorDetail.SalesOrder = dbo.ShortOrderOrigOrderQty.SalesOrder
LEFT OUTER JOIN dbo.InvMaster ON dbo.SorDetail.MStockCode = dbo.InvMaster.StockCode
LEFT OUTER JOIN dbo.InvCustProdType ON dbo.SorDetail.MStockCode = dbo.InvCustProdType.KeyField
LEFT OUTER JOIN dbo.ArCustomer ON dbo.SorMaster.Customer = dbo.ArCustomer.Customer
WHERE (dbo.SorDetail.LineType = '1')
AND (dbo.SorMaster.OrderStatus <> '*')
AND (dbo.SorMaster.DocumentType IN ('B', 'O'))
AND (dbo.SorMaster.Customer <> 'FAC002')
AND (dbo.SorMaster.OrderStatus <> '\')
ORDER BY dbo.SorMaster.SalesOrder, dbo.SorDetail.SalesOrderLine
Okay so I need the IF statement to do the following:
IF (IntWhSales = Y, then OrderQtyOrig = OrderQtyCurrent) else OrderQtyOrig
PLEASE help me

The equivalent IF statement in SQL for your request is:
CASE
WHEN IntWhSales = 'Y'
THEN OrderQtyCurrent
ELSE OrderQtyOrig
END
You just plug this code into the SELECT part of your query.
SELECT TOP (100) PERCENT dbo.SorDetail.MLineShipDate
, dbo.SorMaster.SalesOrder
, dbo.SorDetail.SalesOrderLine
, dbo.SorMaster.CustomerPoNumber
, dbo.SorMaster.Customer
...
, CASE
WHEN IntWhSales = 'Y'
THEN OrderQtyCurrent
ELSE OrderQtyOrig
END AS [OrderQuantity]
FROM dbo.ShortOrderTrimQty
RIGHT OUTER JOIN dbo.SorMaster
...

Add this to your select statement:
Case IntWhSales = 'Y' Then OrderQtyCurrent Else OrderQtyOrig End AS [OrderQuantity]

Related

Using an except statement with Alias tables

I probably didn't label that right but am not all that great at sql. The short of the problem is I am having to find parts that are used to make up a product, but are use ONLY for that product and not any other product. I was trying to accomplish this with e except statement but kept getting an error with the end and i'm stuck. Any help would be appreciated. Thanks.
Select A.Component_Part From (Select * From (SELECT T.Component_Part, View_BOM_FG_ALL_v2_1.FGITEM, IM.IM_Inv_Type
FROM IM INNER JOIN
View_BOM_FG_ALL_v2 AS View_BOM_FG_ALL_v2_1 ON IM.IM_Part_Nbr = View_BOM_FG_ALL_v2_1.FGITEM RIGHT OUTER JOIN
(SELECT Component_Part
FROM View_BOM_FG_ALL_v2
WHERE (FGITEM = 'SPZ-9')) AS T ON View_BOM_FG_ALL_v2_1.Component_Part = T.Component_Part
WHERE (View_BOM_FG_ALL_v2_1.FGITEM <> 'SPZ-9') AND (T.Component_Part NOT LIKE '%30-') and IM.IM_Inv_Type = 'F') AS A
Join
(Select * From (SELECT C.Component_Part, View_BOM_FG_ALL_v2_1.FGITEM, IM.IM_Inv_Type
FROM IM INNER JOIN
View_BOM_FG_ALL_v2 AS View_BOM_FG_ALL_v2_1 ON IM.IM_Part_Nbr = View_BOM_FG_ALL_v2_1.FGITEM RIGHT OUTER JOIN
(SELECT Component_Part
FROM View_BOM_FG_ALL_v2
WHERE (FGITEM = 'SPZ-9')) AS C ON View_BOM_FG_ALL_v2_1.Component_Part = C.Component_Part
WHERE (View_BOM_FG_ALL_v2_1.FGITEM = 'SPZ-9') AND (C.Component_Part NOT LIKE '%30-') and IM.IM_Inv_Type = 'F') AS B
Except
Select * From (SELECT T.Component_Part, View_BOM_FG_ALL_v2_1.FGITEM, IM.IM_Inv_Type
FROM IM INNER JOIN
View_BOM_FG_ALL_v2 AS View_BOM_FG_ALL_v2_1 ON IM.IM_Part_Nbr = View_BOM_FG_ALL_v2_1.FGITEM RIGHT OUTER JOIN
(SELECT Component_Part
FROM View_BOM_FG_ALL_v2
WHERE (FGITEM = 'SPZ-9')) AS T ON View_BOM_FG_ALL_v2_1.Component_Part = T.Component_Part
WHERE (View_BOM_FG_ALL_v2_1.FGITEM <> 'SPZ-9') AND (T.Component_Part NOT LIKE '%30-') and IM.IM_Inv_Type = 'F') AS A

LEFT OUTER JOIN MATCHING

I have the following query, I modified it to be able to look in a new table called Futuredial_Validation, I wrote all the conditions and the joins but its still don't looking in the table, just in the one called cellscripter, the rest of the query works, I really appreciate any help, thanks:
SELECT row_number() OVER (ORDER BY wms_us.wms_us.rrno.rrdate, wms_us.wms_us.rrno.pono, wms_us.wms_us.transferboxdet.meidhex, att.Date_cleared) AS ROW_ID,
wms_us.wms_us.rrno.pono AS ATT_PO,
wms_us.wms_us.rrno.rrdate AS Received_Date,
CASE
WHEN (wms_us.wms_us.model.modeldesc = 'MIXED')
THEN wms_us.wms_us.transferboxdet.basemodel
ELSE wms_us.wms_us.model.modelbase
END AS Model,
CASE
WHEN LEN(wms_us.wms_us.transferboxdet.meidhex) >= 15
THEN LEFT(wms_us.wms_us.transferboxdet.meidhex, 14)
ELSE wms_us.wms_us.transferboxdet.meidhex
END AS IMEI,
/*Check in Futerdial else obtain the values from cellscripter*/
CASE
WHEN (Future.[Error Code] = '1')
THEN Future.LocalTime
ELSE att.Date_cleared
END AS Date_cleared,
CASE
WHEN (Future.[Error Code] = '1')
THEN 'PASS'
ELSE att.Result
END AS Result
FROM wms_us.wms_us.transferboxdoc
INNER JOIN wms_us.wms_us.transferboxdet
ON wms_us.wms_us.transferboxdoc.transferboxdocid = wms_us.wms_us.transferboxdet.transferboxdocid
INNER JOIN wms_us.wms_us.rrno
ON wms_us.wms_us.transferboxdet.rrnoid = wms_us.wms_us.rrno.rrnoid
INNER JOIN wms_us.wms_us.model
ON wms_us.wms_us.transferboxdoc.modelid = wms_us.wms_us.model.modelid
LEFT OUTER JOIN Futerdial.dbo.Futuredial_Validation AS Future
ON Future.IMEI = LEFT(wms_us.wms_us.transferboxdet.meidhex, 14)
LEFT OUTER JOIN DRSCSQLQADB01.att_view2.dbo.attview2 AS att
ON att.IMEI = LEFT(wms_us.wms_us.transferboxdet.meidhex, 14)
WHERE (wms_us.wms_us.rrno.rrdate > '2016-12-01')
Most likely: either your join specification is not working or the case expression for the [Error Code] is not correct.
Check if you get any values for [Future_imei] and [Error Code] with this:
select
row_id = row_number() over (
order by rrno.rrdate
, rrno.pono
, tbdet.meidhex
, att.Date_cleared)
, att_po = rrno.pono
, Received_Date = rrno.rrdate
, Model=case
when (m.modeldesc = 'mixed')
then tbdet.basemodel
else m.modelbase
end
, imei=case
when len(tbdet.meidhex) >= 15
then left(tbdet.meidhex, 14)
else tbdet.meidhex
end
, [Future_imei] = Future.imei
, Future.[Error Code]
, Date_cleared=case
when (Future.[Error Code] = '1')
then Future.LocalTime
else att.Date_cleared
end
, [Result]=case
when (Future.[Error Code] = '1')
then 'pass'
else att.Result
end
from wms_us.wms_us.transferboxdoc as tbdoc
inner join wms_us.wms_us.transferboxdet as tbdet
on tbdoc.transferboxdocid = tbdet.transferboxdocid
inner join wms_us.wms_us.rrno as rrno
on tbdet.rrnoid = rrno.rrnoid
inner join wms_us.wms_us.model as m
on tbdoc.modelid = m.modelid
left outer join Futerdial.dbo.Futuredial_Validation as Future
on Future.imei = left(tbdet.meidhex, 14)
left outer join drscsqlqadb01.att_view2.dbo.attview2 as att
on att.imei = left(tbdet.meidhex, 14)
where (rrno.rrdate > '2016-12-01')

SQL Server CASE WHEN in WHERE clause

I have a query with some joins. One of the joins depends of another value.
This is my query:
SELECT *
FROM CLIIRE A
LEFT JOIN CLIIOR B ON A.PTA = B.PTA AND A.ORD = B.ORD AND A.ITE = B.ITE
LEFT JOIN CLIORD C ON C.PTA = B.PTA AND C.ORD = B.ORD
LEFT JOIN CLIPAC D ON C.OPA = D.ORI AND C.PAC = D.PAC
LEFT JOIN CLIREN E ON E.NRE = A.NRE AND D.INS = E.INS AND A.CPT = E.CPT
LEFT JOIN
(SELECT *
FROM CLINOM
WHERE CLA = (CASE WHEN B.PFA = '88882' THEN 'MD' ELSE 'BU' END )) AS N ON B.PFA = N.COD
LEFT JOIN
(SELECT *
FROM CLIMED WHERE PRE = '') AS M ON B.PFA = M.MED
LEFT JOIN CLIPRF P ON B.PRF = P.PRF
WHERE
(D.INS LIKE 'OM%' OR D.INS LIKE 'SOL%')
AND E.NFA IN ('5188')
AND A.CPT IN ('fi', 'pi')
ORDER BY
E.NFA
But I'm getting error in the line:
LEFT JOIN (SELECT * FROM CLINOM WHERE CLA = (CASE WHEN B.PFA = '88882' THEN 'MD' ELSE 'BU' END )) AS N ON B.PFA=N.COD
Looks like SQL Server won't allow me to do that:
The multi-part identifier "B.PFA" could not be bound
The column B.PFA exists.
Can anyone help me? Thanks !
The problem is that the outer query reference is not available in the subquery. Happily, though the subquery is not needed. Try this logic:
LEFT JOIN CLINOM N
ON P.FA = N.COD AND
N.CLA = (CASE WHEN B.PFA = '88882' THEN 'MD' ELSE 'BU' END)
And, you can phrase this without the CASE. Assuming B.PFA is not NULL:
LEFT JOIN CLINOM N
ON P.FA = N.COD AND
((N.CLA = 'MD' AND B.PFA = '88882') OR
(N.CLA = 'BU' AND B.PFA <> '88882')
)
If B.PFA could be NULL:
LEFT JOIN CLINOM N
ON P.FA = N.COD AND
((N.CLA = 'MD' AND B.PFA = '88882') OR
(N.CLA = 'BU' AND (B.PFA <> '88882' OR B.PFS IS NULL))
)
You can also use COALESCE() if you like.

How to avoid to duplicate a subquery in a query

I'm running a query that contains the same subquery two times, one used in a inner join condition and the another in a outer join. I'm highlighting the repeated subquery with **. How can I optimize this in order to run only once?
SELECT DISTINCT dbo.tb_contato.Nome, dbo.tb_contato.id_contato, dbo.tb_contato.Sexo, dbo.tb_contato.codigo, dbo.tb_email.email
FROM dbo.tb_contato INNER JOIN
dbo.tb_email ON dbo.tb_contato.id_contato = dbo.tb_email.id_contato INNER JOIN
dbo.tb_empresa ON dbo.tb_empresa.id_empresa = dbo.tb_contato.id_empresa LEFT OUTER JOIN
(SELECT dbo.tb_interacao.IDContato AS id_contato
FROM dbo.tb_interacao INNER JOIN
**(SELECT MAX(IDInteracao) AS IDIntMax, IDPerfilParticipante AS id_perfil_participante, IDProjeto, IDContato
FROM dbo.tb_interacao AS tb_interacao_2
GROUP BY IDPerfilParticipante, IDProjeto, IDContato)** AS IntMax1 ON dbo.tb_interacao.IDInteracao = IntMax1.IDIntMax INNER JOIN
dbo.tb_projeto ON dbo.tb_interacao.IDProjeto = dbo.tb_projeto.id_projeto INNER JOIN
dbo.tb_status_processo ON dbo.tb_interacao.IDStatusProcesso = dbo.tb_status_processo.id_status_processo
WHERE (dbo.tb_projeto.id_projeto = 2057) AND (dbo.tb_interacao.IDPerfilParticipante = 1) AND (dbo.tb_status_processo.id_status_processo = 7) OR
(dbo.tb_projeto.id_projeto = 2057) AND (dbo.tb_interacao.IDPerfilParticipante = 1) AND (dbo.tb_status_processo.id_status_processo = 6) OR
(dbo.tb_interacao.IDPerfilParticipante = 1) AND (dbo.tb_status_processo.id_status_processo = 6) AND (dbo.tb_projeto.id_grupo = 55) OR
(dbo.tb_interacao.IDPerfilParticipante = 1) AND (dbo.tb_status_processo.id_status_processo = 7) AND (dbo.tb_projeto.id_grupo = 55))
AS ConvidadosOut ON dbo.tb_contato.id_contato = ConvidadosOut.id_contato INNER JOIN
(SELECT tb_interacao_1.IDContato AS id_contato
FROM dbo.tb_interacao AS tb_interacao_1 INNER JOIN
**(SELECT MAX(IDInteracao) AS IDIntMax, IDPerfilParticipante AS id_perfil_participante, IDProjeto, IDContato
FROM dbo.tb_interacao AS tb_interacao_3
GROUP BY IDPerfilParticipante, IDProjeto, IDContato)** AS IntMax2 ON tb_interacao_1.IDInteracao = IntMax2.IDIntMax INNER JOIN
dbo.tb_projeto AS tb_projeto_1 ON tb_interacao_1.IDProjeto = tb_projeto_1.id_projeto INNER JOIN
dbo.tb_status_processo AS tb_status_processo_1 ON tb_interacao_1.IDStatusProcesso = tb_status_processo_1.id_status_processo
WHERE (tb_projeto_1.id_projeto = 181) AND (IntMax2.id_perfil_participante = 1) AND (tb_status_processo_1.id_status_processo = 4) OR
(tb_projeto_1.id_projeto = 1581) AND (IntMax2.id_perfil_participante = 1) AND (tb_status_processo_1.id_status_processo = 5) OR
(IntMax2.id_perfil_participante = 1) AND (tb_status_processo_1.id_status_processo = 6) AND (tb_projeto_1.id_grupo = 62)) AS ConvidadosIn ON
dbo.tb_contato.id_contato = ConvidadosIn.id_contato
WHERE (dbo.tb_email.email_visibility = 0 OR
dbo.tb_email.email_visibility IS NULL) AND (dbo.tb_empresa.id_pais = 1) AND (dbo.tb_contato.Fonte <> 'salesloft_orange' AND
dbo.tb_contato.Fonte <> 'salesloft_int_orange' OR
dbo.tb_contato.Fonte IS NULL) AND (dbo.tb_contato.id_contato_visibility = 1 OR
dbo.tb_contato.id_contato_visibility IS NULL) AND (ConvidadosOut.id_contato IS NULL)
I hope you are using SQL server 2005 or later. You can safely try using Common table expressions for the purpose as stated in your question.
Below is an example of script from adventureworks database:
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
AS
-- Define the CTE query.
(
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

Work Around Divide By Zero Error in SQL SELECT

SELECT
*
FROM
RM_Sales_Union
WHERE
DOCDATE >= 'September 1, 2011'
AND DOCDATE < 'October 1, 2011'
AND CUSTNMBR = '2186020'
That query results in an error:
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
if run as shown.
Excluding the last line AND CUSTNMBR = '2186020' allows the query to complete.
CUSTNMBR is a char(21) field. Divide by zero has me confused.
What is the "correct" way to work around this?
RM_Sales_Union is a union query view:
SELECT ACTNUMBR_1,
ACTNUMBR_2,
ACTNUMBR_3,
ORSLSAMT,
CUSTCLAS,
CUSTNAME,
CUSTNMBR,
SLPRSNID,
DOCABREV,
CSPORNBR,
CURNCYID,
DOCDATE,
DOCNUMBR,
GLPOSTDT,
SLSAMNT,
VOIDSTTS,
SLPRSNFN,
SPRSNSLN,
DocOrigin,
ORFRTAMT,
FRTAMNT,
COMPRCNT,
TRDISAMT,
ORTDISAM,
ORMISCAMT,
ORTAXAMT,
ORCTRXAM
FROM dbo.RM_Sales_Hist
UNION
SELECT ACTNUMBR_1,
ACTNUMBR_2,
ACTNUMBR_3,
ORSLSAMT,
CUSTCLAS,
CUSTNAME,
CUSTNMBR,
SLPRSNID,
DOCABREV,
CSPORNBR,
CURNCYID,
DOCDATE,
DOCNUMBR,
GLPOSTDT,
SLSAMNT,
VOIDSTTS,
SLPRSNFN,
SPRSNSLN,
DocOrigin,
ORFRTAMT,
FRTAMNT,
COMPRCNT,
TRDISAMT,
ORTDISAM,
ORMISCAMT,
ORTAXAMT,
ORCTRXAM
FROM dbo.RM_Sales_Open
RM_Sales_Hist and RM_Sales_Open are views defined as follows:
--RM_Sales_Hist
SELECT dbo.GL_Sales_Accounts.ACTNUMBR_1,
dbo.GL_Sales_Accounts.ACTNUMBR_2,
dbo.GL_Sales_Accounts.ACTNUMBR_3,
ISNULL(dbo.MC020102.ORSLSAMT, dbo.RM30101.SLSAMNT) AS ORSLSAMT,
dbo.RM00101.CUSTCLAS,
dbo.RM00101.CUSTNAME,
dbo.RM00101.CUSTNMBR,
dbo.RM00101.SLPRSNID,
dbo.RM40401.DOCABREV,
dbo.RM30101.CSPORNBR,
dbo.RM30101.CURNCYID,
dbo.RM30101.DOCDATE,
dbo.RM30101.DOCNUMBR,
dbo.RM30101.GLPOSTDT,
dbo.RM30101.SLSAMNT,
dbo.RM30101.VOIDSTTS,
dbo.RM00301.SLPRSNFN,
dbo.RM00301.SPRSNSLN,
'HIST' AS DocOrigin,
ISNULL(dbo.MC020102.ORFRTAMT, dbo.RM30101.FRTAMNT) AS ORFRTAMT,
dbo.RM30101.FRTAMNT,
dbo.RM00301.COMPRCNT,
dbo.RM30101.TRDISAMT,
ISNULL(dbo.MC020102.ORTDISAM, 0) AS ORTDISAM,
ISNULL(dbo.MC020102.ORMISCAMT, 0) AS ORMISCAMT,
ISNULL(dbo.MC020102.ORTAXAMT, 0) AS ORTAXAMT,
ISNULL(dbo.MC020102.ORCTRXAM, 0) AS ORCTRXAM,
dbo.RM00101.STATE
FROM dbo.GL_Sales_Accounts
INNER JOIN dbo.RM30301
ON dbo.GL_Sales_Accounts.DSTINDX = dbo.RM30301.DSTINDX
INNER JOIN dbo.RM30101
ON dbo.RM30301.CUSTNMBR = dbo.RM30101.CUSTNMBR
AND dbo.RM30301.DOCNUMBR = dbo.RM30101.DOCNUMBR
INNER JOIN dbo.RM00101
ON dbo.RM30101.CUSTNMBR = dbo.RM00101.CUSTNMBR
INNER JOIN dbo.RM40401
ON dbo.RM30101.RMDTYPAL = dbo.RM40401.RMDTYPAL
INNER JOIN dbo.RM00301
ON dbo.RM00101.SLPRSNID = dbo.RM00301.SLPRSNID
LEFT OUTER JOIN dbo.MC020102
ON dbo.RM30301.DOCNUMBR = dbo.MC020102.DOCNUMBR
WHERE ( CAST(dbo.RM30301.DOCNUMBR AS VARCHAR(21)) NOT IN (SELECT
CAST(
DOCNUMBR AS
VARCHAR(21)) AS
Expr1
FROM
dbo.Invoices_With_Display_Discounts) )
--RM_Sales_Open
SELECT dbo.GL_Sales_Accounts.ACTNUMBR_1,
dbo.GL_Sales_Accounts.ACTNUMBR_2,
dbo.GL_Sales_Accounts.ACTNUMBR_3,
ISNULL(dbo.MC020102.ORSLSAMT, 0) AS ORSLSAMT,
dbo.RM00101.CUSTCLAS,
dbo.RM00101.CUSTNAME,
dbo.RM00101.CUSTNMBR,
dbo.RM00101.SLPRSNID,
dbo.RM40401.DOCABREV,
dbo.RM20101.CSPORNBR,
dbo.RM20101.CURNCYID,
dbo.RM20101.DOCDATE,
dbo.RM20101.DOCNUMBR,
dbo.RM20101.GLPOSTDT,
dbo.RM20101.SLSAMNT,
dbo.RM20101.VOIDSTTS,
dbo.RM00301.SLPRSNFN,
dbo.RM00301.SPRSNSLN,
'OPEN' AS DocOrigin,
ISNULL(dbo.MC020102.ORFRTAMT, 0) AS ORFRTAMT,
dbo.RM20101.FRTAMNT,
dbo.RM00301.COMPRCNT,
dbo.RM20101.TRDISAMT,
ISNULL(dbo.MC020102.ORTDISAM, 0) AS ORTDISAM,
ISNULL(dbo.MC020102.ORMISCAMT, 0) AS ORMISCAMT,
ISNULL(dbo.MC020102.ORTAXAMT, 0) AS ORTAXAMT,
ISNULL(dbo.MC020102.ORCTRXAM, 0) AS ORCTRXAM,
dbo.RM00101.STATE
FROM dbo.GL_Sales_Accounts
INNER JOIN dbo.RM10101
ON dbo.GL_Sales_Accounts.DSTINDX = dbo.RM10101.DSTINDX
INNER JOIN dbo.RM20101
ON dbo.RM10101.CUSTNMBR = dbo.RM20101.CUSTNMBR
AND dbo.RM10101.DOCNUMBR = dbo.RM20101.DOCNUMBR
INNER JOIN dbo.RM00101
ON dbo.RM20101.CUSTNMBR = dbo.RM00101.CUSTNMBR
INNER JOIN dbo.RM40401
ON dbo.RM20101.RMDTYPAL = dbo.RM40401.RMDTYPAL
INNER JOIN dbo.RM00301
ON dbo.RM00101.SLPRSNID = dbo.RM00301.SLPRSNID
LEFT OUTER JOIN dbo.MC020102
ON dbo.RM10101.DOCNUMBR = dbo.MC020102.DOCNUMBR
WHERE ( CAST(dbo.RM20101.DOCNUMBR AS VARCHAR(21)) NOT IN (SELECT
CAST(
DOCNUMBR AS
varchar(21)) AS
Expr1
FROM
dbo.Invoices_With_Display_Discounts) )
I guess that RM_Sales_Union is a view that contains a division. The filter simply changes something like a COUNT or SUM to give zero, and it is this that is used as divisor
Change it to use something/NULLIF(..., 0) to make the expressions give NULL. Or ISNULL(something/NULLIF(..., 0), 0) to get NULL instead

Resources