I'm having trouble doing a SQL query.
To give some context, i have a data extension populated with automated file import. In this table i have every purchase customer did on certain products.
I need to get from this DE only the people who did buy more then 47 € on the "Gamme blanche" during the last 6 months AND buy LESS then 56 € on all the other products during the last 6 months.
Here is what my query looks like (but doesnt give me the results i want)
SELECT
'LAST 6 MONTH' AS PERIOD,
o.MKT_RANGE,
o.YN_MRQ_CNS_BUYER,
o.CS_BP_ID,
SUM(o.MRQ_CNS_RNG_TURNO) AS CA_CUMUL
FROM
"OFFERS by customer and range" AS o
WHERE
CAST(CONCAT(o.YEAR,'/',o.MONTH,'/','30') as date) >= DATEADD(MONTH, DATEDIFF(MONTH,0, GETDATE())-6,0) AND
o.MKT_RANGE = 'GAMME BLANCHE'
GROUP BY
o.CS_BP_ID,
o.MKT_RANGE,
o.YN_MRQ_CNS_BUYER
HAVING SUM(o.MRQ_CNS_RNG_TURNO) > 47
UNION
SELECT
'LAST 6 MONTH' AS PERIOD,
o.MKT_RANGE,
o.YN_MRQ_CNS_BUYER,
o.CS_BP_ID,
SUM(o.MRQ_CNS_RNG_TURNO) AS CA_CUMUL
FROM
"OFFERS by customer and range" AS o
WHERE
CAST(CONCAT(o.YEAR,'/',o.MONTH,'/','30') as date) >= DATEADD(MONTH, DATEDIFF(MONTH,0, GETDATE())-6,0) AND
o.MKT_RANGE <> 'GAMME BLANCHE'
GROUP BY
o.CS_BP_ID,
o.MKT_RANGE,
o.YN_MRQ_CNS_BUYER
HAVING SUM(o.MRQ_CNS_RNG_TURNO) < 56
I know that union isn't the good thing to use, but i also cannot use join/inner join because i have two contradictory conditions.
If anyone could help me solving this, it would be greatly appreciated.