简体   繁体   English

在 postgresql 中按日期过滤特定列值

[英]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.我正在尝试编写一个 SQL 查询,它只会按特定日期范围过滤特定列值。在其他情况下,它不会考虑数据范围。

This is my SQL query:这是我的 SQL 查询:

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:我想要的是只有当值是 Win 我想按日期过滤时,如果是其他值,它不会考虑日期范围。像这样:

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?我怎样才能通过保持相同的结构来做到这一点?

You can use boolean conditions in WHERE as follows:您可以在WHERE中使用 boolean 条件,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM