Related
I'm struggling with what is a complicated SQL query for me, although I believe that it is not particularly complicated. I'm close to the right answer, but not quite there yet.
My database represents a criminal abstract. I have three tables in my database (I've simplified my schema enormously for the purposes of this question): arrest, arrestcharges, and dispositions.
Each defendant can have multiple arrests (defendant table not included for simplification). Each arrest can have multiple charges, which are in the arrestcharges table. And each charge has a grade and is associated with a disposition (guilty, not guilty, etc...). The dispositions are categorized so that 0=some form of guilt disposition, 1=a non-guilty disposition.
I want to find individuals who have been convicted of a charge graded as "M1" on more than one case. If an individual has been convicted of more than M1, but they are in the same case, that person shouldn't be returned (unless they have another case with an M1 conviction).
A sqlfiddle link and the SQL to create and populate the table is below.
I believe that this query should work, but it doesn't:
select a.defendantid, count(a.id)
FROM `arrest` AS a LEFT JOIN `arrestcharges` AS ac
ON a.id=ac.arrestid LEFT JOIN `dispositions` AS d
ON ac.dispositionid=d.id
WHERE d.dispocategory=0 AND ac.grade="M1"
GROUP BY a.id HAVING COUNT(a.id) > 1 ORDER BY a.defendantid;
Based on the sql below, I expect that defendant IDs 1 and 5 should be returned since they are the only two defendants with an M1 conviction in more than one arrest. But the actual response I am getting is 2 and 5. 2 should not be returned b/c defendant 2 only has one arrest in the database.
Any thoughts on what I am doing wrong?
SQLFiddle
CREATE TABLE IF NOT EXISTS `arrest` (
`id` int(6) unsigned NOT NULL,
`defendantid` int(6) unsigned NOT NULL,
`docketno` varchar(21) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `arrestcharges` (
`id` int(6) unsigned NOT NULL,
`arrestid` int(6) unsigned NOT NULL,
`grade` varchar(2) NOT NULL,
`dispositionid` int(6) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `dispositions` (
`id` int(6) unsigned NOT NULL,
`disposition` varchar(30) NOT NULL,
`dispoCategory` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `arrest` (`id`, `defendantid`, `docketno`) VALUES
('1', '1', 'MC-51-CR-0000222-1999'),
('2', '1', 'MC-51-CR-0000223-1999'),
('3', '1', 'MC-51-CR-0000224-1999'),
('4', '2', 'MC-51-CR-0002343-2000'),
('5', '3', 'MC-51-CR-0002349-2000'),
('6', '3', 'MC-51-CR-0002350-2000'),
('7', '3', 'MC-51-CR-0002351-2010'),
('8', '3', 'MC-51-CR-0002352-2013'),
('9', '4', 'MC-51-CR-1209293-2011'),
('10', '5', 'MC-51-CR-2389848-1999'),
('11', '5', 'MC-51-CR-3893923-1999'),
('12', '5', 'MC-51-CR-2393912-1999');
INSERT INTO `dispositions` (`id`, `disposition`, `dispoCategory`) VALUES
('1', 'Guilty', '0'),
('2', 'Not Guilty', '1'),
('3', 'Guilty Plea', '0'),
('4', 'Dismissed', '1');
INSERT INTO `arrestcharges` (`id`, `arrestid`, `grade`, `dispositionid`) VALUES
('1', '1', 'M1', '1'),
('2', '1', 'M', '2'),
('3', '2', 'F', '2'),
('4', '2', 'M1', '3'),
('5', '3', 'M1', '1'),
('6', '4', 'M2', '4'),
('7', '4', 'M1', '3'),
('8', '4', 'M1', '3'),
('9', '4', 'M1', '1'),
('10', '5', 'M1', '2'),
('11', '6', 'M1', '2'),
('12', '7', 'F2', '1'),
('13', '8', 'F3', '1'),
('14', '9', 'M1', '2'),
('15', '9', 'M1', '2'),
('16', '9', 'M1', '2'),
('17', '9', 'M1', '2'),
('18', '10', 'M1', '1'),
('19', '10', 'M1', '1'),
('20', '11', 'M2', '3'),
('21', '12', 'M1', '4'),
('22', '12', 'M1', '3');
Try this query:
select a.defendantid, count(distinct(ac.arrestid)) as count
FROM `arrest` AS a LEFT JOIN `arrestcharges` AS ac
ON a.id=ac.arrestid LEFT JOIN `dispositions` AS d
ON ac.dispositionid=d.id
WHERE d.dispocategory=0 AND ac.grade="M1"
GROUP BY a.defendantid HAVING count>1;
You should count the distinct number as distinct_count of rows you need and make use of having filter such as having distinct_count>1. This way you can ensure that the count are not getting repeated.
You appear to be aggregating by the wrong column. You need a.defendantid in the group by:
SELECT a.defendantid, count(*)
FROM `arrest` a JOIN
`arrestcharges` ac
ON a.id = ac.arrestid JOIN
`dispositions` d
ON ac.dispositionid = d.id
WHERE d.dispocategory = 0 AND ac.grade = 'M1'
GROUP BY a.defendantid
HAVING COUNT(DISTINCT a.id) > 1
ORDER BY a.defendantid;
Note that I also changed the outer joins to inner joins. If charges and dispositions are not available, then your filtering conditions cannot be met. Hence, the appropriate join is an inner join.
I have three tables in my database - staff, salary, shift.
Shift table is linked to the staff table by the staff_id, and salary by the salary_band_id which is also in staff table.
I'm trying to calculate staffs wage by doing (Shift.Total_Hours * Salary.Hourly_Wage)
Here is my query:
SELECT Staff.First_Name, Staff.Last_Name, Shift.Staff_ID, Shift.Total_Hours, Sa.Hourly_Wage,
SUM(Shift.Total_Hours * Sa.Hourly_Wage) Total
FROM Shift, Salary Sa, Staff
INNER JOIN Salary ON Staff.Salary_Band_ID = Salary.Salary_Band_ID
WHERE Shift.Staff_ID = Staff.Staff_ID
GROUP by Staff_ID
ORDER BY Total;
Tables:
CREATE TABLE Shift (
Staff_ID INT(5) NOT NULL,
Date DATE NOT NULL,
Time_Started TIME NOT NULL,
Time_Ended TIME NOT NULL,
Total_Hours DOUBLE(4,2) NOT NULL,
Confirmed ENUM('y','n') NOT NULL
)
INSERT INTO Shift
(Staff_ID, Date, Time_Started, Time_Ended, Total_Hours, Confirmed)
VALUES
('1', '2012-06-08', '9:00', '16:45', '7.75', 'y'),
('3', '2012-06-18', '13:10', '17:30', '4.33', 'y'),
('6', '2012-10-14', '11:15', '16:45', '5.5', 'y'),
('4', '2012-10-30', '10:00', '17:00', '7', 'n'),
('5', '2013-07-15', '9:10', '17:20', '8.1', 'y'),
('10', '2013-08-10', '9:00', '17:00', '8', 'n'),
('8', '2013-10-05', '10:15', '17:30', '7.25', 'y'),
('7', '2013-11-06', '9:20', '17:00', '7.66', 'y'),
('2', '2014-03-25', '9:00', '16:45', '7.75', 'n'),
('9', '2014-04-11', '9:00', '17:00', '8', 'n'),
('11', '2014-06-05', '9:00', '17:00', '8', 'y');
CREATE TABLE Salary (
Salary_Band_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Position VARCHAR(40) NOT NULL,
Hourly_Wage DOUBLE(4,2) NOT NULL
)
INSERT INTO Salary
(Position, Hourly_Wage)
VALUES
('Worker', '6'),
('Worker Manager', '8'),
('General Manager', '10.22'),
('Manager', '13.45'),
('Owner', '25'),
('Brewer', '7.76'),
('Shop Assistant', '6.12');
CREATE TABLE Staff (
Staff_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Salary_Band_ID INT(5) NOT NULL,
First_Name VARCHAR(25) NOT NULL,
Last_Name VARCHAR(25) NOT NULL,
Part_Full_Time ENUM('f','p') NOT NULL,
DOB DATE NOT NULL,
Start_Date DATE NOT NULL,
End_Date DATE,
Address_ID INT(7) NOT NULL,
Contact_Number VARCHAR(12) NOT NULL,
Availability_ID INT(5) NOT NULL,
Brewery_ID INT(4),
Shop_ID INT(4)
)
INSERT INTO Staff
(Salary_Band_ID, First_Name, Last_Name, Part_Full_Time, DOB, Start_Date, Address_ID, Contact_Number, Availability_ID, Brewery_ID, Shop_ID)
VALUES
('1', 'Jenny', 'Bolick', 'p', '1966-08-15', '2005-06-06', '1', '+447700900969', '1', '1', '1'),
('2', 'Margit', 'Eves', 'f', '1968-01-13', '2005-10-06', '2', '07700900711', '1', '1', '2'),
('3', 'Ramonita', 'Layton', 'f', '1975-11-28', '2005-09-29', '3', '07700900336', '1', '1', '1'),
('4', 'Kattie', 'Speck', 'p', '1977-10-24', '2006-09-07', '6', '07700900615', '1', '1', '2'),
('5', 'Christa', 'Denk', 'p', '1968-03-25', '2008-04-11', '5', '07700900542', '2', '2', '4'),
('6', 'Francene', 'Scholl', 'p', '1991-08-05', '2009-05-06', '12', '07700900763', '2', '1', '4'),
('7', 'Patti', 'Tomaszewski', 'f', '1974-01-22', '2010-03-11', '11', '07700900125', '3', '2', '6'),
('7', 'Pandora', 'Laplant', 'p', '1982-02-14', '2011-10-03', '11', '+447700900118', '2', '3', '5'),
('1', 'Branden', 'Bermejo', 'f', '1985-08-15', '2012-04-18', '10', '07700900687', '4', '3', '6'),
('1', 'Lanell', 'Delao', 'p', '1987-12-24', '2012-04-20', '9', '07700900057', '5', '3', '2'),
('1', 'Floria', 'Vandermark', 'f', '1991-03-13', '2013-11-13', '6', '07700900396', '6', '4', '2');
The results all look OK, except the Totals are way off! Also it seems to only pick one salary_band.. £6.60/hour Can anyone see what I'm doing wrong?
I think this is what you are looking for, use explicit JOIN instead of implicit.
SELECT
st.First_Name,
st.Last_Name,
sh.Staff_ID,
sh.Total_Hours,
sa.Hourly_Wage,
SUM(sh.Total_Hours * sa.Hourly_Wage) Total
from Staff st
inner join Salary sa on sa.Salary_Band_ID = st.Salary_Band_ID
inner join Shift sh on sh.Staff_ID = st.Staff_ID
GROUP by st.Staff_ID
ORDER BY Total
desc
;
DEMO
This should be relatively simple, but I haven't been able to figure it out.
I have a query that currently creates a table that looks like:
Teacher, course, # students in course
John Doe, Algebra 1, 3
John Doe, Algebra 2, 1
Jeff Doh, Geometry, 2
I want to also count the number of students being taught by each teacher, giving results like:
Teacher, course, # students in course, # students with teacher
John Doe, Algebra 1, 3, 4
John Doe, Algebra 2, 1, 4
Jeff Doh, Geometry, 2, 2
But I can't figure out how to produce the last column that sums all of the students being taught by a teacher across all courses.
Here's my current query (I'd also be interested in a better way to write this existing query)
SELECT
u.username AS 'teacher',
c.fullname AS 'course',
(SELECT COUNT(u1.username)
FROM prefix_user u1
JOIN prefix_user_enrolments ue1 on ue1.userid=u1.id
JOIN prefix_enrol e1 ON e1.id=ue1.enrolid
JOIN prefix_course c1 on c1.id = e1.courseid
JOIN prefix_context AS ctx1 ON ctx1.instanceid = c1.id
JOIN prefix_role_assignments AS ra1 ON ra1.contextid = ctx1.id
JOIN prefix_course_categories AS cc1 ON cc1.id=c1.category
WHERE ra1.roleid="5" ### "5" = student
AND ra1.userid=u1.id
AND e1.courseid=c1.id
AND c1.id=c.id) AS '# students in course'
FROM prefix_user u
JOIN prefix_user_enrolments ue on ue.userid=u.id
JOIN prefix_enrol e ON e.id=ue.enrolid
JOIN prefix_course c on c.id = e.courseid
JOIN prefix_context AS ctx ON ctx.instanceid = c.id
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_course_categories AS cc ON cc.id=c.category
WHERE ra.roleid="3" ### "3" = Teacher
GROUP BY c.id
ORDER BY cc.name, c.fullname
I wish I could just add a SUM(# students in course) column, but that doesn't work. And the interface I'm using won't let me use WITH ROLLUP.
My Schema:
CREATE TABLE prefix_user
(`id` varchar(2), `username` varchar(11))
;
INSERT INTO prefix_user
(`id`, `username`)
VALUES
('1', 'JohnDoe'),
('2', 'JaneDuh'),
('3', 'JeffDoh'),
('4', 'JackSprat'),
('5', 'WillieWinky'),
('6', 'DonaldDuck'),
('7', 'MickeyMouse')
;
CREATE TABLE prefix_user_enrolments
(`id` varchar(2), `enrolid` varchar (4), `userid` varchar(1), `status` varchar(1))
;
INSERT INTO prefix_user_enrolments
(`id`, `enrolid`, `userid`, `status`)
VALUES
('10', '1000', '1', '0'),
('11', '1001', '2', '0'),
('12', '2000', '3', '0'),
('13', '1002', '4', '0'),
('14', '2001', '5', '0'),
('15', '1003', '6', '0'),
('16', '2002', '7', '0'),
('17', '3000', '1', '0'),
('18', '3001', '7', '0')
;
CREATE TABLE prefix_enrol
(`id` varchar(4), `status` varchar (1), `courseid` varchar(3), `roleid` varchar(1))
;
INSERT INTO prefix_enrol
(`id`, `status`, `courseid`, `roleid`)
VALUES
('1000', '0', '100', '5'),
('1001', '0', '100', '5'),
('2000', '0', '200', '5'),
('1002', '0', '100', '5'),
('2001', '0', '200', '5'),
('1003', '0', '100', '3'),
('2002', '0', '200', '3'),
('3000', '0', '300', '3'),
('3001', '0', '300', '5')
;
CREATE TABLE prefix_course
(`id` varchar(3), `fullname` varchar(8), `category` varchar(2))
;
INSERT INTO prefix_course
(`id`, `fullname`, `category`)
VALUES
('100', 'Algebra1', '10'),
('200', 'Geometry', '10'),
('300', 'Algebra2', '10')
;
CREATE TABLE prefix_context
(`id` varchar(5), `instanceid` varchar(8))
;
INSERT INTO prefix_context
(`id`, `instanceid`)
VALUES
('10000', '100'),
('10001', '100'),
('20000', '200'),
('10002', '100'),
('20001', '200'),
('10003', '100'),
('20002', '200'),
('30000', '300'),
('30001', '300')
;
CREATE TABLE prefix_role_assignments
(`id` varchar(6), `roleid` varchar(1), `contextid` varchar(5), `userid` varchar(1))
;
INSERT INTO prefix_role_assignments
(`id`, `roleid`, `contextid`, `userid`)
VALUES
('100000', '5', '10000', '1'),
('100001', '5', '10001', '2'),
('200000', '5', '20000', '3'),
('100002', '5', '10002', '4'),
('200001', '5', '20001', '5'),
('100003', '3', '10003', '6'),
('200002', '3', '20002', '7'),
('300000', '3', '30000', '1'),
('300001', '5', '30001', '7')
;
CREATE TABLE prefix_role
(`id` varchar(1), `shortname` varchar(7))
;
INSERT INTO prefix_role
(`id`, `shortname`)
VALUES
('5', 'student'),
('3', 'teacher')
;
CREATE TABLE prefix_course_categories
(`id` varchar(2), `name` varchar(4))
;
INSERT INTO prefix_course_categories
(`id`, `name`)
VALUES
('10', 'math')
;
It looks like your original query may be in error. I believe JohnDoe is enrolled in Algebra 1 as a student not a teacher. I also noticed that the role is held on two tables: prefix_enrol and prefix_role_assignments - not sure if I am misinterpreting something or this is redundant data.
In my attempt below I used sub-queries to count the enrollments by courseID where the role of the enrolled is a student and count the students per teacher. There may be a more efficient way to do this, but this is what I cam up with on my first attempt:
SELECT
t.username AS 'teacher'
,c.fullname AS 'course'
,studentCountCourse.numStudents AS '# students in course'
,studentCountTeach.numStudents AS '# students with teacher'
FROM prefix_enrol AS e
INNER JOIN prefix_course AS c ON c.id = e.courseid
INNER JOIN prefix_user_enrolments AS ue ON ue.enrolid = e.id
INNER JOIN prefix_user AS t ON t.id = ue.userID AND e.roleid = 3
INNER JOIN (
SELECT
e1.courseid
,count(e1.courseid) AS numStudents
FROM prefix_enrol AS e1
WHERE e1.roleid = 5
GROUP BY e1.courseid) AS studentCountCourse ON studentCountCourse.courseid = c.id
INNER JOIN (
SELECT
t1.id
,count(t1.id) AS numStudents
FROM prefix_user AS t1
INNER JOIN prefix_user_enrolments AS ue ON ue.userid = t1.id
INNER JOIN prefix_enrol AS e ON e.id = ue.enrolid
INNER JOIN prefix_enrol AS e2 ON e2.courseid = e.courseid AND e2.roleid = 5
INNER JOIN prefix_course AS c ON c.id = e.courseid
WHERE e.roleid = 3
GROUP BY t1.id) AS studentCountTeach ON studentCountTeach.id = t.id
ORDER BY e.courseid;
If I am misunderstanding your schema please let me know by explaining what each of the tables is used for.
Hello I'm working on a system for primary school and im stuck on queries in database part. I would like to retrieve:
Number of students below the average score
Average mark for each student across all tests
Average mark for each test
I'm not really good at queries and I couldn't work it out. I appreciate any help. Thanks
Below you can find my tables and test data
Tables:
CREATE TABLE Student
(
StudentID INT auto_increment,
ClassID VARCHAR(10),
FirstName VARCHAR(25),
LastName VARCHAR(25),
DateOfBirth DATE,
Gender VARCHAR(10),
PRIMARY KEY (StudentID, ClassID)
);
create table Subjects
(
SubjectID INT,
SubjectName VARCHAR(25),
PRIMARY KEY (SubjectID)
);
create table Grade
(
StudentID INT,
SubjectID INT,
ClassID VARCHAR(10),
Grade DECIMAL(5,1),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (SubjectID) REFERENCES Subjects(SubjectID),
FOREIGN KEY (ClassID) REFERENCES Student(ClassID)
);
Test data:
INSERT INTO Student (StudentID, ClassID, FirstName, LastName, DateOfBirth, Gender)
VALUES ('', '01', 'John', 'Smith', '15/01/2000', 'Male'),
('', '01', 'Michael', 'Black', '15/03/2000', 'Male'),
('', '01', 'Dennis', 'White', '07/08/1999', 'Male'),
('', '01', 'Emy', 'Blue', '10/04/2000', 'Female'),
('', '01', 'Joe', 'Yellow', '09/05/2000', 'Male'),
('', '01', 'Aaren', 'Jackson', '09/009/1999', 'Male'),
('', '01', 'Marta', 'Harris', '30/01/2000', 'Female'),
('', '01', 'Laura', 'Lewis', '09/01/2000', 'Female'),
('', '01', 'Michael', 'Jackson', '01/01/2000', 'Male'),
('', '02', 'Piotr', 'Kowalski', '15/01/2000', 'Male'),
('', '02', 'Harris', 'Bialy', '15/03/2000', 'Male'),
('', '02', 'Porter', 'Czarny', '07/08/1999', 'Male'),
('', '02', 'Maciek', 'Blue', '10/04/2000', 'Female'),
('', '02', 'Mateusz', 'Yellow', '09/05/2000', 'Male'),
('', '02', 'Aaren', 'Jackson', '09/009/1999', 'Male'),
('', '02', 'Marta', 'Harris', '30/01/2000', 'Female'),
('', '02', 'Laura', 'Lewis', '09/01/2000', 'Female'),
('', '02', 'Chaytan', 'Jackson', '01/01/2000', 'Male');
INSERT INTO Subjects (SubjectID, SubjectName)
VALUES ('01', 'English'),
('02', 'Mathematics'),
('03', 'Science'),
('04', 'Geography'),
('05', 'IT'),
('06', 'History');
INSERT INTO Grade (StudentID, SubjectID, ClassID, Grade)
VALUES ('1', '2', '01', '60.5'),
('2', '2', '01', '70.0'),
('3', '2', '01', '40.0'),
('4', '2', '01', '33.5'),
('5', '2', '01', '90.0'),
('6', '2', '01', '77.5'),
('7', '2', '01', '89.0'),
('8', '2', '01', '74.0'),
('9', '2', '01', '66.5'),
('10', '2', '02', '30.5'),
('11', '2', '02', '50.0'),
('12', '2', '02', '30.0'),
('13', '2', '02', '73.5'),
('14', '2', '02', '70.0'),
('15', '2', '02', '57.5'),
('16', '2', '02', '69.0'),
('17', '2', '02', '34.0'),
('18', '2', '02', '76.5'),
('1', '1', '01', '65.5'),
('2', '1', '01', '73.0'),
('3', '1', '01', '41.0'),
('4', '1', '01', '39.5'),
('5', '1', '01', '96.0'),
('6', '1', '01', '70.5'),
('7', '1', '01', '80.0'),
('8', '1', '01', '74.0'),
('9', '1', '01', '64.5'),
('10', '1', '02', '55.5'),
('11', '1', '02', '43.0'),
('12', '1', '02', '61.0'),
('13', '1', '02', '49.5'),
('14', '1', '02', '76.0'),
('15', '1', '02', '80.5'),
('16', '1', '02', '99.0'),
('17', '1', '02', '100.0'),
('18', '1', '02', '55.5');
Here you go, you have all your homework in one fiddle send it to your teacher :D fiddle
SELECT Student.StudentID, Student.FirstName, Student.LastName, Grade.Grade, Subjects.SubjectName
FROM Student INNER JOIN Grade ON Grade.StudentID=Student.StudentID
INNER JOIN Subjects ON Grade.SubjectID=Subjects.SubjectID
WHERE Grade.Grade<50;
SELECT Student.StudentID, Student.FirstName, Student.LastName, AVG(Grade.Grade) AS Average
FROM Student INNER JOIN Grade ON Grade.StudentID=Student.StudentID
GROUP BY Grade.StudentID;
SELECT Subjects.SubjectID, Subjects.SubjectName, AVG(Grade.Grade) AS Average
FROM Subjects INNER JOIN Grade ON Grade.SubjectID=Subjects.SubjectID
GROUP BY Grade.SubjectID;
Here would be my first tries :
SELECT COUNT(*) FROM Grade WHERE Grade <= AVG(Grade);
SELECT AVG(Grade) FROM Grade GROUP BY StudentID;
SELECT AVG(Grade) FROM Grade GROUP BY SubjectID;
Some adaptations to your specific case might be necessary. By the way, next time your use Stack Overflow, post your attempts. Efforts are greatly appreciated here, and will help you improve beyond this particular case. Plus, copy/paste won't make you capable after graduation.
About GROUP BY : https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html, and AVG : http://www.tutorialspoint.com/mysql/mysql-avg-function.htm.
How can I rank the users below based on points .I really appreciate any help.
Thanks in Advance .
http://sqlfiddle.com/#!2/374db/11
CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment ,
sender varchar(255),
receiver varchar(255),
msg varchar(255),
date timestamp,
points varchar(255),
refno varchar(255),
PRIMARY KEY (id)
);
CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment ,
sno varchar(255),
name varchar(255),
PRIMARY KEY (id)
);
CREATE TABLE if not exists tblC
(
id int(11) NOT NULL auto_increment ,
data varchar(255),
refno varchar(255),
extrarefno varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblA (sender, receiver,msg,date,points,refno ) VALUES
('1', '2', 'buzz ...','2011-08-21 14:11:09','10','001'),
('1', '2', 'test ...','2011-08-21 14:12:19','20','002'),
('4', '2', 'test ...','2011-08-21 14:13:19','30','003'),
('1', '3', 'buzz ...','2011-08-21 14:11:09','10','004'),
('1', '3', 'test ...','2011-08-21 14:12:19','20','005'),
('1', '4', 'buzz ...','2011-08-21 14:11:09','10','006'),
('1', '4', 'test ...','2011-08-21 14:12:19','20','007'),
('3', '4', 'test ...','2011-08-21 14:13:19','20','008'),
('2', '4', 'test ...','2011-08-21 14:13:19','20','009');
INSERT INTO tblB (sno, name ) VALUES
('1', 'Aa'),
('2', 'Bb'),
('3', 'Cc'),
('4', 'Dd'),
('5', 'Ee'),
('6', 'Ff'),
('7', 'Gg'),
('8', 'Hh');
INSERT INTO tblC (data,refno,extrarefno ) VALUES
('data1', '001', '101'),
('data2', '002', '102'),
('data3', '003', '103'),
('data4', '004', '101'),
('data5', '005', '102'),
('data6', '006', '103'),
('data7', '007', '101'),
('data8', '008', '101'),
('data9', '009', '101');
sql
SELECT *
FROM (SELECT tblA.receiver, MAX(tblA.id) AS id
FROM tblA
GROUP BY tblA.receiver
) subset JOIN
tblA
ON subset.receiver = tblA.receiver AND subset.id = tblA.id JOIN
tblB
on tblA.receiver = tblB.sno join
tblC
ON tblA.refno=tblC.refno ;
You can order the results of a query using ORDER BY. So, you can rank the rows in tblA as follows:
SELECT *
FROM tblA
ORDER BY points
However, I notice you are using varchar for the points column. If this is to hold a numerical value you probably want int or float.