any suggestions on how to overcome ORA-00937? - sql

select s.S_FIRST||' '||s.S_LAST, sum(c.CREDITS) from enrollment e,student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by s.S_ID
having sum(c.credits)>12 order by s.s_id;
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
It keeps coming back with an error, any suggestions ?
thanks for your cooperation

Actually there were a couple issues in the SQL. Try this...
select s.s_id, s.S_FIRST||' '||s.S_LAST name, sum(c.CREDITS) sum_credits
from enrollment e,student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by 1, 2
having sum(c.credits) > 12
order by s.s_id;
You must group by all nonaggregate fields. Also I don't think you can order on a field not in your query (and hence would also need to be in the select list and group by). The error in the docs is ORA-00937.
Based on your invalid number comments, I think your join is wrong on course_no or maybe credits isn't a number or something, though.

You meed to include all your columns in select list to group by. See here http://www.dba-oracle.com/t_ora_00979_not_a_group_by_expression.htm
From documentation
Cause: The GROUP BY clause does not contain all the expressions in the
SELECT clause. SELECT expressions that are not included in a group
function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must
be listed in the GROUP BY clause.
Action: Include in the GROUP BY clause all SELECT expressions that are
not group function arguments
Remedy, include all columns in select list to your group by clause. change your query like
select s.S_FIRST||' '||s.S_LAST as fullname, s.s_id,
sum(c.CREDITS) as total_credit from enrollment e,
student s,course c
where s.s_id=e.S_ID
and c.COURSE_NO=e.C_SEC_ID
group by s.S_FIRST||' '||s.S_LAST
having total_credit > 12
order by s.s_id;
Per Oracle spec error ORA-01722 means The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal.
Make sure all your fields are of INT type or same type. IS c.CREDITS INT type?
are s.s_id and e.S_ID of same type?
are c.COURSE_NO and e.C_SEC_ID of same type?

Related

Can't find the lowest value from percentage query

I'm looking for the lowest value from percentage value from one subquery in PostgreSQL.
This is what I tried:
-- Objective, find lowest value from percent subquery to return only one row
WITH lowest_pct AS
(
SELECT
c2010.geo_name, -- Geographical name
c2010.state_us_abbreviation AS state_name, --State abbreviation
-- p0010001 = total population for each county used in 2010 and 200
-- Here, it's percentage change query
ROUND(((CAST(c2010.p0010001 AS NUMERIC(8,1)) - c2000.p0010001) / c2010.p0010001)*100 , 1)
AS pct_change
FROM us_counties_2010 AS c2010 INNER JOIN us_counties_2000 AS c2000
ON c2010.state_fips = c2000.state_fips
AND c2010.county_fips = c2000.county_fips
AND c2010.p0010001 <> c2000.p0010001
ORDER BY pct_change ASC
)
SELECT
geo_name,
state_name,
MIN(pct_change)
FROM lowest_pct;
The result:
ERROR: ERROR: Column 'low_pct.geo_name' must appear in GROUP BY clause or must be used in an aggregate function
LINE 17: geo_name,
^
SQL Status: 42803
Character: 459
How to fix this?
Like Frank already hinted, you need a GROUP BY clause in the outer SELECT.
While being at it, I simplified / fixed a couple of other minor things:
SELECT geo_name, state_name, min(pct_change) AS min_pct_change
FROM (
SELECT c2010.geo_name
, c2010.state_us_abbreviation AS state_name
, round(((c2010.p0010001 - c2000.p0010001) * 100 / c2010.p0010001)::numeric , 1) AS pct_change
FROM us_counties_2010 AS c2010
JOIN us_counties_2000 AS c2000 USING (state_fips, county_fips)
WHERE c2010.p0010001 <> c2000.p0010001
) lowest_pct
GROUP BY geo_name, state_name -- !!!
ORDER BY min_pct_change, state_name, geo_name;
While working with round(expr, 1), there is no need (no point really) to also cast to numeric (8,1) before rounding. The result is type numeric (without modifier) either way.
No need for a CTE. A subquery is cheaper, especially in Postgres 11 or older.
ORDER BY in the CTE (or subquery) is pointless. That order may or may not be carried over after the aggregate in the outer SELECT. I added ORDER BY min_pct_change, state_name, geo_name to the outer SELECT. (You might want ORDER BY min_pct_change DESC ...?)

Specified Expression not part of an aggregate function

SELECT
Product_Line_ID=2 OR Product_Line_ID=3,
COUNT(Product_Finish), MIN(Standard_Price)
FROM Product_T
WHERE Product_Finish
GROUP BY Standard_Price
HAVING AVG(Standard_Price) <700
ORDER BY Product_FInish;
I keep getting this error: Your query does not include the specified expression 'Product_Line_ID=2 OR Product_Line_ID=3' as part of an aggregate function. Can anyone help me with this? Not sure how to select product line id that is 2 or 3.
What is confusing about the error message? Product_Line_ID=2 OR Product_Line_ID=3 is not valid SQL.
Your query basically makes no sense. You have boolean conditions in the SELECT, you have a WHERE clause with a column name and no conditions, you are ordering by the column you are counting.
I can guess that you intend something like this:
SELECT Product_Line_ID, COUNT(Product_Finish), MIN(Standard_Price)
FROM Product_T
WHERE Product_Line_ID IN (2, 3)
GROUP BY Product_Line_ID
HAVING AVG(Standard_Price) < 700
ORDER BY COUNT(Product_Finish);

column "pedroad.id" must appear in the GROUP BY clause or be used in an aggregate function

select di.seq, di.node , di.edge , di.cost, a.geom
from pgr_dijkstra(
'select id, target, source, sum(cost) from pedroad',
array(select get_source2('location1'))
,array(select get_target2('test4'))
,false) as di,
pedroad a
where di.node = a.source;
error: column "pedroad.id" must appear in the GROUP BY clause or be used in an aggregate function
How should I use group by?
There is at least one syntax error in the SQL string you are passing as first parameter: sum(cost) is an aggregate function, so all other columns in the SELECT list must appear in the GROUP BY clause or be used in an aggregate function - like the error message says.
This would fix the syntax error:
SELECT di.seq, di.node, di.edge, di.cost, a.geom
FROM pgr_dijkstra('select id, target, source, sum(cost) from pedroad
group by 1,2,3'
, array(select get_source2('location1'))
, array(select get_target2('test4'))
, false) di
JOIN pedroad a ON di.node = a.source;
But its unclear how you actually wanted to sum ...
If id happens to be the PK of pedroad, you can simplify to just group by 1.
Explanation:
Return a grouped list with occurrences using Rails and PostgreSQL
Concatenate multiple result rows of one column into one, group by another column

...oracle group by syntax for beginners

What is the problem in this please?
select inst.id
, inst.type as "TypeOfInstall"
, count(inst.id) as "NoOfInstall"
from dm_bsl_ho.installment inst
group by inst.type
You're not allowed to use single function with group function. Like mixing count with single row function.
You should include the group by function:
select inst.type as "TypeOfInstall"
, count(inst.id) as "NoOfInstall"
from dm_bsl_ho.installment inst
GROUP BY inst.type;
When you do a GROUP BY in most RDBMSs, your selection is limited to the following two things:
Columns mentioned in the GROUP BY - in your case, that's inst.type
Aggregate functions - for example, count(inst.id)
However, the inst.id at the top is neither one of these. You need to remove it for the statement to work:
SELECT
type as "TypeOfInstall"
, COUNT(id) as "NoOfInstall"
FROM dm_bsl_ho.installment
GROUP BY type

Error in group by using hive

I am using the following code and getting the error below
select d.searchpack,d.context, d.day,d,txnid,d.config, c.sgtype from ds3resultstats d join
context_header c on (d.context=c.contextid) where (d.day>='2012-11-15' and d.day<='2012-11-25' and c.sgtype='Tickler' and d.config like
'%people%') GROUP BY d.context limit 10;
FAILED: Error in semantic analysis: line 1:7 Expression Not In Group By Key d
I am guessing I am using the group by incorrectly
when you use group by, you cannot select other additional field. You can only select group key with aggregate function.
See hive group by for more information.
Related questions.
Code example:
select d.context,count(*)
from ds3resultstats
...
group by d.context
or group by multiply fields.
select d.context, d.field2, count(*)
from ds3resultstats
...
group by d.context, d.field2
It is expecting all the columns to be added with group by.
Even I am facing the same issue however I managed to get a work around to these kind of issues.
you can use collect_set with the column name to get the output. For example
select d.searchpack,collect_set(d.context) from sampletable group by d.searchpack;