Jump to content

Recommended Posts

Hi all I could use some help with this query i am working on. I have a transaction table with the following structure.

 

TransactionID - int(11), Primary

Date - datetime

ClientID - int(11)

PaymentType - varchar(50)

Reference - varchar(200)

AmountUSD - decimal(8,2)

AmountGBP - decimal(8,2)

Amount - decimal(10,2)

Currency - varchar(4)

 

I am trying to get a monthly total of all transactions not marked as an expense or target and then attach that month's target to the result which is marked with 'Target' in the PaymentType. aka Month, Sum(`GBPAmount`), `Target`

 

Here is my query so far. I think i am nearly there but its doing my head in.

SELECT date( T1.Date ) AS Date,
sum( T1.AmountGBP ) AS Sales

FROM transactions AS T1
INNER JOIN transactions AS T2 
ON month( T1.Date ) = month( T2.Date )

WHERE T1.PaymentType NOT IN ('Target', 'Expense')
AND T2.PaymentType = 'Target'

GROUP BY month( T1.Date )

 

its valid but doesnt return any results. If i remove the "AND T2.PaymentType = 'Target'" part i get a huge sum from joining every record to each other. I think i may need a subquery rather than a join but it seeemed overkill initially. Any help even if its just a pointer would be most appreciated :)

 

Edit: MySQL client version: 5.1.41

Link to comment
https://forums.phpfreaks.com/topic/197937-help-with-my-crazy-query/
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.