SQL - Error with Sum(Case - sql

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.

Related

ORA-01476: Divisor is equal to zero

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)

How to convert If statement from crystal report code to SQL case statements

I have an If statement written in Crystal report that I wanted to convert to SQL code. The first code block is the Crystal report code; the last code block is the SQL code. Thank You!!
If {Command.TRT} = '23' then
( if {Command.CAT} = '200' or {Command.CAT} = '300' then 'Home'
else {Command.CAT} ='111' or {Command.CAT} = '22A' then 'School'
else "Other: " & {Command.CAT} )
else
if {Command.TRT} = '20' then (
if {Command.CAT} = '220' or {Command.CAT} = '400 then 'Homework'
else "Other: " & {Command.CAT} )
)
Case When TRT = '23' and CAT = '200' or CAT = '300' then 'Home'
when TRT = '23' and CAT = '111' or CAT = '22A' then 'School'
else
Case When TRT = '20' and AT = '220' or CAT = '400 ' then 'Homework'
else Concat('Other: ',CAT)
See nested CASE example here: Best way to do nested case statement logic in SQL Server
But long-term, creating and joining to lookup tables may provide a simpler, less error-prone, and more maintainable solution.

How to add a column in a sql query with selection of the result

I have a sql statment like this:
SELECT a.link_id, a.Speed_Limit_1, a.Speed_Limit_2, a.speed_limit_source, g.autos, g.trucks, g.motorcycles, 'vehicleType' as vehicleType
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id
result:
link_id......autos...trucks...motorcycles.........vehicleTypes
1..........|.......Y....|....Y......|.....N.................|.............?
2..........|.......Y....|....Y......|.....Y.................|.............?
3..........|.......Y....|....Y......|.....N.................|.............?
4..........|.......Y....|....N......|.....Y.................|.............?
I will add the column "vehicleTypes. Inside this column should be:
0 if autos, trucks and motorcycles are allowed (e.g. row 2)
1 if only cars are allowed
2 if only trucks are allowed
3 if only motorcycles are allowed
How can I do this?
This should do it.
I added an ELSE -1 to indicate where it didnt match any of the criteria.
SELECT
a.link_id,
a.Speed_Limit_1,
a.Speed_Limit_2,
a.speed_limit_source,
g.autos, g.trucks,
g.motorcycles,
CASE
WHEN g.autos = 'Y' AND g.trucks = 'Y' AND g.motorcycles = 'Y' THEN 0
WHEN g.autos = 'Y' AND g.trucks = 'N' AND g.motorcycles = 'N' THEN 1
WHEN g.autos = 'N' AND g.trucks = 'Y' AND g.motorcycles = 'N' THEN 2
WHEN g.autos = 'N' AND g.trucks = 'N' AND g.motorcycles = 'Y' THEN 3
ELSE -1
END as vehicleTypes
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id
You should use CASE WHEN block
for example in Oracle:
https://www.techonthenet.com/oracle/functions/case.php

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.

Crystal Reports v14 not finding all null values

Alright, I've run into this before and have gotten everything to work correctly in the past. I have an SQL code that was created that now needs to be turned into a crystal report. The SQL shows 956 lines, but Crystal is only showing 886.
Here is the SQL code:
SELECT
I4240,
I4201,
I4202,
I4203,
I4204,
I4206,
I4213,
I4214,
I4225,
I4208,
I4299
FROM
MT.INVENTORY
WHERE
(
I4202 IN ('UNKNONWN','VERIFY MFR','OTHER','VARIOUS','TBD','NA','N/A') OR
(
I4203 IN ('UNKNONWN','VERIFY MODEL NUMBER','OTHER','VARIOUS','TBD','NA','N/A','MISCELLANEOUS')
OR I4203 IS NULL
OR LENGTH(I4203)=0
) OR
(
I4204 IN ('UNKNOWN DESCRIPTION','VERIFY DESCRIPTION','OTHER','VARIOUS','TBD','NONE - NO STD USED','NA','N/A','MISCELLANEOUS')
OR I4204 IS NULL
OR LENGTH(I4204)=0
)
) AND
I4240 NOT IN ('MT','STD','NESD')
ORDER BY I4240,I4202,I4203,I4204
and the record selection formula from CR:
(
{Inventory.I4240} <> 'mt' and
{Inventory.I4240} <> 'std' and
{Inventory.I4240} <> 'nesd'
)
AND
(
(
{Inventory.I4202} = 'UNKNONWN' OR
{Inventory.I4202} = 'VERIFY MFR' OR
{Inventory.I4202} = 'OTHER' OR
{Inventory.I4202} = 'VARIOUS' OR
{Inventory.I4202} = 'TBD' OR
{Inventory.I4202} = 'NA' OR
{Inventory.I4202} = 'N/A'
)
OR
(
{Inventory.I4203} = 'UNKNONWN' OR
{Inventory.I4203} = 'VERIFY MODEL NUMBER' OR
{Inventory.I4203} = 'OTHER' OR
{Inventory.I4203} = 'VARIOUS' OR
{Inventory.I4203} = 'TBD' OR
{Inventory.I4203} = 'NA' OR
{Inventory.I4203} = 'N/A' OR
{Inventory.I4203} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4203}) OR
LENGTH(trim({Inventory.I4203})) < 1 OR
INSTR(trim({Inventory.I4203}), "") = 0 OR
TRIM({Inventory.I4203}) = ""
)
OR
(
{Inventory.I4204} = 'UNKNOWN DESCRIPTION' OR
{Inventory.I4204} = 'VERIFY DESCRIPTION' OR
{Inventory.I4204} = 'OTHER' OR
{Inventory.I4204} = 'VARIOUS' OR
{Inventory.I4204} = 'TBD' OR
{Inventory.I4204} = 'NONE - NO STD USED' OR
{Inventory.I4204} = 'NA' OR
{Inventory.I4204} = 'N/A' OR
{Inventory.I4204} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4204}) OR
LENGTH(trim({Inventory.I4204})) < 1 OR
INSTR({Inventory.I4204}, "") = 0 OR
TRIM({Inventory.I4204}) = ""
)
)
Any help would be appreciated.
In Crystal, if a particular field can be null then you need to check for that condition as the very first thing you do with it, otherwise the entire formula will error out and nothing will be evaluated.
So in your case, fields I4203 and I4204 need the isnull() check moved to the top of their respective sections at the very least. If I4240 and I4202 can be null, then you should handle those conditions as well.
Also, you have a couple references to the word "UNKNONWN"; is that a typo?
The issue was that the report options were set to use null as default, but for some reason in the record selection formula it was set to exception. Once that was set to the default setting it worked without any issues and the counts were correct.