0

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?

1 Answer 1

2

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' 
      )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.