MySQL error Greatest-n-per-group - mysql

SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
Unknown column 'ma_forum.not_id' in 'on clause'

The column not_id does not exist in the ma_forum table.
If the column does exist, try the following and report back.
SELECT ma_forum.*,
ma_forum_cat.*
FROM ma_forum JOIN
ma_forum_cat ON ma_forum.notcat_id = ma_forum_cat.notcat_id JOIN
ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id = '$notcat_id' AND
ma_forum.not_status= 'Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC

Related

Error using WITH clause and UPDATE in MySql

i tried to run this query:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product
SET product.stock = updateables.quantity
WHERE product.id_product = updateables.id_product```
But this trows:
Error Code: 1054. Unknown column 'updateables.id_product' in 'where clause```
Defining a CTE using the WITH clause does not implicitly join that CTE to other tables in your query.
You would need to use a JOIN in your query to the CTE:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product JOIN updateables ON product.id_product = updateables.id_product
SET product.stock = updateables.quantity;

Column not found in where with inner joins

I have this mysql query:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`sumaTotal` >= 100 and `sumaTotal` <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
Mysql says: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sumaTotal' in 'where clause'
Please help I think that I can use more than one select o some, but don't know how...
In your query sumaTotal is an alias of SUM(sd_documentos.total). So either use HAVING sumaTotal >= 100 and sumaTotal <= 200 or WHERE SUM(sc_documentos.total) >= 100 AND SUM(sc_documentos.total) <= 200
sumaTotal is an alias of SUM part in your query so you can change the query like that:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
HAVING (SUM(sc_documentos.total) >= 100 and SUM(sc_documentos.total) <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
This runs after simialbi instructions: (thanks again)
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie` from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`tipo` like '%presupuesto%' and `sc_documentos`.`idUsuario` = 1682) and `sc_documentos`.`deleted_at` is null
group by `idCliente` having sumaTotal>=600 and sumaTotal<=800
order by `sc_documentos`.`created_at` desc

Update Statement using Derived Table

I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id

Insert a parameter into Where Clause

I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan

Invalid use of group function while updating a table with sum function

I have two tables: o_daily_lcsgeneration and o_daily_generation.
While trying to update the o_daily_generation I receive an error saying:
error(1111) invalid use of Group function
Here is the code I am running:
update o_daily_generation join o_daily_lcsgeneration
on o_daily_generation.Location =o_daily_lcsgeneration.Location
and o_daily_generation.Date =o_daily_lcsgeneration.Date
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)
Try this instead:
UPDATE o_daily_generation od
INNER JOIN
(
SELECT Location, SUM(Turbine_Generation) TurbineSum
FROM o_daily_lcsgeneration
GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum
I did update the above query based on above answer but there is an issue.
UPDATE tmpTotal t1
INNER JOIN
(
SELECT thirdlevel_delivery_id, MAX(baseline_id) MaxBaseLineId
FROM tmpTotal
GROUP BY release_id
) t2 ON t1.release_id = t2.release_id
SET t1.planned_delivery_date = t2.MaxBaseLineId;
It gives
Error Code : 1054
Unknown column 't2.release_id' in 'on clause'