Question : Please help me write this mySQL query.

I am trying to calculate profit for a specific Month (and year).  I need to write a query that calculates the total profit for a particular month and user.  I have successfully managed to print out all the separate profits (as PROFIT) in the first query.  BUT, when I sum the profits (second query) it gives me a value way to high.  I don't know what to group the values by to get the SUM to give the correct profit.

Please help.

Individual Profits Listed Query:
1:
2:
3:
4:
5:
6:
7:
SELECT (CREDITS.CRED_VALUE_PER_CREDIT-PACKAGE_DEFAULTS.PACDEF_TUTOR_HOURLY_PAY/LESSON_TYPES.LESTY_BILLING_PERSONS) *CREDITS.CRED_NUM_OF_CREDITS AS PROFIT,
 YEAR(CREDITS.CRED_PURCHASE_DATE), MONTH(CREDITS.CRED_PURCHASE_DATE), CRED_ID

FROM CREDITS INNER JOIN CUSTOMERS ON CREDITS.CRED_CUS_ID = CUSTOMERS.CUS_ID INNER JOIN LESSON_TYPES ON CREDITS.CRED_LESTY_ID = LESSON_TYPES.LESTY_ID INNER JOIN PACKAGE_DEFAULTS ON LESSON_TYPES.LESTY_ID = PACKAGE_DEFAULTS.PACDEF_LESTY_ID 

WHERE MONTH(CREDITS.CRED_PURCHASE_DATE) = '02' AND YEAR(CREDITS.CRED_PURCHASE_DATE) = '2010' AND CUSTOMERS.CUS_USER_ID = '14' AND CREDITS.CRED_DEACTIVE = 0 AND CREDITS.CRED_ORDER_STATUS = 4 
GROUP BY PACDEF_TUTOR_HOURLY_PAY, CREDITS.CRED_VALUE_PER_CREDIT


Incorrect Sum Profit Query:
1:
2:
3:
4:
5:
6:
7:
SELECT SUM((CREDITS.CRED_VALUE_PER_CREDIT-PACKAGE_DEFAULTS.PACDEF_TUTOR_HOURLY_PAY/LESSON_TYPES.LESTY_BILLING_PERSONS) *CREDITS.CRED_NUM_OF_CREDITS) AS PROFIT,
 YEAR(CREDITS.CRED_PURCHASE_DATE), MONTH(CREDITS.CRED_PURCHASE_DATE), CRED_ID

FROM CREDITS INNER JOIN CUSTOMERS ON CREDITS.CRED_CUS_ID = CUSTOMERS.CUS_ID INNER JOIN LESSON_TYPES ON CREDITS.CRED_LESTY_ID = LESSON_TYPES.LESTY_ID INNER JOIN PACKAGE_DEFAULTS ON LESSON_TYPES.LESTY_ID = PACKAGE_DEFAULTS.PACDEF_LESTY_ID 

WHERE MONTH(CREDITS.CRED_PURCHASE_DATE) = '02' AND YEAR(CREDITS.CRED_PURCHASE_DATE) = '2010' AND CUSTOMERS.CUS_USER_ID = '14' AND CREDITS.CRED_DEACTIVE = 0 AND CREDITS.CRED_ORDER_STATUS = 4 
GROUP BY MONTH(CREDITS.CRED_PURCHASE_DATE)

Answer : Please help me write this mySQL query.

Yeah...if they were normal controls then the approach you originally posted (and Paul_Harris_Fusion tried to extend) would have worked.

Unfortunately, though, the ShapeControls are an oddity!...  =O
Random Solutions  
 
programming4us programming4us