jack
jack

Reputation: 391

filtering specific column value by date in postgresql

I am trying to write a SQL query which will only filter specific column value by specific date range.In other cases it will not consider the data range.

This is my SQL query:

select ld.level_data as name,sum(dm.revenue) as value
from deal_management dm
left join lookup_data ld on ld.id = dm.pipeline_id
left join lookup_data ld1 on ld1.id=ld.parent_id
group by ld.level_data

This is the result I am getting:

name        value
-----------------
Win         190
Nurturing   200
Lost        210

What I want is only when value is Win I want to filter by date and in case of other value it will not consider the date range.Something like this:

where ld.level_data = 'Win' 
  and cast((dm.created_date) as date) between '2021-01-01' and '2021-01-31' 

How can I do it by maintaining the same structure?

Upvotes: 0

Views: 57

Answers (1)

Popeye
Popeye

Reputation: 35900

You can use boolean conditions in WHERE as follows:

WHERE (ld.level_data != 'Win' 
       OR cast((dm.created_date)as date)between '2021-01-01' and '2021-01-31' 
      )

Upvotes: 2

Related Questions