query is returning " single-row subquery returns more than one row" - sql

I am not sure why the below query is giving the " single-row subquery returns more than one row" please let me know if I am missing anything.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END

The error message advise very clearly, more than one row are returned from the sub-query after the key word "Then". You can add "Top 1" after the first SELECT keyword for SQL Serve4.
Or get the sub query end with "and rownum =1" for Oracle. The following is for SQL Server.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT TOP 1 E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END

Related

I have A problem with dividing the value of 0 by 0 in an Access database

When I try to extract the result of Division 2 Field from table in access database
If I have a value of 0 an error occurs
sqlSTR = "SELECT TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY AS ['Price']FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)
If you are trying to avoid the error, you can use a CASE statement in your query to check for a 0 value in the Item_Org_Price2 field and return a different value if it is 0. For example, you could do something like this:
SELECT
CASE
WHEN TBL_Category_Item_File.Item_Org_Price2 = 0
THEN 0
ELSE TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY
END AS ['Price']
FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)

sql oracle - filter query sql using case when

I have the below query
select
ens.ID_ENS,
ens.NOM_ENS,
-- for the first SURVEILLANT
disp.SALLE_EXAM,
disp.NB_HEURES_ENS,
-- for the second SURVEILLANT
disp.SALLE_EXAM2,
disp.NB_HEURES_ENS2
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';
I want update the query for display SALLE_EXAM, NB_HEURES_ENS only for SURVEILLANT
select
ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN (disp.SALLE_EXAM AS "EXAM", disp.NB_HEURES_ENS AS "HOURDISP")
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN (disp.SALLE_EXAM2 AS "EXAM", disp.NB_HEURES_ENS2 AS "HOURDISP")
END
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';
You can rewrite the query with explicit JOIN syntax as
SELECT ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.SALLE_EXAM
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.SALLE_EXAM2
END AS "EXAM",
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.NB_HEURES_ENS
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.NB_HEURES_ENS2
END AS "HOURDISP"
FROM ESP_ENSEIGNANT ens
JOIN ESP_MODULE_PANIER_CLASSE_SAISO disp
ON ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2)
AND ens.NOM_ENS != 'A AFFECTER';
A side note : if disp.SURVEILLANT and disp.SURVEILLANT2 have equal values, then disp.SALLE_EXAM and disp.NB_HEURES_ENS will be picked respectively. Eg the first components have precedence.

Convert SQL Query to LINQ query/Lambda expression

I am trying to fetch the results from the DB using the Entity Framework using the below SQL query:
SELECT
Header_Id,
Header_Number, Details_Id,
Details_Header_Date,
(SELECT TOP 1 c.[Comment_Description] FROM [Comment] c
WHERE c.[Comment_CarrierId] = i.[Header_CarrierId] AND c.[Comment_ParentEntityId] = i.[Header_Id]
ORDER BY c.Comment_AddedOn DESC) AS 'HeaderComments',
CONCAT(ht.[HeaderTracker_Reason], ' ', ht.[HeaderTracker_Notes]) AS 'RejectedComments',
at.[InternalComments] AS 'InternalComments',
CASE i.[Header_Status]
WHEN 'Pending' THEN ar.[HeaderTracker_ApprovedLvl] END AS 'ApprovedLevel',
CASE i.[Header_Status]
WHEN 'Pending' THEN ar.[HeaderTracker_DueBy] END AS 'ApprovedDueDate'
FROM [Header] i
LEFT OUTER JOIN [Details] ii
ON i.[Header_Id] = ii.[Details_HeaderId]
INNER JOIN [Carrier] t
ON t.[Carrier_Id] = i.[Header_CarrierId]
LEFT OUTER JOIN [HeaderTracker] ht
ON ht.[HeaderTracker_HeaderId] = i.[Header_Id] AND ht.[HeaderTracker_Type] = 'Rejected'
LEFT OUTER JOIN [AdjustmentTracker] at
ON at.[DetailsId] = ii.[Details_Id]
LEFT OUTER JOIN [HeaderTracker] ar
ON ar.[HeaderTracker_HeaderId] = i.[Header_Id]
AND i.[Header_Status] IN ('Paid', 'Pending', 'Approved', 'PaidWithAdj')
AND ar.[HeaderTracker_IsPending] = 1
The result output should be using LINQ query / LAMBDA expression
The following code is what I tried: (not sure how TOP 1 and CASE would come here + if & is also valid here)
var statuses = new string[] { "Paid", "Pending", "Approved", "PaidWithAdj" };
var hdrdet =
(
from hdr in dbContext.Headers
join det in dbContext.Details on hdr.Header_Id equals det.Header_Id into hdrdet
from h in hdrdet.DefaultIfEmpty()
join carr in dbContext.Carriers on hdr.Carrier_Id equals carr.Header_Carrier_Id into carrs
from c in carrs.DefaultIfEmpty()
join hdrtk in dbContext.HeaderTracker on hdr.Header_Id equals hdrtk.HeaderTracker_HeaderId && hdrtk.HeaderTracker_Type equals "Rejected" into hdrtks
from k in hdrtks.DefaultIfEmpty()
join adjt in dbContext.AdjustmentTracker on adjt.DetailsId equals det.Details_Id into adjts
from a in adjts.DefaultIfEmpty()
join pnd in dbContext.HeaderTracker on pnd.HeaderTracker_HeaderId equals hdr.Header_Id && hdr.Header_Status contains(statuses) && pnd.[HeaderTracker_IsPending] equals 1 into pnds
from p in pnds.DefaultIfEmpty()
select new
{
hdrid = Header_Id,
hdrnumber = Header_Number, Details_Id,
det_date = Details_Header_Date,
reason = HeaderTracker_Reason + ' ' + HeaderTracker_Notes,
intcomments = InternalComments
}
)

ORA-00904: invalid identifier but table and column names are correct?

I have the below SQL query but it gives the error message ORA-00904: "KUST_ADR"."KU_NR": invalid identifier even though those are the correct table and column names. What else could be the cause?
update auf_adr
set email = (select k.ku_email
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
)
where auf_adr.adr_art = 2 and
exists (select 1
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
);
There is a "and" missing after each of the "where" clause line, this could be the issue.
where auf_adr.auf_nr = k.auf_nr AND

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.