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

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

Related

Spark throws : "expression is neither present in the group by, nor is it an aggregate function..."

I'm trying to execute this with pyspark:
query = "SELECT *\
FROM transaction\
INNER JOIN factures\
ON transaction.t_num = factures.f_trx\
WHERE transaction.t_num != ''\
GROUP BY transaction.t_num"
result = sqlContext.sql(query)
Spark gives an error :
u"expression transaction.t_aut is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don't care which value you get.;
You forgot to add list of columns in group by statement. As you are selecting all columns in select statement.
It's saying that there is column named transaction.t_aut that you have projected in your select statement when you used select * that is not being used in your group by.
Solution is to either replace select * with the columns that are in your group by in your case transaction.t_num or add transaction.t_aut to your group by

oracle decode group by warning

I have this code
for x_eo in ( select decode(mod(card_name_id,2),0,1,1,2) e_o, count(*) nr
from rp_Deck where session_id=p_session_id_in
and position<=35 group by mod(card_name_id,2) )
I am getting sqldeveloper warning that select list inconsistent with group by.
And developer gives me solution:
select decode(mod(card_name_id,2),0,1,1,2) e_o, count(*) nr
from rp_Deck where session_id=p_session_id_in
and position<=35 group by mod(card_name_id,2), card_name_id, 2, decode(mod(card_name_id,2),0,1,1,2) )
What is difference between these two group by ?
Thanks !
In general, when you use GROUP BY in a statement then all the values either need to be:
constants;
within aggregation functions; or
in the GROUP BY clause.
SQL Developer does not realise that decode(value_mod_2,0,1,1,2) is effectively just adding 1 to the value and does not change the allocation of items to groups so, since it is not either a constant or an aggregation function, it expects the entire function to be in the GROUP BY clause.
Personally, I would write it as:
select mod(card_name_id,2) + 1 e_o,
count(*) nr
from rp_Deck
where session_id=p_session_id_in
and position<=35
group by mod(card_name_id,2)
(the + 1 is a constant so does not need to be in the GROUP BY clause)
The solution SQL Developer proposes is wrong as:
select decode(mod(card_name_id,2),0,1,1,2) e_o,
count(*) nr
from rp_Deck
where session_id=p_session_id_in
and position<=35
group by
mod(card_name_id,2),
card_name_id,
2,
decode(mod(card_name_id,2),0,1,1,2)
is effectively the same as just grouping by the finest grained grouping, so:
group by card_name_id;
Which is not what you want to group by. To be the same as your original query's intended output, it should propose something like:
group by
mod(card_name_id,2),
decode(mod(card_name_id,2),0,1,1,2)
or more simply just:
group by
decode(mod(card_name_id,2),0,1,1,2)

any suggestions on how to overcome ORA-00937?

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?

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