MySQL not recognizing virtual column / field - mysql

I'm trying to manipulate two virtual fields generated of a subquery into a new field, but MySQL is telling me that "GP" is an unknown column, but it has been already declared. Please, take a look at my query:
SELECT *,
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',
(SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO',
(GP+(GC*-1)) as 'SALDO'
FROM tb_equipe as e
INNER JOIN tb_gruposEquipes as ge ON (e.id_equipe = ge.fk_id_equipe)
INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
WHERE g.fk_id_campeonato = 23
ORDER BY WO ASC
As you can see, "SALDO" would be the result of "GP-GC". But MySQL do not recognize these columns
How can I solve this?
SOLUTION
Thanks a lot for the help, guys, but I figured out the problem.
"SALDO" couldn't be created by substracting GC of GP because "GP" and "GC" doesn't exist during runtime.
So, when you need to manipulate virtual fields during the runtime, you'll have to repeat the code that generated the virtual field.
Then, if you have this select:
SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', (GP+GC) as 'SALDO' FROM (...)
You'll need to replace it with this:
SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', ((field1+1)+(field2+1)) as 'SALDO' FROM (...)

Push the stuff into in inner query
SELECT core.*,
(GP+(GC*-1)) as 'SALDO'
from (
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',
(SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO' )
FROM tb_equipe as e
) as core
INNER JOIN tb_gruposEquipes as ge ON (core.id_equipe = ge.fk_id_equipe)
INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
WHERE g.fk_id_campeonato = 23
ORDER BY WO ASC

Related

MySQL query with multiple INNER JOIN returns duplicate entries in the result

I have the following data structure:
There are multiple tables:
I need the following return: sum of balance (sum(a.balance) as pnl) from tabposition.
However, this query returns duplicate results. Can anybody help me with this? Thanks
SET #dt_inicio := '2021-12-01';
SET #dt_fim := '2021-12-09';
select a.data, a.fund, 'CASH-USD' as ticker , 'CASH' as strategy, 'Dolar Cash' as company,
'Others' as type, 'Others' as sector,
sum(a.balance) as pnl from tabposition as a
inner join tablistaativos as c on a.ticker = c.ticker
inner join tabdiautil as d on a.data = d.data_util
inner join tabhistorynav as e on a.data = e.data
inner join tabhistorynav as f on d.data_util_ant = f.data
inner join tabprice as g on a.data = g.data
inner join tabprice as i on d.data_util_ant = i.data
inner join tabhistorynav as j on d.data_util_ant = j.data and a.fund = j.fund
inner join tabfatorx as l on a.data = l.data_pos
where a.data >= #dt_inicio and a.data <= #dt_fim and a.fund = 'TORK FUND' and a.qnt <> 0
and (c.class = 'ACCO' or c.class = 'ACCOUNTING' or c.class = 'SPAC' or c.class = 'ACOES' or
c.class = 'OPCOES' or c.class = 'FUNDO')
and e.official = 'Yes' and e.fund = 'TORK FIA' and f.official = 'Yes' and f.fund = 'TORK FIA'
and g.ticker = 'usdbrl' and i.ticker = 'usdbrl'
and l.cotista = 'TORK FIA'
group by a.data
order by a.data

Getting syntax error for json in PostgreSQL: could not identify an equality operator for type json

I wrote a retrieve function in PostgreSQL. In JSON calling another object and I am getting a duplicate value so I added a distinct and run could not identify an equality operator for type JSON.
Here is my function:
return query
select(SELECT array_to_json(array_agg(row_to_json(resdt))) FROM
(select distinct vp.project_id,cvv.id as projid,vp.project_name,vp.description,vp.start_date,vp.end_date,vp.duration,
vp.label_id,vp.created_by,vp.company_id,vp.provider_name as client_name,vp.actual_end_date,
concat(vu.first_name,vu.last_name) as user_name,
--vt.name as company_name,vt.domain,
vu.email,
10 as chat_count,vp.project_start_time,vp.project_end_time,
vp.status as project_status,
(select array_to_json(array_agg(row_to_json(b))) FROM
(select vpr.project_id,vpr.project_name,vl.label_id,vl.label_name,vl.label_type
from public."VOfficeApp_projects" vpr
inner join public."VOfficeApp_project_labels" vpl on vpr.project_id = vpl.project_id
inner join public."VOfficeApp_labels" vl on vl.label_id = vpl.label_id
where vpr.company_id = 1047
and vpr.created_by = 3465
and vpr.project_id = vp.project_id
and vl.label_type ='p' )b)as label_name,
(select array_to_json(array_agg(row_to_json(b))) FROM
(select concat(u.first_name,u.last_name) as first_name,vpr.employee_id as id,
vpr.project_resource_id,u.email
from public."VOfficeApp_projects" pro
inner join public."VOfficeApp_project_resources" vpr on pro.project_id = vpr.project_id
inner join public."VOfficeApp_user" u on vpr.employee_id = u.id
where pro.company_id = 1047
and pro.created_by = 3465 and vpr.released_date is null
and pro.project_id = vp.project_id)b)as team_members
from public."VOfficeApp_projects" vp
inner join public."VOfficeApp_project_resources" vpr on vp.project_id = vpr.project_id
inner join public."VOfficeApp_user" vu on vu.id =vpr.employee_id
left join public."VOfficeApp_conversation" cvv on cvv.projtaskid = vp.project_id
where vp.company_id = 1047 and vpr.employee_id = 3465
and vp.status != 'Delete'
--order by vp.project_id desc
)resdt)as My_tasks;

Mysql adding index for column type text does not improve performance while using select query

I am using InnoDB. From this question, I found out that I have to specify the length if I want to add index to columns which type is TEXT.
But after successfully adding index, the performance for the select query stay the same. Anyone know why? I did check the index with show index from tableName and the index did exist.
So it was the last two table EventResultsFinalSummary and EventResultsPrelims.
CREATE OR REPLACE VIEW ScheduleView AS
SELECT s.ScheduleID, e.EventRound, e.EventRoundsID, e.EventID, e.NumberCouplesInRound, n.NumberOnBack, eic.EventName AS 'Division',
CONCAT(a1.FirstName, ' ', a1.LastName, ' - ', a2.FirstName, ' ', a2.LastName) AS 'Couple',
s.SessionID AS 'Session', erfs.CouplePlace, c.CoupleID,
s.Timeslot, s.SubFloor ,s.itemDuration,s.HeatNumber, o.ActivityName, st.StudioName AS 'DanceStudio', a.AgeName AS 'Age', s.competition_id, erp.CoupleVotes
FROM Schedule AS s
LEFT JOIN EventRounds AS e ON s.EventRoundID = e.EventRoundsID AND s.competition_id = e.competition_id
LEFT JOIN OtherActivities AS o ON s.OtherActivitiesID = o.OtherActivitiesID AND s.competition_id = o.competition_id
LEFT JOIN EntriesEvents AS ee ON e.EventID = ee.EventID AND e.EventRound <= ee.EventRound AND e.Competition_id = ee.Competition_id
LEFT JOIN Couples AS c ON ee.EntryID = c.CoupleID AND ee.Competition_id = c.Competition_id
LEFT JOIN NumSysComps AS n ON c.CompetitorIDMan = n.CompetitorIDMan AND c.Competition_id = n.Competition_id
LEFT JOIN Attendees AS a1 ON c.CompetitorIDMan = a1.AttendeeID AND c.Competition_id = a1.Competition_id
LEFT JOIN Attendees AS a2 ON c.CompetitorIDLady = a2.AttendeeID AND c.Competition_id = a2.Competition_id
LEFT JOIN Studios AS st ON a1.StudioID = st.StudioID AND a1.Competition_id = st.Competition_id
LEFT JOIN EventsInComp AS eic ON eic.EventID = e.EventID AND eic.Competition_id = e.Competition_id
LEFT JOIN ProAmSingleDanceEvents AS psd ON eic.ProAmSingleDanceEventID = psd.ProAmSingleDanceEventID AND eic.Competition_id = psd.Competition_id
LEFT JOIN ProAmMultiDanceEvents AS pmd ON eic.ProAmMultiDanceEventID = pmd.ProAmMultiDanceEventID AND eic.Competition_id = pmd.Competition_id
LEFT JOIN Ages AS a ON (
psd.AgeID = a.AgeID AND psd.Competition_id = a.Competition_id
OR
pmd.AgeID = a.AgeID AND pmd.Competition_id = a.Competition_id
)
LEFT JOIN EventResultsFinalSummary AS erfs ON e.EventID = erfs.EventID AND c.CoupleID = erfs.CoupleID AND s.Competition_id = erfs.Competition_id
LEFT JOIN EventResultsPrelims AS erp ON e.EventID = erp.EventID AND erp.EventRound = e.EventRound AND c.CoupleID = erp.CoupleID AND s.Competition_id = erp.Competition_id
ORDER BY s.ScheduleID;
I added index to the column that I joined.
EventID, CoupleId, Competition_id for EventResultsFinalSummary and EventID, EventRound, Competition_id for EventResultsPrelims by using query like following.
My question is that when those columns have type like varchar or int, the select * query will only take 1s. But it take 26s when the type is Text.
ALTER TABLE `EventResultsPrelims` ADD INDEX(`EventID`(6));

MySQL Update table with sum value of another table

I have a query that I can't seem to manipulate to work in a SUM function in MySQL:
Here is what I want:
UPDATE account_seeds AS a
INNER JOIN b AS b ON b.accountID = a.accountID AND a.areaID = b.areaID
INNER JOIN b_seed AS s ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c ON c.seedID = s.seedID
SET a.amount = a.amount + SUM(s.amount)
WHERE b.status='active' AND a.seedID = s.seedID
Now it obviously won't let me use the SUM in the update without separating it. I have tried joining select queries but can't quite get my head around it. The basic premise being that I have multiple buildings(rows) that has a seed value that will increase total seeds of that type in the area for a particular account. Without the sum it only updates one of the buildings that has a matching seed value
UPDATE
account_seeds AS a
INNER JOIN
( SELECT b.accountID, b.areaID, s.seedID
, SUM(s.amount) AS add_on
FROM b AS b
INNER JOIN b_seed AS s
ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c
ON c.seedID = s.seedID
WHERE b.status = 'active'
GROUP BY b.accountID, b.areaID, s.seedID
) AS g
ON g.accountID = a.accountID
AND g.areaID = a.areaID
AND g.seedID = a.seedID
SET
a.amount = a.amount + g.add_on ;
Maybe you can use a nested query:
UPDATE account_seeds AS a
INNER JOIN b AS b ON b.accountID = a.accountID AND a.areaID = b.areaID
INNER JOIN b_seed AS s ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c ON c.seedID = s.seedID
SET a.amount = a.amount + (SELECT SUM(amount) FROM b_seed)
WHERE b.status='active' AND a.seedID = s.seedID
Can you try that?

MYSQL LEFT JOIN optimization with CASE

I spent some time trying to get working this SELECT with CASE but I failed... (thank to that I'm using COLASCE() now)
How could I optimize this SELECT by using CASE/IF sentences? Is this a fast way to query from different tables selected by a field?
SELECT a.folderid, a.foldername, a.contenttype, COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor
FROM t_folders a
LEFT JOIN t_files b
ON a.contenttype = 'file' AND a.contentid = b.fileid
LEFT JOIN t_links c
ON a.contenttype = 'link' AND a.contentid = c.linkid
LEFT JOIN t_extfiles d
ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
LEFT JOIN t_videos e
ON a.contenttype = 'video' AND a.contentid = e.videoid
LEFT JOIN t_exams f
ON a.contenttype = 'exam' AND a.contentid = f.examid
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC
Using case statement will not make the query faster in your case, but since you asked for it, below is how it would look like.
SELECT a.folderid, a.foldername, a.contenttype,
(CASE a.contenttype
WHEN 'file' THEN b.descriptor
WHEN 'link' THEN c.descriptor
WHEN 'extfile' THEN d.descriptor
WHEN 'video' THEN e.descriptor
ELSE f.descriptor
END CASE) AS descriptor
FROM t_folders a
LEFT JOIN t_files b ON a.contenttype = 'file' AND a.contentid = b.fileid
LEFT JOIN t_links c ON a.contenttype = 'link' AND a.contentid = c.linkid
LEFT JOIN t_extfiles d ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
LEFT JOIN t_videos e ON a.contenttype = 'video' AND a.contentid = e.videoid
LEFT JOIN t_exams f ON a.contenttype = 'exam' AND a.contentid = f.examid
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC
If each of the t_files, t_links, etc tables has the folder_id field, I would also try doing a UNION on these tables and then left join the result with t_folders to get the folderid and foldername.
The joins have to be done this way because the come out of different tables. You cannot use CASE to switch which table a query is coming out of: the statement has to be parsed, including its data sources, before there are values to compare.
More to the point, CASE returns values, whereas table names are.. I don't know the technical term.. structural components of the query. It's sort of the same reason you can't select out of table "Cool_Stuff" with:
select * from "Cool_" + "Stuff"
Hope this answers your question!
You may do the following
select master.* , COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor
from
( SELECT a.folderid, a.foldername, a.contenttype
FROM t_folders a
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC ) master
LEFT JOIN t_files b
ON master.contenttype = 'file' AND master.contentid = b.fileid
LEFT JOIN t_links c
ON master.contenttype = 'link' AND master.contentid = c.linkid
LEFT JOIN t_extfiles d
ON master.contenttype = 'extfile' AND master.contentid = d.extfileid
LEFT JOIN t_videos e
ON master.contenttype = 'video' AND master.contentid = e.videoid
LEFT JOIN t_exams f
ON master.contenttype = 'exam' AND master.contentid = f.examid
While minimizing the result on table a, the joins operation can be optmizsed