ORA-01476: Divisor is equal to zero - sql

I have this query that I've been given to fix and I cannot understand why its causing error. I reviewed similar topics involving the max function and the null function but I can't seem to make them work without throwing another error.
select case when count(trlr_num) > 0 then 'Inbounds Need Closed'
else 'All Good'
end as "Inbounds To Be Closed"
from (select t.trlr_num,
r.invnum "Invoice",
to_char(nvl(max(rl.init_rcv_dte), sysdate), 'MM/DD/YY HH:MI AM') as "Begin receive date",
round(sum(rl.idnqty) / sum(rl.expqty) *100, 1) as "% Received%"
from trlr t
join rcvinv r
on t.trlr_num = r.trknum
join rcvlin rl
on r.trknum = rl.trknum
and r.supnum = rl.supnum
and r.invnum = rl.invnum
and r.wh_id = rl.wh_id
join rcvtrk rt
on r.trknum = rt.trknum
and t.trlr_id = rt.trlr_id
and r.wh_id = rt.wh_id
where t.yard_loc_wh_id = 'US_3218'
and rt.rcvtrk_stat <> 'C'
and r.invtyp not in ('PKG', 'XFR')
and (rl.rcvsts <> 'QI' or (rl.rcvsts = 'QI' and t.trlr_seal1 is not null and t.trlr_seal2 is not null))
group by t.trlr_num,
r.invnum
having sum(rl.idnqty) / sum(rl.expqty) = 1
and nvl(max(rl.init_rcv_dte), sysdate) < sysdate -2)

Related

Conversion failed when converting the varchar value '5005A' to data type int

So I get the data from my server and the mvmt number consists of 3 characters and 4 digits (EX: CVG5694) or unless they start when it starts with X because it's 1 character (the letter x), and 5 digits (ex: X12051). This code worked for me until I got the error,
Conversion failed when converting the varchar value '5005A' to data type int.
I assume because it has the character A in the end. Anyone know how to fix?
SQL_Query = pd.read_sql_query('''SELECT[MVMT_DT],
[MVMT_NUMBER],
CAST(RIGHT([MVMT_NUMBER], PATINDEX('%[0-9][^0-9]%', REVERSE([MVMT_NUMBER])+' ')) as INT) as movement,
[MVMT_TYPE],
[OPERATOR],
[EQUIPMENT],
[ORIG],
[DEST],
[MVMT_STATUS],
CASE WHEN [GROSS_WEIGHT_(KG)] < 0 THEN 0 ELSE [GROSS_WEIGHT_(KG)] END AS [GROSS_WEIGHT_(KG)],
CASE WHEN [NET_WEIGHT_(KG)]< 0 THEN 0 ELSE [NET_WEIGHT_(KG)] END AS [NET_WEIGHT_(KG)],
CASE WHEN [NMBR_ULDS] < 0 THEN 0 ELSE [NMBR_ULDS] END AS [NMBR_ULDS],
CASE WHEN [NMBR_POS] < 0 THEN 0 ELSE [NMBR_POS] END AS [NMBR_POS]
FROM PATH
WHERE [F-T-O] = 'T'
AND ORIG IN ('CVG', 'CVG CRN', 'MIA', 'MIA GTW', 'LAX', 'LAX GTW', 'JFK', 'JFK GTW', 'ORD', 'ORD GTW')
AND MVMT_TYPE IN ('O/XL', 'O/XL/AH', 'T/XL', 'T/XL/AH', 'CL/AH', 'O/AH', 'T/AH')
AND [MVMT_NUMBER] NOT LIKE '%AMZ%'
AND [MVMT_NUMBER] NOT LIKE '%A0%'
AND [MVMT_NUMBER] NOT LIKE '%K0%'
AND [MVMT_NUMBER] NOT LIKE '%A1%'
AND [MVMT_NUMBER] NOT LIKE '%K1%'
--AND RIGHT([MVMT_NUMBER], 4) <= 5000
AND MVMT_DT = '2021-12-06' --DATEADD(DAY, -2, GETDATE()) AND DATEADD(DAY, -1, GETDATE())''',conn_)
CXL_Filter = ['O/XL', 'O/XL/AH', 'T/XL', 'T/XL/AH']
Ad_Hoc_Filter = ['CL/AH', 'O/AH', 'T/AH']
CXL_CVG = SQL_Query[SQL_Query.MVMT_TYPE.isin(CXL_Filter) & (SQL_Query['ORIG'] == 'CVG') & (SQL_Query['movement'] >= 5000)]
CXL_CVG_CRN = SQL_Query[SQL_Query.MVMT_TYPE.isin(CXL_Filter) & (SQL_Query['ORIG'] == 'CVG CRN') & (SQL_Query['movement'] >= 5000)]
Ad_Hoc_CVG = SQL_Query[SQL_Query.MVMT_TYPE.isin(Ad_Hoc_Filter) & (SQL_Query['ORIG'] == 'CVG') & (SQL_Query['movement'] >= 5000)]
Ad_Hoc_CVG_CRN = SQL_Query[SQL_Query.MVMT_TYPE.isin(Ad_Hoc_Filter) & (SQL_Query['ORIG'] == 'CVG CRN') & (SQL_Query['movement'] >= 5000)]```

Convert INNER JOIN to LEFT JOIN - Sql

i have a query which returns ~ 97k records because i am using an inner join. Now i want to use left join to show also the null records on the right table(should be about 300k records). This is the actual query(INNER JOIN):
select artnr as "Article number", ardbez1 as "Article description ", arpadrnr as "Supplier code", cd.ADPPHYSLAND as "Country of this supplier code", p.adrnr as "Parent supplier code", p.adrkname as "Name of parent", pd.ADPPHYSLAND as "Country of parent ", arppreis as "Price", arpwae as "Unit", arzwert as "COO", atatarnr as "HS Code"
from mic_ccs_artikel, mic_ccs_artikeldetail, mic_ccs_artikelpreis, mic_dna_adressen c, mic_dna_adrdetail cd, mic_dna_adressen p, mic_dna_adrrelation, mic_dna_adrdetail pd, mic_ccs_artikelzoll, mic_ccs_artikeltarif
where artsid = ardartsid
and arddatstart <= sysdate
and nvl(arddatend, '01.01.4000') >= sysdate
and artsid = arpartsid
and ARPPREISART = 'VP'
and arpdatstart <= sysdate
and nvl(arpdatend, '01.01.4000') >= sysdate
and c.adrsid = cd.adpadrsid
and arpadrnr = c.adrnr
and cd.adpdatvon <= sysdate
and nvl(cd.adpdatbis, '01.01.4000') >= sysdate
and c.adrmandant = 'S1'
and c.adrwerk = 'V1'
and p.adrmandant = 'S1'
and p.adrwerk = 'V1'
and p.adrsid = arlrelsid1
and c.adrsid = arlrelsid2
and p.adrsid = pd.adpadrsid
and pd.adpdatvon <= sysdate
and nvl(pd.adpdatbis, '01.01.4000') >= sysdate
and artsid = arzartsid
and arztyp = 'URLD'
and arzadrnr = c.adrnr
and arzdatstart <= sysdate
and nvl(arzdatend, '01.01.4000') >= sysdate
and artsid = ataartsid
and ATAREGION = 'SE'
and atatarart='EXPORT'
and atadatstart <= sysdate
and nvl(atadatend, '01.01.4000') >= sysdate
;
Then i tried to "convert" it to LEFT JOIN, but it's always showing the same result:
select artnr as "Article number", ardbez1 as "Article description ", arpadrnr as "Supplier code", cd.ADPPHYSLAND as "Country of this supplier code", p.adrnr as "Parent supplier code", p.adrkname as "Name of parent", pd.ADPPHYSLAND as "Country of parent ", arppreis as "Price", arpwae as "Unit", arzwert as "COO", atatarnr as "HS Code"
from mic_ccs_artikel, mic_ccs_artikeldetail, mic_ccs_artikelpreis, mic_dna_adressen c, mic_dna_adrdetail cd, mic_dna_adressen p, mic_dna_adrrelation, mic_dna_adrdetail pd, mic_ccs_artikelzoll, mic_ccs_artikeltarif
where artsid = ardartsid(+)
and arddatstart <= sysdate
and nvl(arddatend, '01.01.4000') >= sysdate
and artsid = arpartsid(+)
and ARPPREISART = 'VP'
and arpdatstart <= sysdate
and nvl(arpdatend, '01.01.4000') >= sysdate
and c.adrsid = cd.adpadrsid(+)
and arpadrnr = c.adrnr
and cd.adpdatvon <= sysdate
and nvl(cd.adpdatbis, '01.01.4000') >= sysdate
and c.adrmandant = 'S1'
and c.adrwerk = 'V1'
and p.adrmandant = 'S1'
and p.adrwerk = 'V1'
and p.adrsid = arlrelsid1(+)
and c.adrsid = arlrelsid2
and p.adrsid = pd.adpadrsid
and pd.adpdatvon <= sysdate
and nvl(pd.adpdatbis, '01.01.4000') >= sysdate
and artsid = arzartsid(+)
and arztyp = 'URLD'
and arzadrnr = c.adrnr
and arzdatstart <= sysdate
and nvl(arzdatend, '01.01.4000') >= sysdate
and artsid = ataartsid(+)
and ATAREGION = 'SE'
and atatarart='EXPORT'
and atadatstart <= sysdate
and nvl(atadatend, '01.01.4000') >= sysdate
;
Why is it always showing the same result?
Echoing the sentiments in the comments, using a left join and a where clause can result in some unexpected behavior. See my answer here for more details as to why that happens. Basically, you should switch to using ANSI syntax and alias your tables. In your case, that would look something like
select * -- select whichever fields you want here
from mic_ccs_artikel a -- only one table in the from clause
left join mic_ccs_artikeldetail b on b.someID = a.someID -- join conditions here
left join mic_ccs_artikelpreis c
on c.someID = a.someID -- join conidition
and c.someField = 'Some Property' -- filtering here
...
The secondary join conditions removes the risk of throwing out those records in the where clause. Again, see my other answer for more details on that.

Rewrite subquery with calculation in SQL to CASE statement

I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1

Calculating (dividing) two summed fields in SQL Query

I have this query:
Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage
from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where
FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct')
AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey
Which Returns this result:
DimSubscriptionKey TotalUnits AI_NormalizedUsage
4707785 24.77043700 21.08775630
What I need to get and can't get to work is a 4th Column Which would be (AI_NormaliedUsage / TotalUnits)
But when I add it to the query:
Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, (AI_NormalizedUsage / TotalUnits)
from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where
FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct')
AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey, (AI_NormalizedUsage / TotalUnits)
I am not getting expected results:
DimSubscriptionKey TotalUnits AI_NormalizedUsage (No column name)
4707785 1.45831000 0.70567620 0.48389999382847268413
4707785 3.39577900 3.28609533 0.96769999755578911348
4707785 9.49984800 9.19300290 0.96769999898945751553
4707785 3.41661200 3.30625543 0.96769999929754973640
4707785 4.49992800 2.17751515 0.48389999795552284392
4707785 2.49996000 2.41921129 0.96769999919998719979
What I want is
DimSubscriptionKey TotalUnits AI_NormalizedUsge NewCalculatedField<br>
4707785 24.770437 21.0877563 0.851327585
What am I missing? Racking my brain!
if you're referencing derived columns from within the same select statement you can't refer to them by their alias, you need to either duplicate the calculation, or wrap it in another select statement.
Without knowing anything about the underlying tables, this fix should work:
Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, sum(FMUM.AI_NormalizedUsage) / sum(FMUM.TotalUnits) NewCalculatedField
from [AI_DataMart].[AzureViews].[v_FactMeteredUsageMonthly] as FMUM
join [AI_DataMart].[AzureViews].[v_DimServiceExtended] as SE on(FMUM.DimServiceKey = SE.DimServiceKey)
join [AI_DataMart].[AzureViews].[v_DimAccount] as A on(FMUM.DimAccountKey = A.DimAccountKey)
Join [AI_DataMart].[AzureViews].[v_DimSubscription] as SU on(FMUM.DimSubscriptionKey = SU.DimSubscriptionKey)
where
FMUM.DimDateKey >= '20150201'And FMUM.DimDateKey <= '20150331'
And SE.Workload = 'SQLDB'
And SE.ResourceName != 'SQL Reporting Hours'
And (SU.AI_BillingType = 'EA' or SU.AI_BillingType = 'Direct')
AND SU.IsFraudIdentified = 0
AND SU.AI_IsTest = 0
AND SU.DimBillingSystemKey = 1
AND FMUM.DimSubscriptionKey = '4707785'
group by FMUM.DimSubscriptionKey
Sorry I can't comment but have you tried this:
Select FMUM.DimSubscriptionKey, sum(FMUM.TotalUnits) as TotalUnits, sum(FMUM.AI_NormalizedUsage) as AI_NormalizedUsage, (sum(FMUM.AI_NormalizedUsage / sum(FMUM.TotalUnits))
Should not need the group by either.

SQL - Error with Sum(Case

I have searched a bunch of examples which were quite helpful but in creating my query but I am having trouble with one part of the query...
Sorry for not being able to provide the complete table structure. Here is a working query...
select i.spl_instr_code_1 LW,
SUM((d.units_ordered/i.std_sub_pack_qty) - (d.units_rcvd/i.std_sub_pack_qty)) as "Booked"
From wmdbadmin.item_master i, wmdbadmin.cust_po_appt c, wmdbadmin.po_hdr h,
wmdbadmin.po_dtl d, wmdbadmin.appt_sched a, wmdbadmin.vendor_master v,
wmdbadmin.sys_code s
where h.ref_field_2 = 'N' and
c.appt_nbr = a.appt_nbr (+) and c.po_nbr = h.po_nbr and
h.po_nbr = d.po_nbr and i.sku_id = d.sku_id and
h.vendor_master_id = v.vendor_master_id and s.code_id = a.stat_code and
s.rec_type = 'S' and s.code_type = '628' and s.code_desc<>'Scheduled' and
a.stat_code <= '90' and ((to_char(a.sched_date_time,'YYYYMMDD')>= '20150401'
and to_char(a.sched_date_time, 'YYYYMMDD')<='20150401'))
group by i.spl_instr_code_1
order by i.spl_instr_code_1 asc
I tried to alter the above by changing...
SUM((d.units_ordered/i.std_sub_pack_qty) - (d.units_rcvd/i.std_sub_pack_qty)) as "Booked"
to...
SUM( case when s.code_desc<>'Checked In' then (d.units_ordered/i.std_sub_pack_qty) else (d.units_rcvd/i.std_sub_pack_qty) end ) as “Booked”
but I get the following error and I am uncertain what the error means and can not figure out how to correct it...
#Source: OraOLEDB Number: -2147467259 Description: ORA-00972: identifier is too long SQL State: Native Error: 972
#Failed to Open the Recordset [hr=2147500037 (0x80004005)]
# Source: ADODB.Recordset Number: 3704 Description: Operation is not allowed when the object is closed. SQL: select i.spl_instr_code_1 LW, SUM( case when s.code_desc<>'Checked In' then (d.units_ordered/i.std_sub_pack_qty) else (d.units_rcvd/i.std_sub_pack_qty) end ) as “Booked” From wmdbadmin.item_master i, wmdbadmin.cust_po_appt c, wmdbadmin.po_hdr h, wmdbadmin.po_dtl d, wmdbadmin.appt_sched a, wmdbadmin.vendor_master v, wmdbadmin.sys_code s where h.ref_field_2 = 'N' and c.appt_nbr = a.appt_nbr (+) and c.po_nbr = h.po_nbr and h.po_nbr = d.po_nbr and i.sku_id = d.sku_id and h.vendor_master_id = v.vendor_master_id and s.code_id = a.stat_code and s.rec_type = 'S' and s.code_type = '628' and s.code_desc<>'Scheduled' and a.stat_code <= '90' and ((to_char(a.sched_date_time,'YYYYMMDD')>= '20150331' and to_char(a.sched_date_time, 'YYYYMMDD')<='20150331')) group by i.spl_instr_code_1 order by i.spl_instr_code_1 asc
# Source: SQLBuilder.clsSQLBuilder Number: 25004 Description: Closed recordset object returned from database.
#Failed on call to GetRecords on Business Object. at line: 385
#Failed on call RefreshDataGrid.