how is the expression invalid in my simple query? - sql

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.

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
);

Query not executing, unable to run in Big

Quer logs.type = apps.log_type
WHERE logs.datetime query
Quer logs.type = apps.log_type
WHERE logs.datetime
query enter code here : the sql
Query not executing, unable to run in sql server
For GROUP BY to work it should be used along with aggregate functions in the SELECT list
Something like below example:
#standardSQL
SELECT
MAX(logs.datetime) latest_datetime,
logs.type,
SUM(LENGTH(logs.message)) messages_length,
apps.name
FROM logs INNER JOIN apps
ON logs.type = apps.log_type
WHERE logs.datetime > "2017-07-01T00:00:00"
AND logs.datetime < "2017-07-02T00:00:00"
GROUP BY apps.name, logs.type
so the rule of thumb is - any field in SELECT statement should be either in GROUP BY list or (if not in GROUP BY) to be used with Aggregate Function

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

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

ORACLE GROUP BY error

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;

SQL Syntax Error Group By

I've got a sql syntax and it gives me the error:
Msg 8120, Level 16, State 1, Line 1 Column 'VEHICLEMASTR.cconduction'
is invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
Here is the SQL syntax:
SELECT A.cplateno,A.cconduction,A.cname,A.cbatterymodel
,A.dbattery,A.DlastChange,A.nlastoilkm,A.naveragekmday
,A.dkmreading,A.dfranacq,A.dfranexp,A.nlimit,A.dreading
,CONVERT(varchar(2),month(MAX(B.dinsexp)))+'/'+CONVERT(varchar(2),day(MAX(B.dinsexp)))+'/'+CONVERT(varchar(4),year(MAX(B.dinsexp))) as dinsexp
,C.corno,CONVERT(varchar(2),month(MAX(C.dregexp)))+'/'+CONVERT(varchar(2),day(MAX(C.dregexp)))+'/'+ CONVERT(varchar(4),year(MAX(C.dregexp))) as dregexp
FROM VEHICLEMASTR A
LEFT JOIN VEHICLEINSURANCE B
ON A.cplateno = B.cplateno
LEFT JOIN VREGISTRATION C
ON A.cplateno = C.cplateno
GROUP BY A.cplateno
Can anybody tell what went wrong?
The "group by" clause must name every column selected, except those columns that are aggregate functions.
FYI an "aggregate" function is one that returns a single value for many rows, like sum(), count(), etc
a.cconduction needs to be in the group by clause.
When using a Group By clause, a column must either have an aggregate function (i.e. COUNT) or be defined in the group by.
A sample group by statement with multiple grouping:
SELECT column1_name, column2_name, aggregate_function(column_name3)
FROM table_name
WHERE column_name1 operator value
GROUP BY column_name1, column_name2;
You must include all fields that you mentioned in your select except the column that has aggregate function so in your case it would be:
GROUP BY
a.cplateno, a.cconduction,a.cname,a.cbatterymodel,a.dbattery,
a.DlastChange,a.nlastoilkm,a.naveragekmday,
a.dkmreading,a.dfranacq,a.dfranexp,a.nlimit,a.dreading
instead of
GROUP BY a.cplateno
Edit:
If you want only the a.cplateno then you don't include the rest of the fields except aggregate function and a.cplateno like:
SELECT A.cplateno
,CONVERT(varchar(2),month(MAX(B.dinsexp)))+'/'+CONVERT(varchar(2),day(MAX(B.dinsexp)))+'/'+CONVERT(varchar(4),year(MAX(B.dinsexp))) as dinsexp
,CONVERT(varchar(2),month(MAX(C.dregexp)))+'/'+CONVERT(varchar(2),day(MAX(C.dregexp)))+'/'+ CONVERT(varchar(4),year(MAX(C.dregexp))) as dregexp
FROM VEHICLEMASTR A
LEFT JOIN VEHICLEINSURANCE B
ON A.cplateno = B.cplateno
LEFT JOIN VREGISTRATION C
ON A.cplateno = C.cplateno
GROUP BY A.cplateno