ORA-00979 - not a group by expression - join and count - sql

I have two sql SQL queries, and both are working. But I need to combine the two queries in a single one, and my try does not work.
These are the working queries:
from z35
inner join z13
on z35.z35_rec_key=z13.z13_rec_key
where z35_event_type='80'
select count(z35_event_type) as pocet, z35_rec_key
from z35
where z35_event_type='80'
group by z35_rec_key
I try to combine then in this way, but I am getting an error:
select count(z35_event_type) as Total, z35_rec_key, z13.z13_title
from z35 join z13
on z35.z35_rec_key=z13.z13_rec_key where z35_event_type='80'
group by z35_rec_key
The error is: ORA-00979 - not a group by expression

Presumably, you can fix this by putting both keys in the group by:
select count(z35_event_type) as Total, z35_rec_key, z13.z13_title
from z35 join
z13
on z35.z35_rec_key=z13.z13_rec_key
where z35_event_type='80'
group by z35_rec_key, z13.z13_title;
That will fix your syntax problem. Whether is does what you want is another issue, because you have not described what you really want to do.

z13.z13_title - is not in the GROUP BY clause

Related

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.

"Your query does not include the specified expression..."

I have tried endless things to get this to work and it seems to break over and over again and not work. I'm trying to GROUP BY product after I have calculated the field quantity returned/quantity ordered, but I get the error
your query does not include the specified expression 'quantity_returned/quantity_ordered' as part of an aggregate function.
I do not want to GROUP BY quantity_returned, quantity_ordered, and product, I only want to GROUP BY product.
Here's what my SQL looks like currently...
SELECT
quantity_returned/quantity_ordered AS percentage_returned,
quantity_returned,
quantity_ordered,
returns_fact.product
FROM
Customer_dimension
INNER JOIN
(
Product_dimension
INNER JOIN
(
Day_dimension
INNER JOIN
returns_fact
ON Day_dimension.day_key = returns_fact.day_key
)
ON Product_dimension.product_key = returns_fact.product_key
)
ON Customer_dimension.customer_key = returns_fact.customer_key
GROUP BY returns_fact.product;
When you use a group by you need to actually include everything in your select that isn't a aggregate function.
I have no idea how your tables are set up, but I am throwing a blind dart. If you provide fields in each of the 4 tables someone will be better able to help.
SELECT returns_fact.product, count(quantity_returned), count(quantity_ordered), count(quantity_returned)/count(quantity_ordered) as percentage returned

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.

SQL Error - not a group by expression

With this SQL Query, I am attempting to list overdue book by patron. In addition, I want to group and order the books by patron. I understand you have to use some kind of aggregate function when doing a GROUP BY function. Even so, I am getting a "not a group by expression" error. Any help is appreciated.
Edit: updated code
What if I want to get the total fees per person and book? The bottom code is still resulting in the same error.
SELECT PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE, SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS FEE_BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.PATRON_ID
HAVING SUM((SYSDATE - CHECKOUT.DUE_DATE) > 0
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME;
You need to add these 3 columns to the GROUP BY clause...
PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
So it would be...
SELECT
PATRON.LAST_NAME,
PATRON.FIRST_NAME,
PATRON.PHONE,
BOOK.BOOK_TITLE,
CHECKOUT.DUE_DATE,
SUM((SYSDATE - CHECKOUT.DUE_DATE) * 1.00) AS BALANCE
FROM CHECKOUT
JOIN PATRON
ON CHECKOUT.PATRON_ID=PATRON.PATRON_ID
JOIN COPY
ON CHECKOUT.COPY_ID=COPY.COPY_ID
JOIN BOOK
ON COPY.BOOK_ID=BOOK.BOOK_ID
WHERE CHECKOUT.RETURN_DATE IS NULL
AND CHECKOUT.DUE_DATE > SYSDATE
GROUP BY PATRON.LAST_NAME, PATRON.FIRST_NAME, PATRON.PHONE, BOOK.BOOK_TITLE, CHECKOUT.DUE_DATE
ORDER BY PATRON.LAST_NAME, PATRON.FIRST_NAME
Bottom line is, if you have a GROUP BY clause, you can only select columns that are part of the GROUP BY unless you are using some kind of scalar value function on them, like COUNT, SUM, MAX, MIN, etc.
Otherwise if you aren't grouping or using a scalar-value function, multiple rows could have different values for that column, so what would the query results be?