Weird Divide by Zero Error in SQL - sql

I have this query :
-- There are a select and joins in the query
--Then I have this Where part
WHERE
(
dbo.Table.Furnished IS NULL
OR dbo.Table.Furnished = 0
)
AND (
dbo.Table.Type2 = N'House'
)
AND (
dbo.Table.Type3 = N'ForSale'
)
AND (
dbo.Table.FixedArea/ dbo.Table.ToiletNumber< 250
)
AND (
dbo.Table.FixedArea> 25
)
AND (
CASE
WHEN dbo.Table.Segment1 = 1 THEN
'FirstGroup'
WHEN dbo.Table.Segment1 = 2 THEN
'SecondGroup'
WHEN dbo.Table.Segment1 = 3 THEN
'ThirdGroup'
ELSE
'Undefined'
END <> 'Undefined'
)
AND (
dbo.Table.Segment2 <> N'Undefined'
)
OR
(
dbo.Table.Furnished IS NULL
OR dbo.Table.Furnished = 0
)
AND (
dbo.Table.Type2 = N'Commercial'
)
AND (
dbo.Table.Type3 = N'ForSale'
)
AND (
dbo.Table.FixedArea/ dbo.Table.ToiletNumber< 250
)
AND (
dbo.Table.FixedArea> 25
)
AND (
CASE
WHEN dbo.Table.Segment1 = 1 THEN
'FirstGroup'
WHEN dbo.Table.Segment1 = 2 THEN
'SecondGroup'
WHEN dbo.Table.Segment1 = 3 THEN
'ThirdGroup'
ELSE
'Undefined'
END <> 'Undefined'
)
AND (
dbo.Table.Segment2 <> N'Undefined'
)
When I add this part below at the bottom of my original query (which doesn't have any division or something like that), I get Divide by zero error encountered.
OR
(
dbo.Table.Geo IS NOT NULL
)
AND (
dbo.Table.FixedArea > 0
)
AND (
dbo.Table.Price > 0
)
AND (
dbo.Table.RoomNumber LIKE N'%+%'
)
AND (
dbo.Table.Furnished IS NULL
OR dbo.Table.Furnished = 0
)
AND(
dbo.Table.Type3 = N'ForRent'
)
Why am I getting this divide by zero error? I'm not adding any division. I'm just adding some new filters. Please help me with this error. Thanks.

You actually do here dbo.Table.FixedArea/ dbo.Table.ToiletNumber< 250...and probably ToiletNumber is zero
EDIT: Try re-running your query by changing the above code to
dbo.Table.FixedArea / NULLIF(dbo.Table.ToiletNumber, 0) < 250

You div/0 is here:
(
dbo.Table.FixedArea/ dbo.Table.ToiletNumber < 250
)
This:
AND (
dbo.Table.FixedArea> 25
)
Probably removed any div/0
Now this:
AND (
dbo.Table.FixedArea > 0
)
Means that rows with a FixedArea between 0 and 25 probably has a 0 for ToiletNumber, and has to be evaluated
To fix:
case when ToiletNumber = 0 then 1
else dbo.Table.FixedArea/ dbo.Table.ToiletNumber
end <250

Related

Postgres Error: More than one row returned by a subquery used as an expression. 21000

I'm trying to insert distinct rows into a table however, I'm receiving the error in the title. What I don't understand is that I have set the LIMIT to 1 as I assumed this would solve the issue.
Can anyone please shed some light on this?
ERROR: more than one row returned by a subquery used as an expression.
INSERT INTO reports.sem_attribution_v2
SELECT DISTINCT
tsa.user_id,
tsa.transfer_date,
tsa.engine,
tsa.campaign_id,
tsa.adgroup_id,
(CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN (
SELECT
account
FROM reports.lookup_uac_elements
WHERE campaign_id = tsa.campaign_id
LIMIT 1)
WHEN tsa.engine = 'google' THEN (
SELECT
pse.account_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.account_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END) as account_name,
(
CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN (
SELECT
lue.campaign
FROM reports.lookup_uac_elements lue
WHERE lue.campaign_id = tsa.campaign_id
)
WHEN tsa.engine = 'google' THEN (
SELECT
pse.campaign_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1
)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.campaign_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END
) as campaign_name,
(CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN NULL
WHEN tsa.engine = 'google' THEN (
SELECT
pse.adgroup_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1
)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.adgroup_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END) as adgroup_name,
tsa.creation_platform,
tsa.profile_country,
tsa.ltr_12m,
tsa.costs_12m,
tsa.ltv_12m,
tsa.first_paid_touch,
tsa.even_weights,
tsa.first_team_touch,
(CASE
WHEN tsa.profile_type = 'business' THEN 1
ELSE 0
END) as business,
tsa.content,
tsa.ltr_12m_r,
tsa.costs_12m_r,
tsa.ltv_12m_r,
(CASE
WHEN tsa.profile_type = 'consumer' THEN 1
ELSE 0
END) as consumer
FROM tmp_sem_attribution tsa;

ERROR ORA-00905: missing keyword (microsoft ole db provider for oracle)

can anyone see what keyword is missing from the pl/sql query below?
I am getting the error ORA-00905: missing keyword (microsoft ole db provider for oracle)
not sure what keyword is missing - this is an odbc query to oracle database
SELECT "STAT"."ORD"."SAMPLEID",
MAX(CASE WHEN "STAT"."ORDANAEL"."MC"='QAERS' THEN N"STAT"."ORDANAEL"."RES_TXT" ELSE
'' END) AS ERSID,
"STAT"."ORDMCSTP_V"."SEQNB", "STAT"."ORD"."ORDPATNAME",
"STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB", "STAT"."ORD"."PROJNB",
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN 'AMMOL'
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN 'AMMOLR' ELSE '' END) AS TEST,
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N"STAT"."ORDRESMCEL"."MC_RES"
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN N"STAT"."ORDRESMCEL"."MC_RES" ELSE ''
END) AS MC_RES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMI' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMI,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMP' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMP,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMNDM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS NDM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMOX48' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS OXA48,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSPM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SPM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMVIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS VIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMDIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS DIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMKPC' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS KPC,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGES' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSME' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SME,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMFRI1' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS FRI1,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GIM,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N "STAT"."ORDANAEL"."VALMED" ELSE
'' END) AS VALMED_AMMOL,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' AND
"STAT"."ORDANAEL_D"."DFIELD"='RR' THEN N "STAT"."ORDANAEL_D"."DVALUE" ELSE '' END)
AS RPT_DT_AMMOL,
"STAT"."ORDANAEL"."RES_ST"
FROM (("STAT"."ORDMCSTPEL" INNER JOIN ("STAT"."ORDMCSTP_V" INNER JOIN
("STAT"."ORD" INNER JOIN "STAT"."ORDANAEL" ON ("STAT"."ORD"."LID" =
"STAT"."ORDANAEL"."LID") AND ("STAT"."ORD"."LPERIOD" =
"STAT"."ORDANAEL"."LPERIOD") AND ("STAT"."ORD"."ORDNB" =
"STAT"."ORDANAEL"."ORDNB")) ON ("STAT"."ORDMCSTP_V"."LID" = "STAT"."ORD"."LID")
AND ("STAT"."ORDMCSTP_V"."LPERIOD" = "STAT"."ORD"."LPERIOD") AND
("STAT"."ORDMCSTP_V"."ORDNB" = "STAT"."ORD"."ORDNB")) ON ("STAT"."ORDMCSTPEL"."BC"
= "STAT"."ORDMCSTP_V"."BC") AND ("STAT"."ORDMCSTPEL"."CURRENT_STEPNB" =
"STAT"."ORDMCSTP_V"."CURRENT_STEPNB")) INNER JOIN "STAT"."ORDANAEL_D" ON
("STAT"."ORDANAEL"."NBINT" = "STAT"."ORDANAEL_D"."DNBINT") AND
("STAT"."ORDANAEL"."SPSEQ" = "STAT"."ORDANAEL_D"."DSPSEQ") AND
("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDANAEL_D"."DORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDANAEL_D"."DLPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDANAEL_D"."DLID")) INNER JOIN
"STAT"."ORDRESMCEL" ON ("STAT"."ORDMCSTP_V"."SEQNB" = "STAT"."ORDRESMCEL"."SEQNB")
AND ("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDRESMCEL"."ORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDRESMCEL"."LPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDRESMCEL"."LID")
WHERE (("STAT"."ORD"."LID")='H1')
AND (("STAT"."ORD"."LPERIOD")>'5200')
AND (("STAT"."ORD"."PROJNB")='ACPOERS')
GROUP BY "STAT"."ORD"."SAMPLEID", "STAT"."ORDMCSTP_V"."SEQNB",
"STAT"."ORD"."ORDPATNAME", "STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB",
"STAT"."ORD"."PROJNB", "STAT"."ORDANAEL"."RES_ST"
HAVING ((("STAT"."ORDMCSTP_V"."SEQNB") Is Not Null) AND (("STAT"."ORD"."PROJNB")
='ACPOERS') AND (("STAT"."ORDANAEL"."RES_ST")>'4'))
Just too big for comment. Just cleaned up the double quotes and the N and it comes clean in a SQL formatter.
SELECT STAT.ORD.SAMPLEID,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'QAERS' THEN STAT.ORDANAEL.RES_TXT ELSE '' END ) AS ersid,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN 'AMMOL'
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN 'AMMOLR'
ELSE ''
END )
AS test,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDRESMCEL.MC_RES
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN STAT.ORDRESMCEL.MC_RES
ELSE ''
END )
AS mc_res,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMI' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imi,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMP' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imp,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMNDM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ndm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMOX48' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS oxa48,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSPM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS spm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMVIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS vim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMDIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS dim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMKPC' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS kpc,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGES' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ges,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSME' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sme,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMFRI1' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS fri1,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS gim,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDANAEL.VALMED ELSE '' END )
AS valmed_ammol,
MAX ( CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL'
AND STAT.ORDANAEL_D.DFIELD = 'RR' THEN
STAT.ORDANAEL_D.DVALUE
ELSE
''
END )
AS rpt_dt_ammol,
STAT.ORDANAEL.RES_ST
FROM ((STAT.ORDMCSTPEL
INNER JOIN (STAT.ORDMCSTP_V
INNER JOIN (STAT.ORD
INNER JOIN STAT.ORDANAEL
ON ( STAT.ORD.LID = STAT.ORDANAEL.LID )
AND ( STAT.ORD.LPERIOD = STAT.ORDANAEL.LPERIOD )
AND ( STAT.ORD.ORDNB = STAT.ORDANAEL.ORDNB ))
ON ( STAT.ORDMCSTP_V.LID = STAT.ORD.LID )
AND ( STAT.ORDMCSTP_V.LPERIOD = STAT.ORD.LPERIOD )
AND ( STAT.ORDMCSTP_V.ORDNB = STAT.ORD.ORDNB ))
ON ( STAT.ORDMCSTPEL.BC = STAT.ORDMCSTP_V.BC )
AND ( STAT.ORDMCSTPEL.CURRENT_STEPNB = STAT.ORDMCSTP_V.CURRENT_STEPNB ))
INNER JOIN STAT.ORDANAEL_D
ON ( STAT.ORDANAEL.NBINT = STAT.ORDANAEL_D.DNBINT )
AND ( STAT.ORDANAEL.SPSEQ = STAT.ORDANAEL_D.DSPSEQ )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDANAEL_D.DORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDANAEL_D.DLPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDANAEL_D.DLID ))
INNER JOIN STAT.ORDRESMCEL
ON ( STAT.ORDMCSTP_V.SEQNB = STAT.ORDRESMCEL.SEQNB )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDRESMCEL.ORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDRESMCEL.LPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDRESMCEL.LID )
WHERE ( ( STAT.ORD.LID ) = 'H1' )
AND ( ( STAT.ORD.LPERIOD ) > '5200' )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
GROUP BY STAT.ORD.SAMPLEID,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
STAT.ORDANAEL.RES_ST
HAVING ( ( ( STAT.ORDMCSTP_V.SEQNB ) IS NOT NULL )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
AND ( ( STAT.ORDANAEL.RES_ST ) > '4' ) );
You can use N in front of a string literal to mark it as an nvarchar2. Example:
select N'abc' from dual
But you can't use it in front of an identifier the way you are doing it. If you want to read a certain column value as an nvarchar2, you'll need to cast it as such. Example:
... THEN cast("STAT"."ORDANAEL"."RES_TXT" as nvarchar2(50)) ...

Cannot perform an aggregate function on an expression containing an aggregate or a subquery when taking SUM of a Column returned by CASE Statement

I am having a problem with the following SQL query. While executing, it gives error:
Msg 130, Level 15, State 1, Line 2
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
SELECT
sum(case when DESP_SCANNING.PALLET_NO != null then (select PALLETNO_MASTER.PALLET_WEIGHT from PALLETNO_MASTER where DESP_SCANNING.PALLET_NO= PALLETNO_MASTER.PALLET_NO and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID) else (select CARTONNO_MASTER.CARTON_WEIGHT from CARTONNO_MASTER where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID) end) as totweight
FROM CUSTOMER_MASTER, DESP_SCANNING, CARRIER_MASTER, DC_MASTER
WHERE CARRIER_MASTER.CARRIER_CODE = DESP_SCANNING.CARRIER_CODE AND
((DESP_SCANNING.PALLET_NO != null and DESP_SCANNING.PALLET_NO = (select PALLETNO_MASTER.PALLET_NO from PALLETNO_MASTER where DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID)) or(DESP_SCANNING.PALLET_NO = null and DESP_SCANNING.CARTON_NO = (select CARTONNO_MASTER.CARTON_NO from CARTONNO_MASTER where DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID))) AND
DESP_SCANNING.STATUS = 2
group by DESP_SCANNING.DC_NO, DESP_SCANNING.POD_DATE, CUSTOMER_MASTER.CUS_NAME, CARRIER_MASTER.CARRIER_NAME, DESP_SCANNING.AWB_NO,DC_MASTER.DESP_END_DATE_TIME, DESP_SCANNING.SCAN_DATE, DC_MASTER.PSR_LOAD_DATE_TIME
order by CARRIER_MASTER.CARRIER_NAME, DESP_SCANNING.POD_DATE
Error shows in this line:
sum(case when DESP_SCANNING.PALLET_NO != null then (select PALLETNO_MASTER. PALLET_WEIGHT from PALLETNO_MASTER where DESP_SCANNING.PALLET_NO= PALLETNO_MASTER.PALLET_NO and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID) else (select CARTONNO_MASTER.CARTON_WEIGHT from CARTONNO_MASTER where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID) end) as totweight,
So, what I can give instead of this.
It looks like you need to factor out the CASE ... construct (which has the subquery's that cause the error message) into the WHERE clause.
That is, first construct a single table that contains PALLET_WEIGHT or CARTON_WEIGHT as appropriate, in a single column, and do the SUM over that column. Genre SELECT SUM(x) FROM (your big complicated SELECT here).
(I'm not guaranteeing that this will be possible. It's just what I would try to do having read what the error message tells me. Bigger product experts than me could possibly tell you my idea won't work either.)
maybe this code will help you:
select sum(x.totweight) as totweight
from (
select
CARRIER_MASTER.CARRIER_NAME,
DESP_SCANNING.POD_DATE,
case when DESP_SCANNING.PALLET_NO != null then(
select PALLETNO_MASTER.PALLET_WEIGHT
from PALLETNO_MASTER
where DESP_SCANNING.PALLET_NO = PALLETNO_MASTER.PALLET_NO
and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID
)
else(
select CARTONNO_MASTER.CARTON_WEIGHT
from CARTONNO_MASTER
where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO
and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID
)
end as totweight
from CUSTOMER_MASTER,
DESP_SCANNING,
CARRIER_MASTER,
DC_MASTER
where CARRIER_MASTER.CARRIER_CODE = DESP_SCANNING.CARRIER_CODE
and (
(
DESP_SCANNING.PALLET_NO != null
and DESP_SCANNING.PALLET_NO = (
select PALLETNO_MASTER.PALLET_NO
from PALLETNO_MASTER
where DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID
)
)
or (
DESP_SCANNING.PALLET_NO = null
and DESP_SCANNING.CARTON_NO = (
select CARTONNO_MASTER.CARTON_NO
from CARTONNO_MASTER
where DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID
)
)
)
and DESP_SCANNING.status = 2
group by DESP_SCANNING.DC_NO,
DESP_SCANNING.POD_DATE,
CUSTOMER_MASTER.CUS_NAME,
CARRIER_MASTER.CARRIER_NAME,
DESP_SCANNING.AWB_NO,
DC_MASTER.DESP_END_DATE_TIME,
DESP_SCANNING.SCAN_DATE,
DC_MASTER.PSR_LOAD_DATE_TIME
) x
order by x.CARRIER_NAME, x.POD_DATE

Nested case statements in inner joins

Having issues with that I think can be solved by a case statement, but it keeps rejecting my sql. Basically, I'm introducing a new parameter to existing SQL and need to react different ways depending on what it is... sounded like a case statement to me, but it keeps rejecting it. Any thoughts?
Original
INNER JOIN T_CASE CF ON
(
A.AWESOME_ID = CF.AWESOME_ID
// Several more awesome conditions
)
AND
(
(
cy.SOMEFLAG = 0
AND CF.SOMEDATE IS NOT NULL
)
OR
(
cy.SOMEFLAG = 1
AND CF.CASE_TYPE = 1
AND CF.SOMEDATE IS NOT NULL
)
)
Attempt #1
INNER JOIN T_CASE CF ON
(
A.AWESOME_ID = CF.AWESOME_ID
// Several more conditions
)
AND
(
// Just this part is what the new parameter applies to
CASE WHEN #NEWPARAM = 1 THEN
(
(
cy.SOMEFLAG = 0
AND CF.SOMEDATE IS NOT NULL
)
OR
(
cy.SOMEFLAG = 1
AND CF.CASE_TYPE = 1
AND CF.SOMEDATE IS NOT NULL
)
)
ELSE
(
(
cy.SOMEFLAG = 0
AND CF.SOMEDATE IS NULL
)
OR
(
cy.SOMEFLAG = 1
AND CF.CASE_TYPE = 0
OR CF.SOMEDATE IS NULL
)
)
END
)
In general, I think it is a better idea to use basic logical constructs in condition clauses, rather than the case. You can readily do this:
INNER JOIN T_CASE CF ON
(
A.AWESOME_ID = CF.AWESOME_ID
// Several more conditions
)
AND
(
(
#NEWPARAM = 1 AND
cy.SOMEFLAG = 0 AND
CF.SOMEDATE IS NOT NULL
)
OR
(
#NEWPARAM = 1 AND
cy.SOMEFLAG = 1 AND
CF.CASE_TYPE = 1 AND
CF.SOMEDATE IS NOT NULL
)
OR
(
#NEWPARAM <> 1 AND
cy.SOMEFLAG = 0 AND
CF.SOMEDATE IS NULL
)
OR
(
#NEWPARAM <> 1 AND
((cy.SOMEFLAG = 1 AND CF.CASE_TYPE = 0) OR
CF.SOMEDATE IS NULL
)
)
)
You can't use the case statement to dynamically include/exclude portions of the SQL statement, but you can still get a similar effect like this
INNER JOIN T_CASE CF ON
(
A.AWESOME_ID = CF.AWESOME_ID
// Several more conditions
)
AND
(
// Just this part is what the new parameter applies to
1 = CASE WHEN
#NEWPARAM = 1
and
(
cy.SOMEFLAG = 0
AND CF.SOMEDATE IS NOT NULL
)
OR
(
cy.SOMEFLAG = 1
AND CF.CASE_TYPE = 1
AND CF.SOMEDATE IS NOT NULL
)
THEN 1
CASE WHEN
#NEWPARAM <> 1
and
(
(
cy.SOMEFLAG = 0
AND CF.SOMEDATE IS NULL
)
OR
(
cy.SOMEFLAG = 1
AND CF.CASE_TYPE = 0
OR CF.SOMEDATE IS NULL
)
)
THEN 1
ELSE 0
END
)
Basically we're returning 1 for rows that meet the criteria, and 0 for rows that don't and comparing that against the value 1.

Make sql query run faster

Is it possible to make some optimization on such query ?
with t (CurrentPrice, [Weight])
as ( select CurrentPrice, [Weight] from SomeTable where GroupingId = SomeBigIntId )
SELECT MaxExactPrice = ( select MAX(CurrentPrice) FROM t Where [Weight] > 0 ),
MinExactPrice = ( select MIN(CurrentPrice) FROM t Where [Weight] > 0 ),
MaxSimilarPrice = ( select MAX(CurrentPrice) FROM t Where [Weight] = 0 ),
MinSimilarPrice = ( select MIN(CurrentPrice) FROM t Where [Weight] = 0 ),
ExactCount = ( select Count(*) FROM t Where [Weight] > 0 ),
SimilarCount = ( select Count(*) FROM t Where [Weight] = 0 ),
Count(*) as TotalCount
FROM t
Thank you.
Do this instead. this way you do not have to hit same table 8 different times.
select MAX(CASE WHEN [Weight] > 0 THEN CurrentPrice ELSE NULL END) AS MaxExactPrice
,MIN(CASE WHEN [Weight] > 0 THEN CurrentPrice ELSE NULL END) AS MinExactPrice
,MAX(CASE WHEN [Weight] = 0 THEN CurrentPrice ELSE NULL END) AS MaxSimilarPrice
,MIN(CASE WHEN [Weight] = 0 THEN CurrentPrice ELSE NULL END) AS MinSimilarPrice
,COUNT(CASE WHEN [Weight] > 0 THEN 1 ELSE NULL END ) AS ExactCount
,COUNT(CASE WHEN [Weight] = 0 THEN 1 ELSE NULL END ) AS SimilarCount
,COUNT(*)AS TotalCount
from SomeTable where GroupingId = SomeBigIntId