I am firing a query in mysql but not getting desired output.
this is the code:
select team_name,
sum(semis.points+final.points) as final_points
from semis
inner join final on semis.sid=final.sid
inner join teams on teams.tid=semis.tid
group by semis.tid
union
select team_name,
semis.Points
from semis
inner join teams on semis.tid=teams.tid
left join final on semis.sid=final.sid
where final.sid is null;
OUTPUT:
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 7 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 11 |
| Gujarati | 10 |
| English | 10 |
| Economics | 20 |
| BCOM | 3 |
| Chemistry | 3 |
| English | 3 |
+-----------------------+--------------+
and the result i want to fetch
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 10 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 14 |
| Gujarati | 10 |
| English | 13 |
| Economics | 20 |
+-----------------------+--------------+
Adding last 3 values to english,bcom,chemistry increasing it by 3 and making a total of BCOM: 14, Chemistry:10 , English: 13
From the sample data you posted and the expected results it looks like you can do it without UNION, by left joining final and with coalesce() for final.points:
select team_name, sum(semis.points + coalesce(final.points, 0)) as final_points
from semis
inner join teams on teams.tid=semis.tid
left join final on semis.sid=final.sid
group by semis.tid
Related
I have this query:
SELECT c.`id`, w.`qty`, COUNT(c.`id`) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`
I have these tables:
`control` c `warehouse` w
+----+--------+------+----------+ +------+-------+
| id | pieces | code | location | | id | qty |
+----+--------+------+----------+ +------+-------+
| 112| 112-1 | 40 | london | | 112 | 3 |
| 112| 112-2 | 40 | london | | 113 | 3 |
| 112| 112-3 | 40 | london | | 114 | 1 |
| 113| 113-1 | 40 | italy | | 115 | 1 |
| 113| 113-2 | 40 | italy | +--------------+
| 113| 113-3 | 40 | italy |
| 114| 114-1 | 41 | france |
| 115| 115-1 | 41 | france |
| 112| 112-1 | 40 | germany |
| 112| 112-2 | 40 | germany |
| 112| 112-3 | 40 | germany |
| 113| 112-1 | 40 | russia |
| 113| 112-2 | 40 | russia |
| 113| 112-3 | 40 | russia |
| 112| 112-1 | 40 | poland |
| 112| 112-2 | 40 | poland |
| 112| 112-3 | 40 | poland |
+-------------------------------+
Im getting this:
actual output
+-----+-----+--------+----------+
| id | qty | pieces | location |
+-----+-----+--------+----------+
| 112 | 3 | 9 | poland |
| 113 | 3 | 6 | russia |
+-------------------------------+
I'm trying to get this result:
desired output
+-----+-----+--------+----------+
| id | qty | pieces | location |
+-----+-----+--------+----------+
| 112 | 3 | 3 | london |
| 113 | 3 | 3 | italy |
| 112 | 3 | 3 | germany |
| 113 | 3 | 3 | russia |
| 112 | 3 | 3 | poland |
+-------------------------------+
Is possible this result? maybe tweaking my query?
I tried without GROUP BY but in that case i just get 1 row totalizing pieces.
If you want to separate the different locations to different rows, you need to add that column to the group by clause:
SELECT c.`id`, w.`qty`, COUNT(c.`id`) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`, c.`location`
-- Here ---------^
I suspect that you just need to add qty and location to the group by clause:
SELECT c.`id`, w.`qty`, COUNT(*) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`, w.`qty`, c.`location`
Starting MySQL 5.7, it is mandatory to list all-non aggregated columns in the group by clause (unless you change default sql option ONLY_FULL_GROUP_BY); most other databases also implement this constraint. I would recommend getting used to it...
Side notes:
COUNT(c.id) is better written COUNT(*), since id seems like a not nullable column
generally spearking, you shoud avoid using backticks around table and column names unless when absolutly necessary.
These are the tables:
professor:
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
works:
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
+----------+-------+---------+
course:
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 100 |
| 2 | Science | 80 |
| 3 | English | 85 |
| 4 | Social | 90 |
| 5 | Malayalam | 99 |
| 6 | Arts | 40 |
+----------+------------+--------+
The question is :
Return list of employees who have taught course Maths or Science but
not both
The query which I wrote is :
select distinct professor.name from professor
inner join works
on professor.empid=works.empid
where works.courseid in
(select courseid from course where coursename ='Maths' or coursename='Science');
The output I received is:
Arun
Benoy
Chacko
Here the employee 'Arun' shouldnt have been displayed as he as taught both Maths and Science.
Please help me out !!
You may use an aggregate COUNT() to check that the total number of DISTINCT courses taught is exactly 1, while still filtering to the two different types of courses. That ensures that only one, never both, is returned.
Because the IN () limits all rows initially returned only to the two desired courses, professors can have a maximum of 2 possible different courses via COUNT(DISTINCT coursename). A HAVING clause then prohibits those with 2 from the final result set.
SELECT
DISTINCT professor.name
FROM
professor
INNER JOIN works ON professor.empid = works.empid
/* Join against course to get the course names */
INNER JOIN course ON works.courseid = course.courseid
WHERE
/* Restrict only to Maths, Science */
course.coursename IN ('Maths', 'Science')
GROUP BY professor.name
/* Only those with exactly one type of course */
HAVING COUNT(DISTINCT course.coursename) = 1
Here is a demonstration: http://sqlfiddle.com/#!2/2e9610/2
You want to use an xor here instead of an or.
select distinct professor.name from professor
inner join works
on professor.empid=works.empid
where works.courseid in
(select courseid from course where coursename ='Maths' xor coursename='Science');
These are the tables:
professor
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
works
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
+----------+-------+---------+
The above are the tables from which I need to retrieve the data from.
The question is to return list of Employees who take both Class 10 and
Class 9.
The query I have written is:
select professor.name
from inner join works
on professor.empid=works.empid
where works.classid=9 and works.classid=10;
I know that the result I want is Arun, but I don't know what should be the exact query to retrieve the required result.
He wants the professeors that take class 9 AND 10. So there are 2 different records in works that need to match.
select professor.name from professor
join works A on A.empid=professor.empid and A.classid=9
join works B on B.empid=professor.empid and B.classid=10
See http://sqlfiddle.com/#!2/4be88a/1
Try This
select professor.name
from professor inner join works
on professor.empid=works.empid
where works.classid=9 OR works.classid=10;
(OR)
select professor.name
from professor inner join works
on professor.empid=works.empid
where works.classid IN ('9','10')
SELECT prof.name FROM professor AS prof
JOIN works
ON prof.empid = works.empid
WHERE works.classid IN (9, 10);
structure:
tbl 1
|car_id(PK)| make | model | year |
-----------------------------------
| 1 | Toyot | Camry | 1999 |
| 2 | Honda | Civic | 2005 |
tbl 2
|img_id(PK)| car_id| img_link |
------------------------------------
| 1 | 1 | tcamry1.jpeg |
| 2 | 1 | tcamry2.jpeg |
| 3 | 1 | tcamry3.jpeg |
| 4 | 2 | hcivic1.jpeg |
| 5 | 2 | hcivic2.jpeg |
My query:
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE 1
Result:
|img_id(PK)| car_id| make | model | year | img_link |
-----------------------------------------------------------
| 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg |
| 2 | 1 | Toyot | Camry | 1999 | tcamry2.jpeg |
| 3 | 1 | Toyot | Camry | 1999 | tcamry3.jpeg |
| 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg |
| 5 | 2 | Honda | Civic | 2005 | hcivic2.jpeg |
I need to get 1 row for each car and have WHERE clause with something like lowest img_id value out of all img_id related to the same car.
Result I want:
|img_id(PK)| car_id| make | model | year | img_link |
-----------------------------------------------------------
| 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg |
| 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg |
Thank you.
UPDATE:
I need something along these lines :-/
SELECT g.id, c.car_id, c.mc_make, c.mc_model, c.mc_year, c.mc_desc
FROM mycars c
INNER JOIN (SELECT * FROM mycars_gallery g WHERE )
ON c.car_id=g.car_id
WHERE g.id = min(g.id)
Try:
SELECT MIN(b.img_id), a.car_id, a.make, a.model, a.year, b.img_link
FROM cars a
LEFT JOIN imgs b ON a.car_id = b.car_id
GROUP BY a.car_id, a.make, a.model, a.year ;
Demo: http://sqlfiddle.com/#!2/1469f/15
Hope this helps.
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE img_id IN(SELECT MIN(img_id)) GROUP BY model
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE 1
GROUP BY g.img_link;
try this.. not sure though.
I have three existing SQL tables we will call "teams", "miles", and "riders". Leaving out the fluff, their structure looks like this:
Table: teams
------------+-------------+---------+
| team_name | captains_id | team_id |
------------+-------------+---------+
| superbads | 11 | 1 |
| superflys | 12 | 2 |
------------+-------------+---------+
Table: riders
--------------+-----------+----------+
| rider_name | team_id | rider_id |
--------------+-----------+----------+
| donatello | 1 | 10 |
| leonardo | 1 | 11 |
| michelangelo| 2 | 12 |
| raphael | 2 | 13 |
--------------+-----------+----------+
Table: miles
--------------+-----------+----------+
| rider_id | miles | id |
--------------+-----------+----------+
| 10 | 100 | 1 |
| 10 | 62 | 2 |
| 11 | 110 | 3 |
| 11 | 100 | 4 |
| 12 | 8 | 5 |
| 12 | 22 | 6 |
| 13 | 29 | 7 |
| 13 | 2 | 8 |
--------------+-----------+----------+
I need to return a list of teams with total miles generated by that team (I also need to return the team captain's name, but that's a bit easier).
The difficulty is that I need to join miles on riders, sum the "miles" field, and then join that on teams somehow.
Changing the table structure is pretty much out, as this is an existing application. This is a LAMP environment, so manipulating PHP arrays after the query is an option if needed.
This should do it:
select t.team_id, t.team_name, t.captains_id, sum(m.miles) as total_miles
from teams t
inner join riders r on r.team_id = t.team_id
inner join miles m on m.rider_id = r.rider_id
group by t.team_id, t.team_name, t.captains_id