i got these tables in my dbpre_exer5;//mysql wampserver2.2
+-----------------------+
| Tables_in_dbpre_exer5 |
+-----------------------+
| tblcourse |
| tblcutoff |
| tblgrades |
| tblstud |
| tblsub |
+-----------------------+
now, i just want to ask how will i be able to get these outputs considering the usage of stored procedure.
the output is:
+---------+----------------+---------------+-------+
| Gender | With Failure | W/out Failure | Total |
+---------+----------------+---------------+-------+
| Male | 1 | 1 | 2 |
| Female | 2 | 1 | 3 |
+---------+----------------+---------------+-------+
by the way here is the enter code here;
**
mysql> select * from tblcourse;
+-----------+------------------------+
| course_id | course_name |
+-----------+------------------------+
| 1 | Information Technology |
| 2 | Computer Science |
+-----------+------------------------+
2 rows in set (0.00 sec)
mysql> select * from tblgrades;
+---------+--------+-------+
| stud_id | sub_id | grade |
+---------+--------+-------+
| 1 | 1 | 80 |
| 1 | 2 | 78 |
| 2 | 2 | 75 |
| 2 | 3 | 84 |
| 3 | 1 | 81 |
| 3 | 3 | 90 |
| 4 | 1 | 74 |
| 4 | 2 | 77 |
| 5 | 2 | 76 |
| 5 | 3 | 81 |
+---------+--------+-------+
10 rows in set (0.00 sec)
mysql> select * from tblcourse;
+-----------+------------------------+
| course_id | course_name |
+-----------+------------------------+
| 1 | Information Technology |
| 2 | Computer Science |
+-----------+------------------------+
2 rows in set (0.00 sec)
mysql> select * from tblcutoff;
+-----------+
| passgrade |
+-----------+
| 78 |
+-----------+
1 row in set (0.00 sec)
mysql> select * from tblgrades;
+---------+--------+-------+
| stud_id | sub_id | grade |
+---------+--------+-------+
| 1 | 1 | 80 |
| 1 | 2 | 78 |
| 2 | 2 | 75 |
| 2 | 3 | 84 |
| 3 | 1 | 81 |
| 3 | 3 | 90 |
| 4 | 1 | 74 |
| 4 | 2 | 77 |
| 5 | 2 | 76 |
| 5 | 3 | 81 |
+---------+--------+-------+
10 rows in set (0.00 sec)
mysql> select * from tblstud;
+---------+-------------------+--------+-----------+
| stud_id | stud_name | gender | course_id |
+---------+-------------------+--------+-----------+
| 1 | Angelina Jolie | F | 1 |
| 2 | Jennifer Garner | F | 1 |
| 3 | Liam Neeson | M | 2 |
| 4 | Paul Walker | M | 2 |
| 5 | Jennifer Lawrence | F | 2 |
+---------+-------------------+--------+-----------+
5 rows in set (0.00 sec)
mysql> select * from tblsub;
+--------+------------+
| sub_id | sub_name |
+--------+------------+
| 1 | Math 1 |
| 2 | English 1 |
| 3 | Filipino 1 |
+--------+------------+
3 rows in set (0.00 sec)
mysql>
**
my first problem is having the results "Male" and "Female" under gender..
any help? thanks a lot.
UPDATED
SELECT gender,
SUM(failure) `With failure`,
COUNT(*) - SUM(failure) `Without failure`,
COUNT(*) total
FROM
(
SELECT s.stud_id,
CASE WHEN s.gender = 'M' THEN 'Male' ELSE 'Female' END gender,
MAX(CASE WHEN g.grade < c.passgrade THEN 1 ELSE 0 END) failure
FROM tblgrades g JOIN tblstud s
ON g.stud_id = s.stud_id CROSS JOIN tblcutoff c
GROUP BY s.stud_id
) q
GROUP BY gender
Sample output:
| GENDER | WITH FAILURE | WITHOUT FAILURE | TOTAL |
---------------------------------------------------
| Female | 2 | 1 | 3 |
| Male | 1 | 1 | 2 |
Here is SQLFiddle demo
You can use a simple case statement:
CASE WHEN 'F' THEN SELECT 'Female' ELSE SELECT 'Male'
Further reading
Related
i have following tables,
mysql> select * from purchase_order;
+-------------------+-------------------------+-------+---------------------+
| purchase_order_id | purchase_order | cost | created_on |
+-------------------+-------------------------+-------+---------------------+
| 1 | Dell Computer 000001256 | 10000 | 2015-02-19 22:14:52 |
| 2 | HP Computer 000001256 | 50000 | 2015-02-19 22:14:52 |
+-------------------+-------------------------+-------+---------------------+
2 rows in set (0.00 sec)
mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
| 1 | 1 | 279 | 100 |
| 2 | 1 | 286 | 100 |
| 3 | 2 | 279 | 200 |
| 4 | 2 | 286 | 300 |
+--------------------------+-------------------+---------+------------------+
4 rows in set (0.00 sec)
mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on |
+-------------------+--------------------------+-------------------+---------------------+
| 1 | 1 | 50 | 2015-02-19 22:22:51 |
| 2 | 2 | 50 | 2015-02-19 22:24:59 |
| 3 | 1 | 50 | 2015-02-19 22:34:14 |
| 4 | 3 | 70 | 2015-02-20 11:11:31 |
| 5 | 4 | 150 | 2015-02-20 11:11:31 |
| 6 | 3 | 90 | 2015-02-20 11:12:20 |
| 7 | 4 | 100 | 2015-02-20 11:12:20 |
| 8 | 3 | 40 | 2015-02-20 11:12:55 |
| 9 | 4 | 50 | 2015-02-20 11:12:55 |
+-------------------+--------------------------+-------------------+---------------------+
9 rows in set (0.00 sec)
mysql> select * from stock;
+----------+-------------------+------------+----------+
| stock_id | delivery_order_id | project_id | quantity |
+----------+-------------------+------------+----------+
| 1 | 1 | 1 | 30 |
| 2 | 1 | 2 | 20 |
| 3 | 2 | 1 | 50 |
| 4 | 3 | 1 | 40 |
| 5 | 3 | 2 | 10 |
+----------+-------------------+------------+----------+
i want to fetch all purchase_order and their quantity in stock for those purchase who has item_id = 279 in it.
The Goal is to create views in which i simply pass the item_id and it fetches the list of all purchase_order in which item_id that will be input parameter and their total quantity in stock.
so, far i have write this query, i am new to mysql and views
select po.purchase_order_id, po.purchase_order from purchase_order po, purchase_order_detail pod where po.purchase_order_id = pod.purchase_order_id and pod.item_id = 279;
+-------------------+-------------------------+
| purchase_order_id | purchase_order |
+-------------------+-------------------------+
| 1 | Dell Computer 000001256 |
| 2 | HP Computer 000001256 |
+-------------------+-------------------------+
but it want some thing like this,
+-------------------+-------------------------+----------+-----------+
| purchase_order_id | purchase_order | item_id | quantity |
+-------------------+-------------------------+----------+-----------+
| 1 | Dell Computer 000001256 | 279 | 100 +
| 2 | HP Computer 000001256 | 279 | 0 +
+-------------------+-------------------------+----------+-----------+
Try this untested query:
select po.purchase_order_id, po.purchase_order, sum(s.quantity)
from purchase_order po
join purchase_order_detail pod on po.purchase_order_id = pod.purchase_order_id
join delivery_order do on do.purchase_order_id = pod.purchase_order_id
join stock s on s.delivery_order_id = do.delivery_order_id
where pod.item_id = 279
group by po.purchase_order_id, po.purchase_order;
My tables :
mysql> select * from 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 |
+-------+--------+--------+--------+------+
6 rows in set (0.00 sec)
mysql> select * from 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 |
| 4 | 2 | 6 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 6 | 4 | 10 |
+----------+-------+---------+
14 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 5 |
| 2 | Science | 1 |
| 3 | English | 6 |
| 4 | Social | 4 |
| 5 | Malayalam | 20 |
| 6 | Arts | 25 |
| 7 | Biology | 20 |
+----------+------------+--------+
7 rows in set (0.00 sec)
Question is :
Return the name(s) of the professor(s) who taught the most number of
courses in Class 10
Query i tried is :
select professor.name,works.courseid,works.empid,works.classid from professor
inner join works
on professor.empid=works.empid
where works.classid=10
group by works.courseid
I know its imcomplete/incorrect. Pls help me to the required result.
select
professor.name, count(works.courseid)
from
works
inner join
professor on
professor.empid = works.empid
where
work.classid = 10
group by
professor.name
order by count(works.courseid) desc
limit 1
change this in your select statment
works.courseid
to
count(works.courseid) as courseid
These are my tables
mysql> select * from 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 |
+-------+--------+--------+--------+------+
5 rows in set (0.00 sec)
mysql> select * from 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 |
| 4 | 2 | 6 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
+----------+-------+---------+
15 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 4 |
| 2 | Science | 4 |
| 3 | English | 85 |
| 4 | Social | 4 |
| 5 | Malayalam | 99 |
| 6 | Arts | 40 |
| 7 | Biology | 100 |
+----------+------------+--------+
7 rows in set (0.00 sec)
Question is :
Return the names of full professors who have taught at least two courses in one Class
My query is :
select professor.name from professor
inner join works
on professor.empid=works.empid
group by works.empid
having count(distinct works.courseid)>=2
The ouput I get now is :
Arun
Benoy
Chacko
Divin
Edwin
I am supposed to get the ouput as 'Edwin' as he is the only person who has taught 2 subjects in the same class. Pls help
you must consider your group by is wrong change it to this
group by classid,works.empid
like that you will count courseid in classid .
So, here is the structure:
mysql> describe tier;
+---------------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+----------------+------+-----+---------+----------------+
| ID | int(10) | NO | PRI | NULL | auto_increment |
| UP_TO | decimal(21,10) | YES | | NULL | |
+---------------------+----------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
Then the datas:
mysql> select id, up_to from tier;
+----+-----------------+
| id | up_to |
+----+-----------------+
| 1 | 1000.0000000000 |
| 2 | 2000.0000000000 |
| 3 | 3000.0000000000 |
| 4 | 500.0000000000 |
| 5 | 1000.0000000000 |
| 6 | 1500.0000000000 |
| 7 | 100.0000000000 |
| 8 | 200.0000000000 |
| 9 | 1000.0000000000 |
| 10 | 2000.0000000000 |
| 11 | 100.0000000000 |
| 12 | 200.0000000000 |
+----+-----------------+
12 rows in set (0.00 sec)
Then there's a little transformation:
mysql> SELECT id, TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM CAST(up_to AS CH
AR) )) as converted from tier;
+----+-----------+
| id | converted |
+----+-----------+
| 1 | 1000 |
| 2 | 2000 |
| 3 | 3000 |
| 4 | 500 |
| 5 | 1000 |
| 6 | 1500 |
| 7 | 100 |
| 8 | 200 |
| 9 | 1000 |
| 10 | 2000 |
| 11 | 100 |
| 12 | 200 |
+----+-----------+
12 rows in set (0.00 sec)
Fine.
Let's put that in a stored function to be handy!
DELIMITER //
CREATE FUNCTION strip_trailing_zero(I_DEC DECIMAL(10,7)) RETURNS VARCHAR(20) DETERMINISTIC
BEGIN
DECLARE strBuff VARCHAR(20);
DECLARE cnt NUMERIC(2);
DECLARE tString VARCHAR(20);
SELECT CAST(I_DEC AS CHAR) INTO tString;
SELECT LOCATE('.',tString) INTO cnt;
IF cnt > 0 THEN
SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM tString)) INTO strBuff;
ELSE
SET strBuff = tString;
END IF;
RETURN strBuff;
END//
DELIMITER ;
Cool.
GRANT EXECUTE ON FUNCTION mysql.strip_trailing_zero TO 'whatever'#'localhost';
At last, trying out my new toy now... :
mysql> select id, mysql.strip_trailing_zero(`up_to`) as converted_2 from tier;
+----+-------------+
| id | converted_2 |
+----+-------------+
| 1 | 999.9999999 |
| 2 | 999.9999999 |
| 3 | 999.9999999 |
| 4 | 500 |
| 5 | 999.9999999 |
| 6 | 999.9999999 |
| 7 | 100 |
| 8 | 200 |
| 9 | 999.9999999 |
| 10 | 999.9999999 |
| 11 | 100 |
| 12 | 200 |
+----+-------------+
12 rows in set, 7 warnings (0.02 sec)
Well, then, f*ck you too mysql!
No seriously, that's a silly joke. I'm convinced I did something wrong and there's a floating point conversion in the middle but I just can't figure it out!
Help welcome!
Thanks.
S.
edit: result after changing the input parameter type to DECIMAL(21,10):
mysql> select id, mysql.strip_trailing_zero(`up_to`) as converted_2 from tier;
+----+-------------+
| id | converted_2 |
+----+-------------+
| 1 | 1000 |
| 2 | 2000 |
| 3 | 3000 |
| 4 | 500 |
| 5 | 1000 |
| 6 | 1500 |
| 7 | 100 |
| 8 | 200 |
| 9 | 1000 |
| 10 | 2000 |
| 11 | 100 |
| 12 | 200 |
+----+-------------+
12 rows in set (0.02 sec)
Problem solved... Great! thank you!
Your UP_TO field in your table is defined as decimal(21,10), but your function takes in a decimal(10,7). I imagine that is stripping your value.
Try changing your function to accept decimal(21,10) instead.
What is this self join and why do we need this self join?. I have till date never used self joins.
See if these links helps you...
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
http://awads.net/wp/2006/07/11/back-to-basics-self-joins/
http://www.sqltutorial.org/sqlselfjoin.aspx
Good Luck!!!
there are number of reasons, and tons of examples are available on web
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
mysql> SELECT * FROM pr WHERE id>80;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 81 | 4 | 4 |
| 82 | 2 | 3 |
| 83 | 2 | 4 |
+----+------+--------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM pr WHERE id<18;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 1 | 1 | 1 |
| 5 | NULL | 2 |
+----+------+--------+
2 rows in set (0.01 sec)
identical requests :
mysql> SELECT * FROM pr AS t1 ,pr AS t2 WHERE t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)
mysql> SELECT * FROM pr AS t1 JOIN pr AS t2 ON t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)