how to use where in condition with this query? like, WHERE institutions.instituteId IN ("1","2","4","15").
SELECT (SELECT avg(rating_score.score) FROM reviews INNER JOIN rating_score ON reviews.reviewsId=rating_score.reviewsId WHERE reviews.instituteId='13' AND ratingItemId='1') as 'Acadamics',
(SELECT avg(rating_score.score) FROM reviews INNER JOIN rating_score ON reviews.reviewsId=rating_score.reviewsId WHERE reviews.instituteId='13' AND ratingItemId='2') as 'Area_of_study',
(SELECT avg(rating_score.score) FROM reviews INNER JOIN rating_score ON reviews.reviewsId=rating_score.reviewsId WHERE reviews.instituteId='13' AND ratingItemId='3') as 'Campus_Facilities',
(SELECT avg(rating_score.score) FROM reviews INNER JOIN rating_score ON reviews.reviewsId=rating_score.reviewsId WHERE reviews.instituteId='13' AND ratingItemId='4') as 'Acommadatoin',
(SELECT avg(rating_score.score) FROM reviews INNER JOIN rating_score ON reviews.reviewsId=rating_score.reviewsId WHERE reviews.instituteId='13' AND ratingItemId='5') as 'Sports_Facilities',
institutions.instituteId, institutions.instituteName, institutions.location, countries.countryName, institutions.siteAddress, institutions.overallRatings FROM institutions INNER JOIN countries ON institutions.countryId=countries.countryId
WHERE institutions.instituteId='13'
This is similar to the solutions in need to return two sets of data with two different where clauses but uses AVG instead of SUM. The main difference is that you don't use ELSE 0 in the CASE, because you don't want to average 0 for all the rows with a different raingItemId. AVG() will just ignore those null rows.
SELECT
AVG(CASE WHEN ratingItemId='1' THEN rs.score END) AS Academics,
AVG(CASE WHEN ratingItemId='2' THEN rs.score END) AS Area_of_study,
AVG(CASE WHEN ratingItemId='3' THEN rs.score END) AS Campus_Facilities,
AVG(CASE WHEN ratingItemId='4' THEN rs.score END) AS Acommadatoin,
AVG(CASE WHEN ratingItemId='5' THEN rs.score END) AS Sports_Facilities,
i.instituteName, i.location, c.countryName, i.siteAddress, i.overallRatings
FROM institutions AS i
INNER JOIN countries AS c ON i.countryId=c.countryId
LEFT JOIN reviews AS r ON r.instituteId = i.instituteId
LEFT JOIN rating_score AS rs ON rs.reviewsId = r.reviewsId
WHERE i.instituteId IN (1, 2, 4, 15)
GROUP BY i.instituteId
Related
I am trying to fetch the value of available stock in MYSQL but when the number of rows gets more than 35000 request is failing with no response. I use subqueries to fetch the value of the available stock.
Here is my code:
SELECT
p.`product-id` AS id,
p.`product-id` AS product_id,
ped.purchase_entry_id AS purchase_id,
p.`product-name` AS name,
m.name AS manufacturer,
p.`product-type` AS product_type,
ped.id AS batch_no,
ped.internal_sales_rate AS barcode,
c.name as category,
ped.size AS size,
IFNULL(ped.sales_rate,0) AS sales_rate,
IFNULL(ped.purchase_rate,0) AS purchase_rate,
IFNULL(SUM(ped.units),0) AS units_purchased,
(select IFNULL(SUM(sed.qty),0) FROM sales_entry_details sed WHERE (sed.batch_no = ped.id)) AS units_sold,
(select IFNULL(SUM(sr.qty),0) FROM sales_return_item sr WHERE (sr.batch_no = ped.id)) AS retured,
(select IFNULL(SUM(pri.qty),0) FROM purchase_return_items pri WHERE (pri.batch_no = ped.id)) AS purchase_return,
(select IFNULL(SUM(ast.qty),0) FROM adjustment_stock ast WHERE (ast.batch_no = ped.id)) AS adjustment,
(select IFNULL(SUM(ast2.batch_no),0) FROM adjustment_stock ast2 WHERE (ast2.batch_no = ped.id)) AS isChecked
FROM purchase_entry_details ped
LEFT JOIN products p on p.`product-id` = ped.product_id
LEFT JOIN category c ON c.id = p.`product-type`
LEFT JOIN manufacturer m ON m.id = p.manufacturer
GROUP BY ped.id;
Is there any better option to fetch the value of stock available in SQL??
The query looks fine. As there is no limiting criteria (no WHERE clause), you'll read the whole table sequentially.
You'd want indexes on batch_no in the involved tables of course. But I guess these are foreign keys to some batch table, so the indexes should already exist.
The only thing that I notice is that you select from adjustment_stock twice. You can avoid this by moving the subqueries to your FROM clause:
SELECT
p.`product-id` AS id,
p.`product-id` AS product_id,
ped.purchase_entry_id AS purchase_id,
p.`product-name` AS name,
m.name AS manufacturer,
p.`product-type` AS product_type,
ped.id AS batch_no,
ped.internal_sales_rate AS barcode,
c.name as category,
ped.size AS size,
IFNULL(ped.sales_rate,0) AS sales_rate,
IFNULL(ped.purchase_rate,0) AS purchase_rate,
IFNULL(SUM(ped.units),0) AS units_purchased,
IFNULL(sed.sum_qty),0) AS units_sold,
IFNULL(sr.sum_qty),0) AS retured,
IFNULL(pri.sum_qty),0) AS retured,
IFNULL(ast.sum_qty),0) AS adjustment,
IFNULL(ast.sum_batch_no),0) AS isChecked
FROM purchase_entry_details ped
LEFT JOIN products p on p.`product-id` = ped.product_id
LEFT JOIN category c ON c.id = p.`product-type`
LEFT JOIN manufacturer m ON m.id = p.manufacturer
LEFT JOIN
(
select batch_no, sum(qty) as sum_qty from sales_entry_details group by batch_no
) sed on sed.batch_no = ped.id
LEFT JOIN
(
select batch_no, sum(qty) as sum_qty from sales_return_item group by batch_no
) sr on sr.batch_no = ped.id
LEFT JOIN
(
select batch_no, sum(qty) as sum_qty from purchase_return_items group by batch_no
) pri on pri.batch_no = ped.id
LEFT JOIN
(
select batch_no, sum(qty) as sum_qty, sum(batch_no) as sum_batch_no
from adjustment_stock group by batch_no
) ast on ast.batch_no = ped.id
GROUP BY ped.id;
(Adding up batch numbers seems a bit weird, though.)
You can make separate joins for each table used in subqueries against purchase_entry_details table. In all these separate selects make group by ped.id. Afterwards make left join to new tables with sums.
SELECT
...
SED.sumqty,
SR.sumqty,
...
FROM purchase_entry_details ped
LEFT JOIN products p on p.`product-id` = ped.product_id
LEFT JOIN category c ON c.id = p.`product-type`
LEFT JOIN manufacturer m ON m.id = p.manufacturer
LEFT JOIN (
SELECT ped1.id, SUM(sed1.qty) as sumqty
FROM purchase_entry_details ped1
JOIN sales_entry_details sed1 ON sed1.batch_no = ped1.id
GROUP BY ped1.id
) SED ON SED.id = ped.id
LEFT JOIN (
SELECT ped1.id, SUM(sr1.qty) as sumqty
FROM purchase_entry_details ped1
JOIN sales_return_item sr1 ON sr1.batch_no = ped1.id
GROUP BY ped1.id
) SR ON SR.id = ped.id
...
I have a list of technicians and their number of patients, when I click on a technician I get the list of patients and their details.
To do this, I have query that Returns the number of patients per technician and another returns the records of the patients.
SELECT *, SUM(Rcount) as Number_of_patients
FROM
(
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM pecs
INNER JOIN users ON pecs.techniciens_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
UNION ALL
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM followup
INNER JOIN users ON followup.technician_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
)x
GROUP BY users_id ORDER BY last_name ASC
the result is:
users_id | name | Number_of_patients
40 | ABABAB | 223
that is 223 patients for technician_ID = 40
Now to view a list of the patients for this technician I have the following query:
SELECT *
FROM
(
SELECT patients.patients_id, patients.name
FROM pecs
LEFT JOIN users ON pecs.techniciens_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE pecs.techniciens_id = 40
#GROUP BY patients_id
UNION ALL
SELECT patients.patients_id, patients.name
FROM followup
LEFT JOIN users ON followup.technician_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
LEFT JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE followup.technician_id = 40
#GROUP BY patients_id
)x
GROUP BY patients_id ORDER BY last_name ASC
Now, I get the same number of records (223) but there are duplicates rows of patients ...I need help on how to get the correct number of patients for each technician without duplicates.
Can anyone please help?
I am not sure I understood exactly your question. Anyway, I would try to keep things as simple as possibile. Regarding your "count" query, you could start from something like this, where I removed filed I think should not be useful for counting. This query should give you count avoiding multiple id (I use DISTINCT in the two select and UNION to remove possible duplicate between the two select). May be some join could be eliminated (but I don't know the whole structure). Please follow Tim B. suggestion about formatting and care in posting a question.
SELECT USERS_ID, COUNT(*) AS PATIENTS_COUNT
FROM (
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM PECS
INNER JOIN USERS ON PECS.TECHNICIENS_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
UNION
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM FOLLOWUP
INNER JOIN USERS ON FOLLOWUP.TECHNICIAN_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PECS ON PECS.PECS_ID = FOLLOWUP.PECS_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
) A
This mysql query gives me the number of sales of my products (total and total_staff) grouped by days and hours of the day. I want the sum of total and total_staff for each product (not grouped by anything, expect by id). Can I do this in the same query?
SELECT p.name, p.color, DATE(date_create) as jour,HOUR(date_create) AS h,
SUM(CASE WHEN s.staff=0 THEN 1 ELSE 0 END) as total,
SUM(CASE WHEN s.staff=1 THEN 1 ELSE 0 END) as total_staff
FROM manifsSubCategories msc
LEFT JOIN products_subCategories psc ON msc.id=psc.manifSubCategory_id
LEFT JOIN sales s ON psc.product_id=s.product_id
LEFT JOIN products p ON psc.product_id=p.id
LEFT JOIN manifsCategories ON manifCategory_id=manifsCategories.id
WHERE manifCategory_id=1 AND s.category_id=1 GROUP BY jour,h ORDER BY p.id DESC
Just omit the other columns :
SELECT p.name,
SUM(CASE WHEN s.staff=0 THEN 1 ELSE 0 END) as total,
SUM(CASE WHEN s.staff=1 THEN 1 ELSE 0 END) as total_staff
FROM manifsSubCategories msc
LEFT JOIN products_subCategories psc ON msc.id=psc.manifSubCategory_id
LEFT JOIN sales s ON psc.product_id=s.product_id
LEFT JOIN products p ON psc.product_id=p.id
LEFT JOIN manifsCategories ON manifCategory_id=manifsCategories.id
WHERE manifCategory_id=1 AND s.category_id=1
GROUP BY p.name
ORDER BY p.id DESC
BTW - Your WHERE clause is wrong. You are filtering by a table for the LEFT JOIN , this will turn your join into an inner join due to NULL comparison. Conditions on the right table of a left join should only be placed inside the ON clause.
I have the following tables:
comitees [ID, Name, Total_Amount, Total_Members, Starting_Date, Ending_Date]
committee_group [Id, Name, Total_Members]
Group_Member [Id, Group_Id, Member_Name, Contribution]
I want to select Name from committee_group Where sum of Contribution from Group_Member is equal to Total_Amount from comitees
This is what I have so far
SELECT committee_group.*, Sum(Case When group_member.Group_Id='1'
THEN group_member.Contribution
ELSE 0 END)
FROM committee_group
LEFT JOIN group_member ON group_member.Id = committee_group.Id
LEFT join comitees ON group_member.Contribution = comitees.Total_Amount
This should work,
SELECT a.id,a.Name,a.Total_Members,c.Total_Amount
FROM committee_Group a
INNER JOIN (SELECT Name,sum(Contribution) as 'Sum_Con'
FROM Group_Member
GROUP BY Name)b on a.Id=b.Id
INNER JOIN comitees c on c.Total_Amount=b.Sum_Con
and b.Name=c.Name
I need to query for the users with highest amount of sales by all projects, where the users are in users table, sales in units table, projects in projects table.
Projects Top Agent Total Sales for Project
Project A User A 100000
Project B User B 20000
Project C User A 1000
Project D - 0
The Projects column is list all the projects regardless it has sales or not.
The Top Agent column is list the user with the highest sales in the project.
The Total Sales for Project is the total sales for a projects.
The agent column i got is incorrect because there is someone else has the highest sales, the query seems to return the first row of the result
SELECT projects, pid, CASE WHEN agent is null THEN '-' ELSE agent END as agent,
CASE WHEN FORMAT(topagent,0) > 0 THEN FORMAT(topagent,0) ELSE 0 END as salesvolume
FROM (
SELECT projects.name as projects, projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent,
SUM(units.price) AS topagent
FROM users inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid
union
select projects.name as projects, projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent,
SUM(units.price) AS topagent
from projects left outer join types on projects.id = types.project_id
left outer join units on types.id = units.types_id and units.status = 'Sold'
left outer join bookings on units.id = bookings.unit and units.status = 'Sold'
left outer join users on bookings.agent_id = users.id and units.status = 'Sold'
group by pid
) a
GROUP BY pid
order by topagent desc
Try it if helps you-
SELECT a.prjname, IFNULL(usr.name,'-') AS Top_Agent, SUM(a.sale) AS Total_Sales_for_Project
FROM
(
SELECT prj.id AS prjid,prj.name AS prjname,usr.id,usr.name AS usrname,IFNULL(SUM(unit.price),0) AS sale
FROM projects AS prj
LEFT JOIN `types` AS typ ON typ.project_id=prj.id
LEFT JOIN units AS unt ON unt.type_id=typ.id AND unt.status='sold'
LEFT JOIN bookings bkg ON bkg.unit=unt.id
LEFT JOIN users usr ON usr.id=bkg.agent_it
GROUP BY prj.id,usr.id
ORDER BY prj.id,usr.id,sale DESC
) a
GROUP BY a.prjid
Your column aliases are confusing to read. In English, it seems what you mean by topagent is "sum of sales by a human". But in SQL, your GROUP BY pid means that the SUM(units.price) really means "sum of sales in a project".
Then the UNION adds a list of projects to a list of users. The agent names are basically random at this point.
If I decipher the requirements as "a list of projects ranked by the sales values of each project's top sales agent", then you'd have SQL as below:
SELECT
pid,
projects.name as project_name,
IFNULL(a.top_agent_name,'-') as top_agent_name,
CASE WHEN FORMAT(top_agent_sales,0) > 0 THEN FORMAT(top_agent_sales,0) ELSE 0 END as top_agent_salesvolume
FROM
projects
JOIN
SELECT
a.pid,
a.agent_name as top_agent_name,
a.agent_sales as top_agent_sales
FROM
(SELECT
projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent_name,
SUM(units.price) AS agent_sales
FROM users
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid, users.id
) a # get all agents for all projects
JOIN
(SELECT
MAX(agent_sales) as max_project_agent_sales
FROM
(SELECT
projects.id as pid,
SUM(units.price) AS agent_sales
FROM users
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid, users.id
)
GROUP BY pid) b ON a.pid = b.pid
WHERE
a.agent_sales = b.max_project_agent_sales
ORDER BY a.agent_sales desc
Old answer below:
There are 2 topagents for each pid in the inner query since it's a union of 2 group bys. There isn't a reducing function in the outer group by pid so the topagent returned in the select is the first one that came up in the inner query.