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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.