How can I add where in the conditional aggregation mysql? [duplicate] - mysql

This question already has answers here:
Using column alias in WHERE clause of MySQL query produces an error
(7 answers)
Closed 4 years ago.
My query like this :
SELECT a.number, a.description, b.attribute_code, b.attribute_value
FROM items a
JOIN attr_maps b ON b.number = a.number WHERE a.number = AB123
If the query executed, the result like this :
I want to make the result like this :
I use conditional aggregation like this :
SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE i.number = AB123
GROUP BY i.number, i.description
It works
But i'm still confused to add where condition. So I want it can filter by brand etc
I try like this :
SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE brand = 'honda'
GROUP BY i.number, i.description
There exist error Unknown column 'brand' in 'where clause'
How can I solve the error?

Change the last part of your query
WHERE brand = 'honda'
GROUP BY i.number, i.description
to
GROUP BY i.number, i.description
HAVING brand = 'honda'

Related

How make Query that returns all the rows of table of a specific date and all the related fields in the same row

I have two tables: blog and blog_translations:
blog:
blog_translations
I have to perform a query that returns the following:
That is to say: I have to make a query that returns all the blog rows of a specific date and all the related translations in the same row.
I have tried with
SELECT b.id, b.category, bt.title
FROM blogs b
INNER JOIN blog_translations bt ON b.id = bt.blog_id
WHERE DATE(b.created_at) = '2010-11-12';
But the result is not what was expected:
Any help please?
Thank you.
From here: https://es.stackoverflow.com/a/442290/54584
SELECT b.id, b.category,
max(case when bt.language = 'es' then bt.title end) title_es,
max(case when bt.language = 'en' then bt.title end) title_en,
max(case when bt.language = 'ca' then bt.title end) title_ca
FROM blogs b
INNER JOIN blog_translations bt ON b.id = bt.blog_id
WHERE DATE(b.created_at) = '2010-11-12'
GROUP BY b.id, b.category;

MYSQL Check if for all table 1 entries has table 2 entry against it

I have two tables Employees and Certificates. Now I have 4 certificates and I want to check for every employee which certificates are passed and which are not.
Select Distinct(certificate.certificatename)
FROM `employeemanagement$certificate` certificate
Where certificatename = '**********'
or certificatename = '************'
or certificatename = '************'
or certificatename = '************'
this will give me the 4 certificate i want
SELECT employees.employeeid, employees.employeename, certificate.certificatename, certificate.expirationdate, certificate.`status`
FROM `employeemanagement$employees` employees
LEFT JOIN `employeemanagement$certificate_employees` certifcate_employees on employees.id = certifcate_employees.`employeemanagement$employeesid`
LEFT JOIN `employeemanagement$certificate` certificate on certificate.id = certifcate_employees.`employeemanagement$certificateid`
Where employees.currentstatus = 'Active'
and this gives me all the employees with all the certificate they have passed but it wont give me result for every certificate
I think conditional aggregation is a reasonable approach:
SELECT e.employeeid, e.employeename,
MAX(CASE WHEN c.certificatename = 'C1' THEN c.expirationdate END) as c1_expiration,
MAX(CASE WHEN c.certificatename = 'C1' THEN c.staus END) as c1_status,
MAX(CASE WHEN c.certificatename = 'C2' THEN c.expirationdate END) as c2_expiration,
MAX(CASE WHEN c.certificatename = 'C2' THEN c.staus END) as c2_status,
MAX(CASE WHEN c.certificatename = 'C3' THEN c.expirationdate END) as c3_expiration,
MAX(CASE WHEN c.certificatename = 'C3' THEN c.staus END) as c3_status,
MAX(CASE WHEN c.certificatename = 'C4' THEN c.expirationdate END) as c4_expiration,
MAX(CASE WHEN c.certificatename = 'C4' THEN c.staus END) as c4_status
FROM `employeemanagement$employees` e LEFT JOIN
`employeemanagement$certificate_employees` ce
ON e.id = ce.`employeemanagement$employeesid` LEFT JOIN
`employeemanagement$certificate` c
ON c.id = ce.`employeemanagement$certificateid`
WHERE e.currentstatus = 'Active'
GROUP BY e.employeeid;
You might find it easier to work with one row per employee with all the certificates summarized on that row, rather than multiple rows per certificate.

How can I make value of a record to be title of field on the mysql? [duplicate]

This question already has answers here:
SQL Transpose Rows as Columns
(5 answers)
Closed 4 years ago.
My query like this :
SELECT a.number, a.description, b.attribute_code, b.attribute_value
FROM items a
JOIN attr_maps b ON b.number = a.number WHERE a.number = AB123
If the query executed, the result like this :
I want to make the result like this :
How can I do it?
You can use conditional aggregation:
SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE i.number = AB123
GROUP BY i.number, i.description

Counting the number of Male & Females from tables

SELECT
student_class.acad_yr,
(case when((students.gender = 'Male') then count(students.gender) end)) AS Male,
(case when((students.gender = 'Female') then count(students.gender) end)) AS Female
FROM
students
INNER JOIN student_class ON (students.st_id = student_class.st_id)
WHERE
student_class.acad_yr = '2013/2014' AND
left(student_class.class_id, 1) = '1'
GROUP BY
student_class.acad_yr
ORDER BY
students.surname,
students.othername
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then count(students.gender) end)) AS Male,
(case when((students.gender = 'Fem' at line 3
Try this :-
SELECT
student_class.acad_yr,
case when students.gender='Male' then count(students.gender) end AS Male,
case when students.gender='Female' then count(students.gender) end AS Female
FROM
students
INNER JOIN student_class ON (students.st_id = student_class.st_id)
WHERE
student_class.acad_yr = '2013/2014' AND
left(student_class.class_id, 1) = '1'
GROUP BY
student_class.acad_yr
ORDER BY
students.surname,
students.othername
Hope it will help you.
SELECT
student_class.acad_yr,
(SELECT COUNT(*) FROM STUDENTS WHERE GENDER = 'Male' AND acad_yr = student_class.acad_yr) AS Male,
(SELECT COUNT(*) FROM STUDENTS WHERE GENDER = 'Female' AND acad_yr = student_class.acad_yr) AS Female,
FROM
students AS S
INNER JOIN student_class ON (S.st_id = student_class.st_id)
WHERE
student_class.acad_yr = '2013/2014' AND
left(student_class.class_id, 1) = '1'
GROUP BY
student_class.acad_yr
ORDER BY
S.surname,
S.othername
Try this:
SELECT
student_class.acad_yr,
sum(case when students.gender = 'Male' then 1 else 0 end) AS Male,
sum(case when students.gender = 'Female' then 1 else 0 end) AS Female,
FROM
students
INNER JOIN student_class ON (students.st_id = student_class.st_id)
WHERE
student_class.acad_yr = '2013/2014' AND
left(student_class.class_id, 1) = '1'
GROUP BY
student_class.acad_yr
ORDER BY
student_class.acad_yr
I removed the columns you were using in the ORDER BY, since it didn't really make much sense.

Is it possible for a query that use a subquery to return multiple rows?

SELECT p.value AS __color__,
milestone AS __group__,
milestone,
priority,
time AS created,
COUNT(t.id) as 'total open tickets',
SUM(c.value) as 'Total Dev LOE',
SUM(d.value) as 'Total QALOE'
FROM ticket t
LEFT JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'devloe')
LEFT JOIN ticket_custom d ON (t.id = d.ticket AND d.name = 'qaloe')
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority'
WHERE t.milestone = '$MILESTONE'
AND status <> 'closed'
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC
Then add subquery to return the sum total devloe and qaloe for each priority.
Select p.value AS __color__
, milestone AS __group__
, milestone
, priority
, time AS created
, Count(t.id) as 'total open tickets'
, Sum( Case When c.name = 'devloe' Then c.value End ) As 'Total Dev LOE'
, Sum( Case When c.name = 'qaloe' Then c.value End ) As 'Total QALOE'
, PriorityCounts.Total_Dev_LOE As Priority_Total_QALOE
, PriorityCounts.Total_QALOE As Priority_Total_QALOE
From ticket As t
Left Join ticket_custom As c
On t.id = c.ticket
And c.name In('devloe', 'qaloe')
Left Join enum p
On p.name = t.priority
AND p.type = 'priority'
Join (
Select t1.priority,
, Sum( Case When c1.name = 'devloe' Then c1.value End ) As Total_Dev_LOE
, Sum( Case When c1.name = 'qaloe' Then c1.value End ) As Total_QALOE
From ticket As t1
Left Join ticket_custom As c1
On t1.id = c1.ticket
And c1.name In('devloe', 'qaloe')
Group By t1.priority
) As PriorityCounts
On PriorityCounts.priority = t.priority
Where t.milestone = '$MILESTONE'
And status <> 'closed'
Group By milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC
Doesn't answer your question, but you could simplify the query:
SELECT p.value AS __color__,
milestone AS __group__,
milestone,
priority,
time AS created,
COUNT(t.id) as 'total open tickets',
SUM(CASE WHEN c.name = 'devloe' THEN c.value ELSE 0 END) as 'Total Dev LOE',
SUM(CASE WHEN c.name = 'qaloe' THEN c.value ELSE 0 END) as 'Total QALOE'
FROM TICKET t
LEFT JOIN TICKET_CUSTOM c ON c.ticket = t.id
AND c.name IN ('devloe', 'qaloe')
LEFT JOIN ENUM p ON p.name = t.priority
AND p.type = 'priority'
WHERE t.milestone = '$MILESTONE'
AND status <> 'closed'
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC