Sum two alias column in mysql - mysql

I tried to sum two alias column but failed, here's my query:
SELECT MAX(a.`score`) AS score1,SUM(c.`testscore`) AS score2,b.`first_name`,b.`last_name`,b.`picture` ,
SUM(score1 + score2) AS grandtotal
FROM test AS a
INNER JOIN employees AS b
ON a.`id` = b.`id`
INNER JOIN game AS c
ON a.`id` = c.`id`
GROUP BY a.`id`;
Getting error: Unknown column 'score1 ' in 'field list'

Solution:
SELECT MAX(a.`score`) AS score1,SUM(c.`testscore`) AS score2,b.`first_name`,b.`last_name`,b.`picture` ,
(MAX(a.`score`) + SUM(c.`testscore`)) AS grandtotal
FROM test AS a
INNER JOIN employees AS b
ON a.`id` = b.`id`
INNER JOIN game AS c
ON a.`id` = c.`id`
GROUP BY a.`id`;

Related

How to recognize parent alias in Join (SELECT?

I get the error below:
ER_BAD_FIELD_ERROR: Unknown column 'c.id' in 'where clause':
SELECT *
FROM clients c
LEFT JOIN
(SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
WHERE client_id = c.id) AS l ON
l.client_id = c.id
I need use WHERE to group smpp_user columns for each c.id from main SELECT.
Help please? I believe it's possible.
Just remove WHERE clause in your sub query and use GROUP BY:
SELECT *
FROM clients c
LEFT JOIN (
SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
GROUP BY client_id
) AS l ON l.client_id = c.id

Make multiple row result to one row SQL

I'm trying to create
select c.FullName, b.*, e.Description as Posisilama, f.Description as Posisibaru, g.Description,
GROUP_CONCAT('b.nilai' SEPARATOR ',') AS nilai
from penilaian_header a
left join penilaian_detail b on a.KodePenilaian = b.KodePenilaianH
left join employee c on a.Nip = c.Nip
left join HistoryPosition d on a.KodePenilaian = d.KodePenilaian
left join Position e on d.OldPosition = e.PositionCode
left join Position f on d.NewPosition = f.PositionCode
left join Outlet g on c.OutletCode = g.OutletCode
Can you check my query, because I got this error:
Incorrect syntax near 'SEPARATOR'
What I need to do is to make multiple row result in to one row SQL. Because my result is like this
I found a solution. I use pivot.
select * from
(
select row, c.Nilai,b.Fullname,a.KodePenilaian,d.Description from penilaian_header a
left join employee b on a.Nip = b.Nip
left join outlet d on a.Outlet = d.OutletCode
left join (select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, Nilai,KodePenilaianH from penilaian_Detail
) c on a.KodePenilaian = c.KodePenilaianH where a.Outlet like '%%' and Periode like '%%'
) nilai
pivot
(
sum(nilai)
for row in ([1],[2],[3],[4],[5])
) piv;
miss a group by clause,check this :
How to use GROUP_CONCAT in a CONCAT in MySQL
i will write like this :
select c.FullName, b.*, e.Description as Posisilama
, f.Description as Posisibaru, g.Description,
GROUP_CONCAT('b.nilai' SEPARATOR ',') AS nilai
from penilaian_header a
left join penilaian_detail b on a.KodePenilaian = b.KodePenilaianH
left join employee c on a.Nip = c.Nip
left join HistoryPosition d on a.KodePenilaian = d.KodePenilaian
left join Position e on d.OldPosition = e.PositionCode
left join Position f on d.NewPosition = f.PositionCode
left join Outlet g on c.OutletCode = g.OutletCode
group by c.FullName, b.*, e.Description
, f.Description , g.Description

mysql query on joins using order by DESC

SELECT a.id, b.title, b.identifier,c.rating
FROM mdl_course_modules a
LEFT JOIN mdl_scorm_scoes b
ON a.instance = b.scorm
and b.scormtype = ''
LEFT JOIN training_rating C
ON C.training_id = a.id
and c.user_id = '1'
WHERE a.module='18'
ORDER BY rating DESC
error in : #1054 - Unknown column 'c.rating' in 'field list'
You alias traning_rating as C but you refer it as c(lowercase), that is the cause of the error. Please try this:
SELECT a.id, b.title, b.identifier,c.rating
FROM mdl_course_modules a
LEFT JOIN mdl_scorm_scoes b
ON a.instance = b.scorm and b.scormtype = ''
LEFT JOIN training_rating c
ON c.training_id = a.id and c.user_id = '1'
WHERE a.module='18'
ORDER BY rating DESC

join two inline tables - unknown column?

I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:
SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING max(n.orders) >= 6) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
count(n.ordervalue) as bought,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId
I'm more used to SQL Server, not MySQL. What am I getting wrong?
I assume that E1.extensao means E1.boughtEnough? Look closely at how E1 is defined:
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
...) t
...) E1
There's a t.boughtEnough, but you're not "passing it up" to E1.

Error: Unknown Column in 'field list'

I receive an error that the column ls.amount is in field list when
I run the following query.
Can anyone help me diagnose the problem.
SELECT c.name, ic.keyword, COUNT(ic.keyword), SUM(ls.amount), ls.buyer FROM in_clicks AS ic
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
INNER JOIN leads AS l ON (ic.id = l.in_click_id)
INNER JOIN lead_status AS ls ON (l.id = ls.lead_id)
WHERE ic.create_date LIKE '%2011-08-19%' AND ic.location NOT LIKE '%Littleton%' AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
The exact error message is:
Error Code: 1054
Unknown column 'ls.amount' in 'field list'
Drop the semicolon (;) on line 4. I suspect that is ending you query before you can define the ls alias.
SELECT c.name,
ic.keyword,
COUNT(ic.keyword),
SUM(ls.amount),
ls.buyer
FROM in_clicks AS ic
INNER JOIN ads AS a
ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag
ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c
ON ag.campaign_id = c.id
INNER JOIN leads AS l
ON ( ic.id = l.in_click_id )
INNER JOIN lead_status AS ls
ON ( l.id = ls.lead_id )
WHERE ic.create_date LIKE '%2011-08-19%'
AND ic.location NOT LIKE '%Littleton%'
AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword
ORDER BY COUNT(ic.keyword) DESC