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
Related
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)
I'm trying to write a query to return the town, and the number of runners from each town where the number of runners is greater than 5.
My Query right now look like this:
select hometown, count(hometown) from marathon2016 where count(hometown) > 5 group by hometown order by count(hometown) desc;
but sqlite3 responds with this:
Error: misuse of aggregate: count()
What am i doing wrong, Why cant I use the count() here, and what should I use instead.
When you're trying to use an aggregate function (such as count) in a WHERE cause, you're usually looking for HAVING instead of WHERE:
select hometown, count(hometown)
from marathon2016
group by hometown
having count(*) > 5
order by count(*) desc
You can't use an aggregate in a WHERE cause because aggregates are computed across multiple rows (as specified by GROUP BY) but WHERE is used to filter individual rows to determine what row set GROUP BY will be applied to (i.e. WHERE happens before grouping and aggregates apply after grouping).
Try the following:
select
hometown,
count(hometown) as hometown_count
from
marathon2016
group by
hometown
having
hometown_count > 5
order by
hometown_count desc;
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
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;
I have the following query which returns the following error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
SELECT Part from Parts Where count(Part) > 1
How could i rewrite it to return the part that appears more than once.
You need to use a GROUP BY and HAVING clause like this:
SELECT part
FROM Parts
GROUP BY part
HAVING COUNT(*) > 1
A perfect opportunity for the rarely used HAVING clause:
SELECT Part, Count(Part) as PartCount
FROM Parts
GROUP BY Part
HAVING Count(Parts) > 1
try this:
select part from parts group by part having count(part) > 1