Skip to content
Advertisement

SQL Query with date range Male Female

I have a query that works but I need to incorporate in that query the following:

For male date is older or equals 3 months; For female date is older or equals 4 months;

SELECT * 
FROM Davaoci
WHERE DatumPoslednjegDavanja >= DATEADD(month, -3, GETDATE())
  AND KrvnaGrupa = 'APos' 
ORDER BY DatumPoslednjegDavanja DESC

Advertisement

Answer

Use a CASE statement:

SELECT * FROM Davaoci
WHERE DatumPoslednjegDavanja >= DATEADD(
  month, 
  CASE WHEN Pol = 'M' THEN -3 ELSE -4 END, 
  GETDATE()
)
AND KrvnaGrupa = 'APos' 
ORDER BY DatumPoslednjegDavanja DESC
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement