ORA-00979: not a GROUP BY expression error - mysql

Keep getting this error when everything from my select is in my group by statement as far as I can see
SELECT DISTINCT Case_Client_ID, Case_Client_Forename, Case_Client_Surname, SUM(Hours_Spent*Rate_Price_Per_Hour) AS
Money_Paid
FROM Case_Client, Legal_Case, Note, Rate
WHERE Case_Client.Case_Client_ID = Legal_Case.FK_Case_Client_ID
AND Legal_Case.Legal_Case_ID = Note.FK_Legal_Case_ID
AND Rate.Rate_ID = Note.FK_Rate_ID
GROUP BY Case_Client_ID, Case_Client_Forename, Case_Client_Surname
HAVING ROWNUM<=(SELECT COUNT(*)FROM Case_Client)/4
ORDER BY Money_Paid DESC;

Assuming Oracle and not MySQL, both are tagged.
The problem is rownum in the having clause. Rownum will have multiple values for a single group.
I am unclear what you are attempting to do, so I don't have a fix to suggest.

Related

Mysql - Group by and select max value

I have the following tables:
reservas_tanatosalas
I have the following query:
SELECT r.cod_reserva, MAX(r.hora_fin_utc) FROM reservas_tanatosalas r GROUP BY r.cod_tanatosala
Results: Is showing the right max value but the wrong cod_reserva. Why?
cod_reserva MAX(r.hora_fin_utc)
7 9999999999
6 9999999999
What I want to get?:
cod_reserva MAX(r.hora_fin_utc)
7 9999999999
8 9999999999
UPDATE
cod_reserva could change to varchar in the future hence is not an option to me use max on it.
Do not use GROUP BY for this. Your query is malformed and won't run in the most recent versions of MySQL (with the default settings) or almost any database.
select r.*
from r
where r.hora_fin_utc = (select max(r2.hora_fin_utc)
from reservas_tanatosalas r2
where r2.cod_tanatosala = r.cod_tanatosala
);
Think about the problem as a filtering problem, not an aggregation problem. You want to filter the data so only the most recent row shows up in the result set.
If performance is an issue, then you want an index on (cod_tanatosala, hora_fin_utc).
This is the your query:
This is the query:
SELECT r.cod_reserva, MAX(r.cod_tanatosala) FROM reservas_tanatosalas r
GROUP BY r.cod_tanatosala
HAVING MAX(r.hora_fin_utc)
This is saying:
produce one row for each value of cod_tanatosala
return the maximum value of cod_tanatosala
ERROR HERE: Don't know what to do with cod_reserva. It is not the argument to an aggregation function or in the GROUP BY.
The HAVING is simply stating that MAX(r.hora_fin_utc) is neither 0 nor NULL. Not very useful.
You are grouping the resultset using a different column, while your select statement refers to a different one. Following should definitely work, please let me know if it doesn't:
SELECT
r.cod_reserva, MAX(r.hora_fin_utc)
FROM
reservas_tanatosalas r
GROUP BY
r.cod_reserva
HAVING
MAX(r.hora_fin_utc)
I ran the same query as yours,
SELECT MAX(r.cod_reserva), MAX(r.hora_fin_utc) FROM reservas_tanatosalas r GROUP BY r.cod_tanatosala
But I used an aggregate function MAX() on r.cod_reserva as well because without it gives an error "this is incompatible with sql_mode=only_full_group_by" and I got it working, you can test it with MAX(r.cod_reserva).
Firstly in most databases including new version of Mysql this code will issue error as you are grouping by a column that is not part of select. Following query will give you what you are after for the dataset you have shown
select (select t.cod_reserva from reservas_tanatosalas t
where t.hora_fin_utc = a.max_hora_fin_utc
and a.cod_tanatosala = t.cod_tanatosala) cod_reserva,
a.max_hora_fin_utc
from (
SELECT x.cod_tanatosala, MAX(x.hora_fin_utc)max_hora_fin_utc
FROM reservas_tanatosalas x group by x.cod_tanatosala
)a;

Converting SQL Server Query to MySQL Query Using Subquery Alias

everyone.. i have a view in SQL Server that need to be ported to MySQL.
SELECT Geb_ID, Geb_Key, Geb_Jahr, Geb_Parzelle, Geb_Standort,
Geb_GebArtID, Geb_BesID, Geb_boolJB, Geb_Info,
(SELECT TOP (1) Geb_BesID FROM TGebaude AS xGeb
WHERE Geb_Key = a.Geb_Key ORDER BY Geb_Jahr DESC) AS Akt_BesID
FROM TGebaude AS a
I've tried to convert this query using LIMIT 1 (because in MySQL there is no TOP 1) but still not succeed because there is subquery using "a" alias for it's own table. is there any way to convert this query?
There are qualifiers missing in your subquery. (And that shouldn't really be a problem, as the inner/local table should have precedence over the main/outer one.) Apart from this I see no issue. Swap TOP for LIMIT and you should be done. Please try with the qualifiers added:
SELECT
Geb_ID, Geb_Key, Geb_Jahr, Geb_Parzelle, Geb_Standort,
Geb_GebArtID, Geb_BesID, Geb_boolJB, Geb_Info,
(
SELECT Geb_BesID
FROM TGebaude AS xGeb
WHERE xGeb.Geb_Key = a.Geb_Key
ORDER BY xGeb.Geb_Jahr DESC
LIMIT 1
) AS Akt_BesID
FROM TGebaude AS a;

why wont multiple where clause work

I have 2 paramters ( $memberparamter and $rest_id), that i am getting from the user. But every time my server runs the statement, it does not find anything. I have double checked with my database, and it says the desired output, does exist. If i delete one of the where clause, it works great.
Have i done it the wrong way?
This is my sql statement:
SELECT
eso.order_id as order_id,
eso.member_id as member_id,
esoi.title as title,
dl.used_date as checked,
dl.order_item_id as order_item_id
FROM exp_store_orders as eso
inner join exp_store_order_items as esoi on (eso.order_id = esoi.order_id)
inner join exp_deal_keys as dl on dl.order_item_id = esoi.order_item_id
where eso.member_id = '$memberparamter' and esoi.entry_id = '$rest_id'
and eso.order_paid > 0
group BY eso.transaction_id
ORDER BY eso.transaction_id desc
You need to specify which where clause fixes the problem. If I were to speculate, I would guess that you misspelled '$memberparamter' and it really should be '$memberparameter' -- on the belief that you would spell "parameter" correctly in your code.
Is it the GROUP BY that is causing the problem. Why do you have GROUP BY when you are not aggregating any of your SELECT columns?

MySQL COUNT() causing empty array() return

MySQL Server Version: Server version: 4.1.14
MySQL client version: 3.23.49
Tables under discussion: ads_list and ads_cate.
Table Relationship: ads_cate has many ads_list.
Keyed by: ads_cate.id = ads_list.Category.
I am not sure what is going on here, but I am trying to use COUNT() in a simple agreggate query, and I get blank output.
Here is a simple example, this returns expected results:
$queryCats = "SELECT id, cateName FROM ads_cate ORDER BY cateName";
But if I modify it to add the COUNT() and the other query data I get no array return w/ print_r() (no results)?
$queryCats = "SELECT ads_cate.cateName, ads_list.COUNT(ads_cate.id),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName ORDER BY cateName";
Ultimately, I am trying to get a count of ad_list items in each category.
Is there a MySQL version conflict on what I am trying to do here?
NOTE: I spent some time breaking this down, item by item and the COUNT() seems to cause the array() to disappear. And the the JOIN seemed to do the same thing... It does not help I am developing this on a Yahoo server with no access to the php or mysql error settings.
I think your COUNT syntax is wrong. It should be:
COUNT(ads_cate.id)
or
COUNT(ads_list.id)
depending on what you are counting.
Count is an aggregate. means ever return result set at least one
here you be try count ads_list.id not null but that wrong. how say Myke Count(ads_cate.id) or Count(ads_list.id) is better approach
you have inner join ads_cate.id = ads_list.category so Count(ads_cate.id) or COUNT(ads_list.id) is not necessary just count(*)
now if you dont want null add having
only match
SELECT ads_cate.cateName, COUNT(*),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
having not count(*) is null
ORDER BY cateName
all
SELECT ads_cate.cateName, IFNULL(COUNT(*),0),
FROM ads_cate LEFT JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName
Did you try:
$queryCats = "SELECT ads_cate.cateName, COUNT(ads_cate.id)
FROM ads_cate
JOIN ads_list ON ads_cate.id = ads_list.category
GROUP BY ads_cate.cateName";
I am guessing that you need the category to be in the list, in that case the query here should work. Try it without the ORDER BY first.
You were probably getting errors. Check your server logs.
Also, see what happens when you try this:
SELECT COUNT(*), category
FROM ads_list
GROUP BY category
Your array is empty or disappear because your query has errors:
there should be no comma before the FROM
the "ads_list." prefix before COUNT is incorrect
Please try running that query directly in MySQL and you'll see the errors. Or try echoing the output using mysql_error().
Now, some other points related to your query:
there is no need to do ORDER BY because GROUP BY by default sorts on the grouped column
you are doing a count on the wrong column that will always give you 1
Perhaps you are trying to retrieve the count of ads_list per ads_cate? This might be your query then:
SELECT `ads_cate`.`cateName`, COUNT(`ads_list`.`category`) `cnt_ads_list`
FROM `ads_cate`
INNER JOIN `ads_list` ON `ads_cate`.`id` = `ads_list`.`category`
GROUP BY `cateName`;
Hope it helps?

MySQL, Using a Subquery to get Rank of Score

I am trying to find out where this particular player ranks among shooting guards in the NBA. I am using this post on stackoverflow for a guide.
I get the error "Invalid use of group function".
SELECT
first,
last,
team,
pos,
SUM(points) AS scoresum,
ROUND(AVG(points), 2) AS avgpoints,
(SELECT
COUNT(*)
FROM nbaboxscore AS bpnb
WHERE (bpnb.first, bpnb.last, SUM(bpnb.points)) >= (bpn.first, bpn.last, SUM(bpn.points))) AS rank
FROM nbaboxscore AS bpn
WHERE bpn.pos = 'SG'
AND bpn.date >= '2009-10-01'
AND FIRST = 'Joe'
AND LAST = 'Johnson'
GROUP BY bpn.first, bpn.last, bpn.team
ORDER BY scoresum DESC
I'm not exactly sure if it's possible this way?
Your subquery is wrong, you cannot use SUM without GROUP BY and in WHERE, so you have to use HAVING.
I let you check : http://dev.mysql.com/doc/refman/5.0/fr/select.html