I want to do a SUM calculation who calls multiple times my 'Q' and QBUYERS
column, but within different rows
this is my table:
group type Q QBUYERS
GROUP1 sent 300 200
GROUP2 shown 400 100
GROUP3 sent 200 150`
And this is what I'm trying to do:
SELECT
notification_batch_id AS group,
(secondrow) QBUYERS AS PAYERS_EXPOSED,
SUM(
CASE
WHEN (firstrow)Q <> 0 AND (secondrow)Q <> 0 AND (thirdrow)Q <> 0
THEN (
(
((firstrow)QBUYERS / (firstrow)Q) -
((thirdrow)QBUYERS / (thirdrow)Q)
) / ((secondrow)Q / (firstrow)Q)
)
ELSE 0
END
) AS CVR_INCREMENTAL FROM MYTABLE
GROUP BY group;
Basically doing calculations between my numeric values and then grouping my results. But I don't know how to 'invoke' my 300, 400, 200 etc without selecting the entire column.
I also tried this:
SELECT
b.QBUYERS AS PAYERS_EXPOSED,
SUM(
CASE
WHEN a.Q <> 0 AND b.Q <> 0 AND c.Q <> 0
THEN (((a.QBUYERS / a.Q) - (c.QBUYERS / c.Q)) / (b.Q/a.Q))
ELSE 0
END
) AS CVR_INCREMENTAL
FROM (
SELECT * FROM BASE_FIRST a
UNION
SELECT * FROM BASE_SECOND b
UNION
SELECT * FROM BASE_THIRD c
) AS mytable
BASE_FIRST being:
group type Q QBUYERS
GROUP1 sent 300 200`
BASE_SECOND being:
group type Q QBUYERS
GROUP2 sent 400 100`
and so on... but it didnt work.
Is this possible to do on TERADATA ?