SELECT X.workorder_id,X.order_id FROM mr_workorder_data AS X
LEFT JOIN
(SELECT order_id, workorder_id, GROUP_CONCAT(trim_id SEPARATOR '|') AS trim_id_arr
FROM mr_workorder_data
WHERE order_id = X.order_id AND workorder_id =
X.workorder_id GROUP BY order_id)
WHERE X.data_type = 'Accessories' GROUP BY X.workorder_id
You have a left join ( subselect ) without an alias add (eg:) T at the end of the subquery ()
SELECT X.workorder_id,X.order_id
FROM mr_workorder_data AS X
LEFT JOIN ( SELECT order_id,workorder_id,
GROUP_CONCAT(trim_id SEPARATOR '|') AS trim_id_arr
FROM mr_workorder_data
WHERE order_id = X.order_id AND workorder_id = X.workorder_id
GROUP BY order_id ) T
WHERE X.data_type = 'Accessories'
GROUP BY X.workorder_id
Related
hi i need help combining 2 cte to get who get 100 attendance percentage but failed at exam
here my first cte
with main as(
select ca.STUDENT_ID,
ca.SCHEDULE_ID,
s.COURSE_ID,
co.NAME as course_name,
st.NAME,
count(ca.ID) as total_attendance,
((CHAR_LENGTH(s.COURSE_DAYS) - CHAR_LENGTH(REPLACE(s.COURSE_DAYS , ',', '')) + 1) * 13) as attendance_needed
from univ.course_attendance ca
left join univ.schedule s on ca.SCHEDULE_ID = s.ID
left join univ.student st on ca.SCHEDULE_ID = st.ID
left join univ.course co on ca.SCHEDULE_ID = co.ID
group by ca.STUDENT_ID, ca.SCHEDULE_ID
)
select *,total_attendance/attendance_needed as attendance_percentage
from main
order by 1,2;
second cte
;with inputdata as
(
select es.STUDENT_ID,es.EXAM_ID,es.SCORE,e.PASS_THRESHOLD, s.NAME , c.NAME as Course_name, es.EXAM_DT,
case
when SCORE>=PASS_THRESHOLD then 'PASS'
else 'Fail'
end as Flag
from exam_submission es
left join student s on es.STUDENT_ID = s.ID
left join exam e on es.EXAM_ID = e.ID
left join course c on e.COURSE_ID = c.ID
)
select * from inputdata I
join
( select student_id,exam_id from
inputdata
group by student_id, exam_id
)T on I.student_id=T.student_id and I.exam_id=T.exam_id
order by exam_dt asc
result:
what i need student name, course name, attendace percentage & flag "failed/pass"
Just chain multiple table expressions in a single CTE by introducing "aliases" like main_ordered for the first CTE and inputdata_grouped for the second one. I'm sticking with the original naming, but it could be improved.
with
main as (
select ca.STUDENT_ID,
...
group by ca.STUDENT_ID, ca.SCHEDULE_ID),
main_ordered as (
select *,total_attendance/attendance_needed as attendance_percentage
...
order by 1,2),
inputdata as (
select es.STUDENT_ID,es.EXAM_ID,es.S...
...),
inputdata_grouped as (
select * from inputdata I
...
group by student_id, exam_id...
...
order by exam_dt asc)
select *
from main_ordered join inputdata_grouped on ...
In MySQL I have 4 tables:
- product(id)
- order(id)
- order_detail_1(id, product_id, order_id, qty)
- order_detail_2(id, product_id, order_id, qty)
I want to get the sum of the quantity of products sold from the 2 tables (order_detail_1, order_detail_2) grouping them by product
produt can existe in order_detail_1 and not in order_detail_2 and vice versa
i tested this query and it worked but I want a simpler query without the union and the subquery.
select tmp.product_id ,sum(tmp.qty) from
(
(
select order_detail_1.product_id ,sum(order_detail_1.qty)
from order_detail_1
inner join order on order_detail_1.id_order = order.id
where order_detail_1.product_id is not null
group by order_detail_1.product_id
)
union all
(
select order_detail_2.product_id ,sum(order_detail_2.qty)
from order_detail_2
inner join order on order_detail_2.id_order = order.id
where order_detail_2.product_id is not null
group by order_detail_2.product_id
)
) tmp
group by tmp.product_id
It looks like you're not using order table other then checking if it exists, so you can use EXISTS()
SELECT p.product_id,sum(p.qty) as qty
FROM (SELECT product_id,qty,id_order FROM order_detail_1
WHERE product_id IS NOT NULL
UNION ALL
SELECT product_id,qty,id_order FROM order_detail_2
WHERE product_id IS NOT NULL) p
WHERE EXISTS(SELECT 1 FROM order o
WHERE o.id = p.id_order)
GROUP BY p.product_id
If a product is in only one table, you can use left join:
select p.id, (coalesce(sum(od1.qty), 0) + coalesce(sum(od2.qty, 0))) as qty
from product p left join
order_detail_1 od1
on od1.product_id = p.id left join
order_detail_2 od2
on od2.product_id = p.id
group by p.id;
This formulation depends on the fact that the two tables are exclusion -- a product is in only one table.
EDIT:
If products can exist in both tables, then you need to aggregate them first:
select p.id, (coalesce(od1.qty, 0) + coalesce(od2.qty, 0)) as qty
from product p left join
(select product_id, sum(qty) as qty
from order_detail_1 od1
group by product_id
) od1
on od1.product_id = p.id left join
(select product_id, sum(qty) as qty
from order_detail_2 od2
group by product_id
) od2
on od2.product_id = p.id;
how do I get the max date value of a union tables? Whenever I use GROUP BY, I only get the max date value of the first table. So far, here's my code:
SELECT * FROM ((SELECT tc.id, tc.personnel_id, tpi.emp_status, tpi.firstname, tpi.lastname, tc.date_from, tc.date_to FROM tbl_contracts AS tc
JOIN (SELECT personnel_id, MAX(date_to) AS Maxdatetime
FROM tbl_contracts
GROUP BY personnel_id) AS r
ON tc.personnel_id = r.personnel_id AND tc.date_to = r.Maxdatetime
LEFT JOIN tbl_personnel_info AS tpi
ON tpi.id = tc.personnel_id
WHERE tpi.firstname LIKE CONCAT('%', '', '%')
AND tpi.lastname LIKE CONCAT('%', '', '%')
ORDER BY tc.date_to DESC)
UNION ALL
(SELECT tpss.id, tpss.personnel_id, tpi.emp_status, tpi.firstname, tpi.lastname, tpss.date_from, tpss.date_to
FROM tbl_personnel_sea_service AS tpss
JOIN (
SELECT personnel_id, MAX(date_to) AS Maxdatetime
FROM tbl_personnel_sea_service
GROUP BY personnel_id
) AS r
ON tpss.personnel_id = r.personnel_id AND tpss.date_to = r.Maxdatetime
LEFT JOIN tbl_personnel_info AS tpi
ON tpi.id = tpss.personnel_id
WHERE tpi.firstname LIKE CONCAT('%', '', '%')
AND tpi.lastname LIKE CONCAT('%', '', '%')
AND agency_name = 'UMMI'
ORDER BY tpss.date_to DESC)) AS tmain
GROUP BY tmain.personnel_id
The result is correct. However if I use GROUP BY tmain.personnel_id, sorting result is wrong. It's not getting the max date value of the second table
I'd suggest you take the group by over all the column and aggregate the date and finally order by date_to (or whichever column/s you need)-
SELECT personnel_id,
emp_status,
first_name,
lastname,
MIN(date_from) date_from,
MAX(date_to) date_to
FROM (
(SELECT tc.id,
tc.personnel_id,
tpi.emp_status,
tpi.firstname,
tpi.lastname,
tc.date_from,
tc.date_to
FROM tbl_contracts AS tc
JOIN
(SELECT personnel_id,
MAX(date_to) AS Maxdatetime
FROM tbl_contracts
GROUP BY personnel_id
) AS r
ON tc.personnel_id = r.personnel_id
AND tc.date_to = r.Maxdatetime
LEFT JOIN tbl_personnel_info AS tpi
ON tpi.id = tc.personnel_id
WHERE tpi.firstname LIKE CONCAT('%', '', '%')
AND tpi.lastname LIKE CONCAT('%', '', '%')
ORDER BY tc.date_to DESC
)
UNION ALL
(SELECT tpss.id,
tpss.personnel_id,
tpi.emp_status,
tpi.firstname,
tpi.lastname,
tpss.date_from,
tpss.date_to
FROM tbl_personnel_sea_service AS tpss
JOIN
(SELECT personnel_id,
MAX(date_to) AS Maxdatetime
FROM tbl_personnel_sea_service
GROUP BY personnel_id
) AS r
ON tpss.personnel_id = r.personnel_id
AND tpss.date_to = r.Maxdatetime
LEFT JOIN tbl_personnel_info AS tpi
ON tpi.id = tpss.personnel_id
WHERE tpi.firstname LIKE CONCAT('%', '', '%')
AND tpi.lastname LIKE CONCAT('%', '', '%')
AND agency_name = 'UMMI'
ORDER BY tpss.date_to DESC
)) AS tmain
GROUP BY personnel_id,
emp_status,
first_name,
lastname
ORDER BY date_to
I want to add data from table b in table a but unfortunately full outer join do not work in mysql . I have also tried union but it is throwing errors because my statement has group by and order by keyword
SELECT COUNT( ReviewedBy ) AS TotalReviews, OrganizationId, SUM( Rating ) AS TotalStars, COUNT( Rating ) AS TotalRatings, (
SUM( Rating ) / COUNT( Rating )
) AS AverageRating
FROM `tbl_reviews`
WHERE ReviewType = 'shopper'
AND ReviewFor = 'org'
AND OrganizationId
IN (
SELECT OrganizationId
FROM tbl_organizations
WHERE CategoryID =79
)
GROUP BY OrganizationId
ORDER BY AverageRating DESC
This is what i'm getting from the above statement
I want to get organizationId 21 data in the result but i'm not getting result because it's not present in 'tbl_review' table
click here to see the table b
How can i get Desired result ?
You don't need a FULL, but a LEFT join:
SELECT COUNT( ReviewedBy ) AS TotalReviews, o.OrganizationId,
SUM( Rating ) AS TotalStars, COUNT( Rating ) AS TotalRatings,
(SUM( Rating ) / COUNT( Rating )) AS AverageRating
FROM tbl_organizations AS o
LEFT JOIN `tbl_reviews` AS r
ON o.OrganizationId = r.OrganizationId
AND ReviewType = 'shopper' -- conditions on inner table
AND ReviewFor = 'org' -- must be moved to ON
WHERE CategoryID =79
GROUP BY o.OrganizationId
ORDER BY AverageRating DESC
Why don't you use AVG instead of SUM/COUNT?
Have you tried:
from organization
left outer join tbl_reviews
on organization.ID = tbl_reviews.organization is
for your where clause? I don't think you need a full outer join in this case... A left outer join should do
http://sqlfiddle.com/#!2/37dd94/17
If I do SELECT DISTINCT I get the same results as doing just SELECT.
On the query results, you will see two activities that contains the District "Evora".
Only one should appear.
Any clue?
How about the following query (SQL FIDDLE):
SELECT GROUP_CONCAT(APA_T.district), t.name
FROM tbl_activity AS t
JOIN tbl_activity_package AS ap ON t.id = ap.id_activity
JOIN
(
SELECT DISTINCT apa.district AS district,
(
SELECT s1.id_activity_package
FROM tbl_activity_package_address s1
WHERE apa.district = s1.district
ORDER BY s1.id DESC
LIMIT 1
) AS idActivityPackage
FROM
tbl_activity_package_address apa
ORDER BY apa.district
) AS APA_T
ON ap.id = APA_T.idActivityPackage
GROUP BY t.name
ORDER BY APA_T.district;
The above query will eliminate the extra Faro and Evora.