SQL Server CASE WHEN in WHERE clause - sql-server

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.

Related

optimize the query, my query taking more than 5 minutes to execute

my query taking more than 5 minutes to execute can anyone tell me what's wrong in this query, below is the query I try to optimize it but no luck
SELECT DISTINCT
'Freight' AS Expr1000,
'Acknowledged Trip Details' AS detailtype,
sCompanyDesc,
VehicleRegistration.sVehicleRegistrationNumber,
Token.iTokenId,
sTokenNumber,
Token.dtTokenGenerating AS TokenDate,
LoadAssign.sCustomerInvoiceNumber,
dtCustomerInvoiceDate,
FillingPoint.sFillingPointName AS [From],
DecantingPoint.sDecantingPointName AS [To],
CloseLoad.fExpectedFreightAmount AS fExpectedFreightAmount,
ISNULL(PayAdvance.fAdvanceAmount, 0) AS fAdvanceAmount,
CloseLoad.fCommissionAmount AS fCommissionAmount,
CloseLoad.fDecantedQuantity,
0 AS [Shortage Quantity],
0 AS [Shortage Rate],
0 AS [Shortage Amount],
sProductName,
ISNULL((SELECT SUM(ISNULL(JVDetails.fCreditAmount, 0))
FROM Transactions
INNER JOIN JV ON JV.sJVCode = Transactions.sTransactionCode
INNER JOIN JVDetails ON JVDetails.iJVId = JV.iJVId
WHERE CloseLoad.iTokenId = Transactions.iTokenId
AND JV.sDescription = 'Munshiana'),
0) AS Munshiana,
ISNULL(JV.sJVCode, '') + ISNULL(PV.sPVCode, '') AS advdocnum
FROM CloseLoad
INNER JOIN Token ON CloseLoad.iTokenId = Token.iTokenId
INNER JOIN LoadAssign ON CloseLoad.iLoadId = LoadAssign.iLoadId
INNER JOIN FillingPoint ON Token.iFillingPointId = FillingPoint.iFillingPointId
INNER JOIN DecantingPoint ON LoadAssign.iDecantingPointId = DecantingPoint.iDecantingPointId
INNER JOIN Product ON LoadAssign.iProductId = Product.iProductId
INNER JOIN VehicleRegistration ON Token.iVehicleRegistrationId = VehicleRegistration.iVehicleRegistrationId
INNER JOIN Company ON Token.iCompanyId = Company.iCompanyId
LEFT JOIN PayAdvance ON PayAdvance.iTokenId = Token.iTokenId
AND PayAdvance.bIsApproved = 1
LEFT JOIN PV ON PV.iPayAdvanceId = PayAdvance.iPayAdvanceId
AND PayAdvance.bIsApproved = 1
AND PV.bIsApproved = 1
LEFT JOIN JV ON JV.iPayAdvanceId = PayAdvance.iPayAdvanceId
AND PayAdvance.bIsApproved = 1
AND JV.bIsApproved = 1
WHERE CloseLoad.iLoadId NOT IN (SELECT tfrli.iLoadId
FROM TempFreightReceiptLoadInfo tfrli
INNER JOIN FreightReceiptChqInfo ci ON ci.iFreightReceiptChqInfoId = tfrli.iFreightReceiptChqInfoId
INNER JOIN LoadAssign l1 ON l1.iLoadId = tfrli.iLoadId
WHERE ci.bIsApproved = 1
AND l1.dtCustomerInvoiceDate BETWEEN '8/1/2020' AND '8/30/2020'
AND tfrli.sAllocationType = 'Freight')
AND LoadAssign.bIsLoadClose = 1
AND Token.iVehicleRegistrationId = 2644
AND LoadAssign.dtCustomerInvoiceDate BETWEEN '8/1/2020' AND '8/30/2020'
AND Token.iCompanyId IN (1, 2, 3)
AND Token.sDecantingPointType IN ('Domestic', 'Export')
AND LoadAssign.bIsActive = 1
AND Token.bIsLoadVerify = 1;
Execute EXPLAIN to find bottleneck.
EXPLAIN SELECT DISTINCT ...

T-SQL to MS Access SQL

I have a working query in SQL Server, but I fail to convert it to MS Access SQL.
This is the bare working query in SQL Server:
select tblKPIData.id
,tblKPIData.KPI_id
,tblKPI.KPI_Name
,tblKPIData.ImportTimestamp
,tblKPIData.Quantity
,tblKPIData.FinancialMonth
,tblKPIData.FinancialYear
,tblKPIData.Zone_id
,tblZone.ZoneName
,tblKPIData.DMA_id
,tblDMA.DMA_Name
,tblKPIData.TargetOrResult
from tblKPIData
inner join tblKPI
on tblKPI.Id = KPI_id
left outer join tblDMA
on tblDMA.Id = tblKPIData.DMA_id
left outer join tblZone
on tblZone.ID = tblKPIData.Zone_id
inner join tblDashboardKPI
on tblDashboardKPI.KPI_Id = tblKPIData.KPI_id
inner join
( select a.kpi_id
,a.financialMonth
,a.financialYear
,isnull(a.zone_id,0) as zone_id
,isnull(a.dma_id,0) as dma_id
,a.targetorresult
,max(a.importtimestamp) as importtimestamp
from tblKPIData a
group by kpi_id
,financialMonth
,financialYear
,zone_id
,dma_id
,targetorresult) as max_kpi
on (tblKPIData.KPI_id = max_kpi.KPI_id
and tblKPIData.ImportTimestamp = max_kpi.importtimestamp
and tblKPIData.FinancialMonth = max_kpi.FinancialMonth
and tblKPIData.FinancialYear = max_kpi.FinancialYear
and isnull(tblKPIData.Zone_id,0) = isnull(max_kpi.zone_id,0)
and isnull(tblKPIData.DMA_id,0) = isnull(max_kpi.dma_id,0)
and tblKPIData.TargetOrResult = max_kpi.TargetOrResult)
where tblKPIData.FinancialMonth = 'Oct'
and tblKPIData.FinancialYear = 2017
and tblKPIData.KPI_id in (select kpi_id from tblDashboardKPI where tblDashboardKPI.KPI_Id = tblKPIData.KPI_id)
and (tblKPIData.Zone_id = 5 or tblKPIData.DMA_id in (select id from tblDMA where Zoneid = 5))
AND ((tblDashboardKPI.Status) = 1)
and ((tblDashboardKPI.Dashboard_Id) = 6)
I know I have to add brackets, but not exactly sure where. This is what I came up with, but I get an error (Join Expression not supported) and it highlites a piece of code on the first join (tblKPI.Id = KPI_id):
select tblKPIData.id
,tblKPIData.KPI_id
,tblKPI.KPI_Name
,tblKPIData.ImportTimestamp
,tblKPIData.Quantity
,tblKPIData.FinancialMonth
,tblKPIData.FinancialYear
,tblKPIData.Zone_id
,tblZone.ZoneName
,tblKPIData.DMA_id
,tblDMA.DMA_Name
,tblKPIData.TargetOrResult
from (((((tblKPIData
inner join tblKPI
on tblKPI.Id = KPI_id)
left outer join tblDMA
on tblDMA.Id = tblKPIData.DMA_id)
left outer join tblZone
on tblZone.ID = tblKPIData.Zone_id)
inner join tblDashboardKPI
on tblDashboardKPI.KPI_Id = tblKPIData.KPI_id)
inner join
( select a.kpi_id
,a.financialMonth
,a.financialYear
,isnull(a.zone_id,0) as zone_id
,isnull(a.dma_id,0) as dma_id
,a.targetorresult
,max(a.importtimestamp) as importtimestamp
from tblKPIData a
group by kpi_id
,financialMonth
,financialYear
,zone_id
,dma_id
,targetorresult) as max_kpi
on (tblKPIData.KPI_id = max_kpi.KPI_id)
and tblKPIData.ImportTimestamp = max_kpi.importtimestamp
and tblKPIData.FinancialMonth = max_kpi.FinancialMonth
and tblKPIData.FinancialYear = max_kpi.FinancialYear
and isnull(tblKPIData.Zone_id,0) = isnull(max_kpi.zone_id,0)
and isnull(tblKPIData.DMA_id,0) = isnull(max_kpi.dma_id,0)
and tblKPIData.TargetOrResult = max_kpi.TargetOrResult)
where tblKPIData.FinancialMonth = 'Oct'
and tblKPIData.FinancialYear = 2017
and tblKPIData.KPI_id in (select kpi_id from tblDashboardKPI where tblDashboardKPI.KPI_Id = tblKPIData.KPI_id)
and (tblKPIData.Zone_id = 5 or tblKPIData.DMA_id in (select id from tblDMA where Zoneid = 5))
AND ((tblDashboardKPI.Status) = 1)
and ((tblDashboardKPI.Dashboard_Id) = 6)
What am I doing wrong?

SQL if function

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]

sql query assistance

I have a sql that is as under:
SELECT ib.branch_no,
ib.on_hand,
p.weightedav,
p.item_code,
FROM physical p
INNER JOIN
item_branch as ib on p.item_code = ib.item_code
WHERE ib.on_hand <> 0
This SQL returns only those branch_no that have on_hand <> 0.
I am trying to get all the branch_nos irrespective of the on_hand field, but while still using the where on_hand clause.
Taking the on_hand clause away solves my problem, but gives me large amount of un-needed rows with 0's.
I am using SQL SERVER 2008 R2.
Thanks in advance for any guidance. Please apologize if I am missing any information.
------------------------------------------ENTIRE SQL QUERY (Updated)---------------------------------------------
select
ib.branch_no,
p.weighted_av,
p.item_code,
p.x_value,
p.y_value,
ib.on_hand,
p.on_hand as PhysicalOH,
ip.price,
i.item_code as StyleCode,
i.description,
i.cat1,
i.cat2,
i.cat3,
i.cat4,
np.is_style_yn,
si.supplier_code ,
ysv.sort as YSort
from physical as p
left outer JOIN
item_branch as ib on p.item_code = ib.item_code -- and ib.on_hand <> 0
INNER JOIN
item_price as ip on p.item_code = ip.item_code and ip.price_type = 'P1'
INNER JOIN
style_values as sv on p.style_code = sv.style_code and p.x_value = sv.value
INNER JOIN
style_values as ysv on p.style_code = ysv.style_code and p.y_value = ysv.value and ysv.axis = 'Y'
INNER JOIN
ITEM as i on p.style_code = i.item_code
INNER JOIN
NON_PHYSICAL as np ON i.item_code = np.item_code and np.is_style_yn = 1
INNER JOIN
supplier_item as si ON i.item_code = si.item_code and si.pref_supp_no = 1
where --ib.on_hand <> 0 and
sv.axis = 'X' and
i.item_code in
(SELECT ITEM.item_code
FROM ITEM
INNER JOIN
NON_PHYSICAL ON ITEM.item_code = NON_PHYSICAL.item_code
LEFT JOIN
supplier_item ON Item.item_code = supplier_item.item_code and pref_supp_no = 1
WHERE NON_PHYSICAL.is_style_yn = 1 and ITEM.cat1 = 'Verge Sportswear Ltd' )
order by
si.supplier_code,
i.cat4,
i.cat3,
i.cat2,
i.cat1,
sv.sort
Try this:
SELECT ib.branch_no,
ib.on_hand,
p.weightedav,
p.item_code,
FROM physical p
INNER JOIN
item_branch as ib on (p.item_code = ib.item_code AND ib.on_hand <> 0)

Invalid identifier on oracle inner join

I'm having the error ORACLE: ORA-00904: Invalid Identifier on:
and l.cd_pergunta = e.cd_pergunta
the invalid identifier is 'e', the prefix of cd_pergunta...
When i execute this query:
select count(*)
from TEACEITE e
inner join TEREGETA re on re.cd_etapa = e.cd_etapa
and re.id_reg_neg = 1.00000000
where e.obrigatorio = 1
and not exists
(select 1
from GESESSAO s
inner join GERESPOS r on r.sessao = s.sessao_resp
and r.resposta_log = 1
inner join GEEPE l on l.cd_quest = s.cd_quest
and l.ord_perg = r.ord_pergunta
and l.cd_pergunta = e.cd_pergunta
where s.cd_quest = e.cd_quest
and s.item = e.cd_etapa
and s.origem = 'GC'
and s.os_nf_orc_cont = 1.00000000)
Any idea?
The problem is that the alias e is not available within the nested select and hence the "invalid identifier".
You can try to rewrite the query so that the conditions s.cd_quest = e.cd_quest and s.item = e.cd_etapa are part of the main select instead of the nested select.
EDIT: I tried a few scenarios and the problem is that the alias e is not available during join resolution in the nested query. Looks like you cannot refer to an outer table alias in the join condition of the inner query.
I believe that the following will work
select count(*)
from TEACEITE e
inner join TEREGETA re on re.cd_etapa = e.cd_etapa
and re.id_reg_neg = 1.00000000
where e.obrigatorio = 1
and not exists
(select 1
from GESESSAO s
inner join GERESPOS r on r.sessao = s.sessao_resp
and r.resposta_log = 1
inner join GEEPE l on l.cd_quest = s.cd_quest
and l.ord_perg = r.ord_pergunta
where l.cd_pergunta = e.cd_pergunta
and s.cd_quest = e.cd_quest
and s.item = e.cd_etapa
and s.origem = 'GC'
and s.os_nf_orc_cont = 1.00000000)
While this query may work, I am not sure if its accomplishing what you want, please ensure that your business logic is taken care of as well.
As Raam said, you can't use the alias e inside the join of the subquery.
You can also try not to use the sql1999 syntax for joins:
select count(*)
from TEACEITE e
inner join TEREGETA re on re.cd_etapa = e.cd_etapa
and re.id_reg_neg = 1.00000000
where e.obrigatorio = 1
and not exists
(select 1
from GESESSAO s, GERESPOS r , GEEPE l
where r.sessao = s.sessao_resp
and r.resposta_log = 1
and l.cd_quest = s.cd_quest
and l.ord_perg = r.ord_pergunta
and l.cd_pergunta = e.cd_pergunta
and s.cd_quest = e.cd_quest
and s.item = e.cd_etapa
and s.origem = 'GC'
and s.os_nf_orc_cont = 1.00000000)

Resources