Jump to content

grouping by month issue


joel24

Recommended Posts

I'm having some trouble grouping by month

 

I have a database with payments to contractors which are attributed a start and finish as a unix timestamp, an hourly rate and a confirmed column of true or false depending on whether the contractor finished their work etc

 

payments

contractorID, paymentID, startTime(unix), finishTime(unix), rate(decimal(5,2), confirmed(tinyInt(1))

 

contractors

contractorID, contractorName, address, phone, mobile etc etc

 

Now i'm creating a report and need to group the payments by month, displaying the monthly payment amount where the 'pay' column is true, I can do this but as I am grouping by contractorID then month if any of the confirmed columns in that month for that contractor are false then no values are displayed as it is grouping by month...

 

This is the sql statement I've been mucking around with

SELECT
c.contractorName, c.contractorID,
DATE_FORMAT(from_unixtime(startTime), '%M %Y') AS month,
IF(confirmed=1,round(sum((endTime-startTime)/60/60),2),0) AS hoursWorked,
IF(confirmed=1,round(sum((endTime-startTime)/60/60) * rate,2),0) AS pay

FROM payments p 
JOIN contractors c ON c.contractorID = p.contractorID

GROUP BY p.contractorID, month(from_unixtime(start));

 

In short

IF(confirmed=1,round(sum((endTime-startTime)/60/60) * rate,2),0) AS pay

is not working if any 'confirmed' values for that month are false

 

 

Link to comment
https://forums.phpfreaks.com/topic/220299-grouping-by-month-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.