ORACLE GROUP BY error - sql

My SQL query is the following:
SELECT
oh.ORDER_NUMBER, oh.CREATED_TS, stat.STATE, stat.TIMESTAMP
FROM
IMPL_OH_ORDER_HEADER oh,
SNCR_ORDER_DISPOSITION stat,
SNCR_ORDER_DISP_HEAD stathead,
IMPL_CUST_ACCOUNT cust
WHERE
stat.DISP_TRANSACTION_ID=stathead.DISP_TRANSACTION_ID
and stathead.TRANSACTION_ID=oh.TRANSACTION_ID
and oh.ACCOUNT_ID=cust.ACCOUNT_ID
and stat.CATEGORY=100
and oh.USER_ID='a'
and cust.ACCOUNT_NUM='123'
GROUP BY oh.ORDER_NUMBER
ORDER BY stat.TRX_SEQ DESC;
It is giving the following error: ORA-00979: not a GROUP BY expression
If I remove the GROUP BY expression, I don't get the error.
What is correct GROUP BY syntax?
Research: A google search says that, if AGGREGATE functions are used, I should have all fields not used by aggregate function in the GROUP BY clause. So I tried to modify the GROUP BY expression to GROUP BY oh.ORDER_NUMBER, oh.CREATED_TS, stat.TIMESTAMP, stat.STATE.
Yet I am getting the same error.
Please help.

You get the error because you have STAT.TRX_SEQ in the order by list.
If you use the group by clause together with the order by clause, you must either order by a column on which you grouped or on an aggregated value.
This rule also applies to the selected values: they're either part of the group by or aggregate functions (or pseudo columns).

Can you try this one and see??
SELECT OH.ORDER_NUMBER,
OH.CREATED_TS,
STAT.STATE,
STAT.TIMESTAMP
FROM IMPL_OH_ORDER_HEADER OH,
SNCR_ORDER_DISPOSITION STAT,
SNCR_ORDER_DISP_HEAD STATHEAD,
IMPL_CUST_ACCOUNT CUST
WHERE STAT.DISP_TRANSACTION_ID = STATHEAD.DISP_TRANSACTION_ID
AND STATHEAD.TRANSACTION_ID = OH.TRANSACTION_ID
AND OH.ACCOUNT_ID = CUST.ACCOUNT_ID
AND STAT.CATEGORY = 100
AND OH.USER_ID = 'A'
AND CUST.ACCOUNT_NUM = '123'
GROUP BY OH.ORDER_NUMBER,
OH.CREATED_TS,
STAT.STATE,
STAT.TIMESTAMP
ORDER BY STAT.TRX_SEQ DESC;

Related

How to fix "is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" in sql

select PRES_NAME,min(PR_AGE),SPOUSE_NAME,SP_AGE
from PRES_MARRIAGE
group by PRES_NAME
why my code isn't working?
Because you have to include all non aggregate columns in your GROUP BY clause. Like this:
SELECT PRES_NAME,min(PR_AGE),SPOUSE_NAME,SP_AGE
from PRES_MARRIAGE
group by PRES_NAME,SPOUSE_NAME,SP_AGE
If you want the first marriage for each pres, then one method is:
select pm.*
from pres_marriage pm
where pm.age = (select min(pm2.age)
from pres_marriage pm2
where pm2.pres_name = pm.pres_name
);

how is the expression invalid in my simple query?

I'm new to SQL, creating a simple query from an identical example but not sure why 1 expression is invalid?
I tried this statement:
SELECT
A.AccountId,
A.Address1_City,
A.Address1_Country,
A.Address1_PostalCode,
A.Address1_StateOrProvince,
A.CreatedOn,
A.EMailAddress1,
A.mcs_ABN2,
A.mcs_AdminContact,
A.mcs_AdminContactName,
A.mcs_AreyouinterestedinInternationalDevelopmen,
A.mcs_BenefitsUtilised,
A.mcs_BusinessNameOrganisationName,
A.mcs_doyouprovideaccreditedtraining,
A.mcs_doyouprovidenonaccreditedtraining,
A.mcs_DoyouprovideservicesinanyNDISTrialSites,
A.mcs_DoyouprovideservicestoChildrenYoungPeople,
A.mcs_DummyRecord,
A.mcs_EntityStatusCode,
A.mcs_IndustryOperatingIn,
A.mcs_IsStreetAddresssameasPostalAddress,
A.mcs_MembershipNote,
A.mcs_MultiStateDepartment,
A.mcs_NotefromApplicant,
A.mcs_NumberofEmployeesDec,
A.mcs_OtherTradingNameOrganisationName,
A.mcs_PartnerDisabilityServiceOrganisations,
A.mcs_PrimaryProductsServicesToPromote,
A.mcs_RevenueDisabServiceProvisionTotalAnnual,
A.mcs_SageERPCustomerCode,
A.mcs_SageErpExportFlag,
A.ModifiedOn,
A.Name,
A.nds_Account_MembershipNumberCopy,
A.nds_IsMember,
A.nds_MainContact,
A.nds_MainContactName,
A.nds_MemberCategoryCopy,
A.PrimaryContactId,
A.Revenue,
A.Revenue_Base,
A.StatusCode,
A.WebSiteURL
FROM
account A
WHERE
A.statuscode = 1
GROUP BY
A.name
and got this error:
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Column 'account.AccountId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Group By is for aggregate functions (Count, Sum, etc), so if you use group by you need to group by at least every column being returned in the select SQL, otherwise it wouldn't be able to do the aggregation. This looks like you just want to order by Name though, so if you replace
GROUP BY A.name
with
ORDER BY A.name
it should work. If you don't care about order or aggregation just remove that part of the query entirely.

table alias not work in subquery in oracle

I am generating records with sum aggregate function and subquery, but the alias is not work there in inner query.
my query is
select UPP.item_total,
(select sum(INN.item_value_afs) total_item_value_afs from
(select distinct INN.reg_no,INN.tpt_cuo_nam,INN.item_total,INN.item_value_afs
from sigtasad.customs_import_data INN where INN.reg_no=UPP.reg_no and INN.tpt_cuo_nam=UPP.tpt_cuo_nam)) total_item_value,
sum(UPP.code_tax_amount), UPP.cmp_nam from SIGTASAD.CUSTOMS_IMPORT_DATA UPP where
UPP.reg_no='38699' and UPP.company_tin='9003247336' group by
UPP.reg_no,UPP.tpt_cuo_nam,UPP.cmp_nam,UPP.item_total ;
this query generate this error :
ORA-00904: "UPP"."TPT_CUO_NAM": invalid identifier
I want like this result!!!
Your query has numerous errors and bad habits. For instance:
You qualify a column name with an undefined table alias.
You are aggregating by columns not in the select.
You are using sum() on a subquery that has sum().
Based on the picture that you show, you probably want something like this:
select upp.item_total,
sum(item_value_afs) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Or perhaps:
select upp.item_total,
sum(sum(item_value_afs)) over (partition by upp.cmp_nam, upp.item_total) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Your innermost subquery
(select distinct nn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)
references a table that is not joined (upp). It also does not have an alias but that problem would come later. Please note, that there also seems to be a type nn.reg_no instead of inn.reg_no
The structure of the tables is not displayed here but fixing the problem would mean something along the lines of:
(select distinct inn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn, SIGTASAD.CUSTOMS_IMPORT_DATA upp
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)

SQL COUNT FORM JOIN TABLES

I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).

Sum is not getting after grouping also in SQL Server

I have a query like this.
select
TC.F_Exhibition_Code, TC.F_Exhibition,
c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson,
c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate,
TC.F_Paymentmethod, TC.F_Currency,
TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt,
sum(TC.F_Totalamt) as sum
from
T_CreditInvoice TC
left join
T_Customer c on c.F_Customer_Code = tc.F_Customer_Code
where
TC.F_CreditInvoiceNo = 'INV100098'
group by
TC.F_Exhibition_Code, TC.F_Exhibition, c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson, c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate, TC.F_Paymentmethod,
TC.F_Currency, TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt
I trying to get sum of my F_Totllamt column, but I am not getting it.
What is wrong with my query?
You can use window function and write as:
sum(TC.F_Totalamt) OVER (PARTITION BY TC.F_CreditInvoiceNo ORDER BY (SELECT 1))as sum
in existing query.. hope it helps!!
Demo
You're grouping on the variable you want to get the sum of. Hence you get the sum of every single "TC.F_Totalamt" because you're also grouping over that same variable.
Just remove "TC.F_Totalamt" from the "group by" statement and it will work.