Hi guys
Lemme explain the situation in 2 lines:
DB Structure:
http://img133.echo.cx/img133/6610/dbstructure6hs.jpg
Client goes to computer shop. Shop has 1 or more NORMAL promotions (of the kind: for each u$sXX ammount you buy, you get X qtty of points to exchange them later by gifts). EACH NORMAL PROMOTION can have 0 or more special "sub"promotions (same kind as normal promotion, but you get more points)
For each purchase, client gets an invoice with ammount, points he got, and details about if promotion is normal or special (or both)
Later, client can exchange points by gifts...
... there's more but I think that this will be enought to explain ...
My problem is:
I was requested to report this (for all clients of actual promotion):
NAME, ACTUAL_POINTS, NORMAL_AMMOUNT, SPECIAL_AMMOUNT, TOTAL_AMMOUNT, NORMAL_POINTS, SPECIAL_POINTS, TOTAL_POINTS, EXCHANGED_POINTS
(order can vary)
*******************************
But, following SQL gives me ACTUAL_POINTS = 0 for all records...
It should give
"SUM(INVOICE.POINTS) - SUM(EXCHANGE.TOTALPOINTS)"
for each record (client)
*******************************
Please look image so you understand the error i get ...
http://img61.echo.cx/img61/8879/dbqueryproblem5it.jpg
--------------------------------------
SELECT
CLIENT.NAME AS CLIENT,
SUM(INVOICE.POINTS) - SUM(EXCHANGE.TOTALPOINTS) AS ACTUAL_POINTS,
SUM(CASE WHEN IS_SPECIAL_PROMO = TRUE THEN 0 ELSE INVOICE.AMMOUNT END) AS NORMAL_AMMOUNT,
SUM(CASE WHEN IS_SPECIAL_PROMO = TRUE THEN INVOICE.AMMOUNT ELSE 0 END) AS SPECIAL_AMMOUNT,
SUM(INVOICE.AMMOUNT) AS TOTAL_AMMOUNT,
SUM(CASE WHEN IS_SPECIAL_PROMO = TRUE THEN 0 ELSE INVOICE.POINTS END) AS NORMAL_POINTS,
SUM(CASE WHEN IS_SPECIAL_PROMO = TRUE THEN INVOICE.POINTS ELSE 0 END) AS SPECIAL_POINTS,
SUM(INVOICE.POINTS) AS TOTAL_POINTS,
SUM(EXCHANGE.TOTALPOINTS) AS EXCHANGED_POINTS
FROM
CLIENT
LEFT OUTER JOIN INVOICE ON (CLIENT.ID = INVOICE.IDCLIENT)
LEFT OUTER JOIN EXCHANGE ON (CLIENT.ID = EXCHANGE.IDCLIENT)
WHERE
(CLIENT.IDPROMO = 1) // I replace "1" by :PARAM_ID_NORMALPROMOTION
GROUP BY
CLIENT.NAME,ACTUAL_POINTS
ORDER BY
CLIENTE.NAME
--------------------------------------
I tried replacing "SUM(INVOICE.POINTS)" by 10000 to test if substraction worked, but i got 10000 for every record, and I also replaced "- SUM(EXCHANGE.TOTALPOINTS)" by 1000 but i got -1000 for every record, so it seems that both fields are taken as NULL...
Does any1 have a solution to correct this ?
Does any1 have another way to make this query ? Easily ?
thanks 4 reading and helping :rolleyes: