SQl Statement Sum - mysql

Hey guys I am trying to sum these tables and I can't figure out what I'm doing wrong
My first table is:
Reserve
ReserveID MembershipID PlayerCount Time CourseID
My second table is
Courses
CourseID Name
My sql statement that I am trying is here:
Select Sum(Reserve.Player_Count)Total
From Reserve
Left Join Courses
On Courses.CourseID = Reserve.ReserveID
Where Time = "2012-04-09 07:10:00"
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")

You seem to mixing alias. Avoid them
Select Sum(Reserve.Player_Count)Total
From Reserve
Left Join Courses
On Courses.CourseID = Reserve.ReserveID
Where Reserve.Time = "2012-04-09 07:10:00"
-- ^ Right here
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")

Related

SQL Count number of people that do not hold a qualification

hoping you can help me as I am going round in circles!
I need to count the number of people that do not hold a certain qualification.
I have tried NOT but this returns all the other qualifications other than the one I wish to count. Whereas I want to specifically count the number of people that do not hold the qualification, i.e. do not have a row entry in the table.
SELECT COUNT(tmp.personnelID) AS Number,
tmp.QualificationID
FROM(SELECT pa.PersonnelID,
pq.QualificationID
FROM dbo.PersonnelActive pa
LEFT JOIN dbo.PersonnelQualifications pq ON pq.PersonnelID = pa.PersonnelID
WHERE NOT pq.QualificationID = 125) tmp
GROUP BY tmp.QualificationID
Any help you guys can give or point me in the right direction would be greatly appreciated.
You should count for personnelID that are not in the personnelID with a QualificationID = 125
SELECT COUNT(pa.personnelID) AS Num,
pq.QualificationID
FROM PersonnelActive pa
INNER JOIN dbo.PersonnelQualifications pq ON pq.PersonnelID = pa.PersonnelID
where pa.PersonnelID NOT IN ( SELECT pa.PersonnelID
FROM dbo.PersonnelActive pa
INNER JOIN dbo.PersonnelQualifications pq ON pq.PersonnelID = pa.PersonnelID
WHERE pq.QualificationID = 125
)
GROUP BY pq.QualificationID

SQL counting different column values

I have a table called spider_status which has a status_code and a locationid. I am retrieving all cities from region_select_city which I joined in the code below. Now I am trying to count all the unique values in status_code in the table spider_status grouping by cities.
SELECT region_select_city.name as stad , COUNT(spider_status.id) as spider_aantal,
CASE WHEN spider_status.status_code = 1 THEN COUNT(spider_status.status_code) ELSE 0 END as LIVE
FROM
spider_status
JOIN
location
ON spider_status.location_id = location.id
JOIN
location_select_addressid
ON location.id = location_select_addressid.locationid
JOIN
location_address
ON location_select_addressid.addressid = location_address.id
JOIN
region_select_city
ON location_address.city_id = region_select_city.id
JOIN
spider_status_code
ON spider_status.status_code = spider_status_code.id
WHERE spider_status.current = 1
GROUP BY region_select_city.name
Hope the following code help you to solve the issue
SELECT count(DISTINCT s.status_code)
FROM spider_status AS s
JOIN location AS l
ON l.locationid = s.locationid
JOIN location_address AS loc_add
ON loca_add.locationid = loc_add.locationid
GROUP BY loc_add.city_id
At the end of the 2nd last line, should the AND be there?

Count if a user has reached the borrwing limit

I've setup a fiddle with tables and data here
I'm trying to write a single sql to check if user has reached the borrowing limit for each category.
Right now, it's done using severals sql statements called after each other.
But the way it goes is simple.
memId and id come through a querystring.
$medId = $_POST['memId']; Using 1 for this example. This is the members Id.
$id = $_POST['id']; Using 4 for this example. This is the item being lent.
After that I do:
select id, holder from collection_db where id = 4 // We have a valid item
select borrowMax from collection_db where id = (holder from the previous select) and category = 10 //Result = 2. Category indicates its a label and not a borrowable item.
select count(borrowedId) from lendings where memId = 1 and holder = (holder from the 1st query) //He's borrowed 2, under 1, so cant borrow any more. User 2 may borrow however.
if (count => borrowMax) {echo 'Cannot borrow more.';} else {echo 'Added to'}
How can this be combined into a single sql or is it best left this way?
This seems to produce a correct result set:
SELECT col1.id, col1.holder, col2.borrowMax, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder
WHERE col1.id = $id
AND col2.category = 10
AND lend.memId = $medId
I think this combines the queries:
select max(c.borrowMax) as BorrowMax, COUNT(*)
from collection_db c join
collection_db c1
on c.id = c1.holder and c1.id = 4 and c.category = 10 join
lendings l
on l.holder = c1.holder;
It does make an assumption that the join between c and c1 does not produce duplicate rows. But you have this requirement by using = in the original query (rather than join).

Check if combination of fields exist

I have a query that displays the modules that are available for a student to enrol on and shows them in a list.. I would like it if the modules that student has enrolled on do not show in the list. Once a student has enrolled on a module it is stored in a table called tbl_studentModule which has two fields, regNum ($studentID) and moduleID.
What do I need to add to this query to only show the modules that are not already in the tbl_studentModule with the relevant regNum.
EG I would lik the query to say something to the effect where the combination of regNum and moduleID do not already exist if tbl_studentModule.
Here is what I have so far...
SELECT DISTINCT am.moduleID, m.moduleName, am.type, s.regNum, s.award
FROM tbl_awardModules am
LEFT OUTER JOIN tbl_module m ON am.moduleID = m.moduleID
INNER JOIN tbl_awardLevels al ON am.awardLevelID = al.awardLevelID
INNER JOIN tbl_award a ON al.awardID = a.awardID
INNER JOIN tbl_student s ON a.awardID = s.award
LEFT JOIN tbl_studentModules sm ON s.regNum = sm.regNum
WHERE am.type <> 'C' AND sm.regNum = '$student_id' AND m.moduleName <> 'A_null_Choice' AND m.moduleName NOT LIKE '%Not Running%'
Hope that makes sense...
Ok, I sussed it..
I added
AND NOT EXISTS (SELECT * FROM tbl_studentModules WHERE regNum = '$student_id' AND moduleID = am.moduleID)
to the end if the query so now it only shows the rows of the table that do not already contain that combination of values

Issues with a MySQL JOIN statement

i have 5 tables called personal,dailypay,bonuses,iou and loans am trying to write a query that will generate payroll from this table's...my code is
select personal.name as NAME,
(sum(dailypay.pay) + bonuses) - (iou.amount + loans.monthly_due)) as SALARY
from personal
join dailypay on personal.eid = dailypay.eid
left join bonuses on personal.eid = bonuses.eid
left join iou on personal.eid = iou.eid
left join where dailypay.date = 'specified_date'
and bonuses.date_approved = 'specified_date'
and iou.date_approved = 'specified_date'
and loans.date = month(now()
It returns the name and null salary values for staffs that does have records for either bonuses,iou and loans. But i want to sum their dailypay, deduct/add deductions or additions return the values, in the event of no record it should proceed with the summation without any deduction or subtraction.
You missed something when pasting the code, as there is no join for the loans table. Also, you are using the table bonuses as a value, you need a field name also. I added some code for the join and for the field, but used ??? for names that are unknown to me.
When you add or subtract a null value to something else, the result is null, that's why you get null as result when any of the values from the left-joined tables are missing. You can use ifnull(..., 0) to turn a null value into zero.
You need a group by clause, otherwise it would sum up the salary for all persons.
If I get you right, you have several records in the dailypay table for each user, but only one record per user in the other tables? In that case you have the problem that you will be joining the other tables against each row in the dailypay, so if you have 20 payment records for a user, it will count the bonus 20 times. You can use an aggregate like max to get the value only once.
You have put conditions for the left.joined tables in the where clause, but this will turn the joins into inner joins. You should have those conditions in each join clause.
select
personal.name as NAME,
(sum(dailypay.pay) + ifnull(max(bonuses.???), 0)) - (ifnull(max(iou.amount), 0) + ifnull(max(loans.monthly_due), 0)) as SALARY
from
personal
inner join dailypay on personal.eid = dailypay.eid
left join bonuses on personal.eid = bonuses.eid and bonuses.date_approved = 'specified_date'
left join iou on personal.eid = iou.eid and iou.date_approved = 'specified_date'
left join loans on personal.??? = loans.??? and loans.date = month(now())
where
dailypay.date = 'specified_date'
group by
personal.name
There seems to be an extranous left join before the where and a missing closing bracket ) in month(now()
so it should look like:
select personal.name as NAME,
(sum(dailypay.pay) + bonuses) - (iou.amount + loans.monthly_due)) as SALARY
from personal
join dailypay on personal.eid = dailypay.eid
left join bonuses on personal.eid = bonuses.eid
left join iou on personal.eid = iou.eid
where dailypay.date = 'specified_date'
and bonuses.date_approved = 'specified_date'
and iou.date_approved = 'specified_date'
and loans.date = month(now())