如果另一列的条件是未满足的,则排除组

发布于 2025-02-09 21:34:58 字数 1693 浏览 1 评论 0原文

这是我正在处理的简单示例。我是通过将另一个临时表与数据库中存储的表连接在一起来创建一个临时表(a)。我有以下临时表(a)的输出:

名称日期美元
a2022-06-01$ 500
a2022-05-01$ 250
$ 250 A2022-04-01$ 100 $ 100
A2022-03-03-01$ 475
B20222-06-06-01$ 180 $ 180
b2022-05-01$ 30
B2022-04-01$ 360
.........

我的最终目标是按“美元”和“名称”组成。但是,如果该组的最早记录日期从今天的日期开始超过3个月,我只希望最终输出包括“名称”。因此,在上面的情况下,我想将“ a”包含在内,并将“美元”加在一起,但我想排除B,因为它不符合我的状况。我该怎么做?

这就是我希望我的最终输出是什么;

名称日期$
a2022-06-011375
...2022-06-01...

This is a simplified example of what I'm dealing with. I'm creating a temp table (a) by joining a different temp table with a table stored in the database. I have the following output for temp table (a):

namedatedollars
A2022-06-01$500
A2022-05-01$250
A2022-04-01$100
A2022-03-01$475
B2022-06-01$180
B2022-05-01$30
B2022-04-01$360
.........

My ultimate goal is to sum 'dollars' and group by 'name'. However, I only want my final output to include 'name' if the earliest recorded date for the group is more than 3 months from today's date. So in the case above, I would want to include 'A' and sum the 'dollars' together, but I want to exclude B because it does not meet my condition. How can I do that?

This is what I want my final output to be;

namedatedollars
A2022-06-01$1375
...2022-06-01...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2025-02-16 21:34:58

您可以使用这样的案例指令

SELECT  
    case 
        when min(dt) <= dateadd(month,-3,getdate()) then (select name FROM example where name = e.name group by name)
    else null
    end as name,
    max(dt),
    case 
        when min(dt) <= dateadd(month,-3,getdate()) then (select sum(dollars) FROM example where name = e.name group by name)
        else null
    end as dollars          
from 
    example e
group by 
    name
 

You can use the case instruction like this

SELECT  
    case 
        when min(dt) <= dateadd(month,-3,getdate()) then (select name FROM example where name = e.name group by name)
    else null
    end as name,
    max(dt),
    case 
        when min(dt) <= dateadd(month,-3,getdate()) then (select sum(dollars) FROM example where name = e.name group by name)
        else null
    end as dollars          
from 
    example e
group by 
    name
 
不及他 2025-02-16 21:34:58
create table #temp1 
(
 name char(1),
 date datetime,
 dollars int
);

insert into #temp1 (name,date,dollars) select 'A','06/01/2022',500
insert into #temp1 (name,date,dollars) select 'A','05/01/2022',250
insert into #temp1 (name,date,dollars) select 'A','04/01/2022',100
insert into #temp1 (name,date,dollars) select 'A','03/01/2022',475
insert into #temp1 (name,date,dollars) select 'B','06/01/2022',180
insert into #temp1 (name,date,dollars) select 'B','05/01/2022',30
insert into #temp1 (name,date,dollars) select 'B','04/01/2022',360

with cte
as
(
select name,min(date) as date1,GETDATE() as date2,sum(dollars) as dollars
from #temp1
group by name
having min(date)<DATEADD(month,-3,getdate()))

select name,date2 as date,dollars 
from cte 

这个可以工作吗?

create table #temp1 
(
 name char(1),
 date datetime,
 dollars int
);

insert into #temp1 (name,date,dollars) select 'A','06/01/2022',500
insert into #temp1 (name,date,dollars) select 'A','05/01/2022',250
insert into #temp1 (name,date,dollars) select 'A','04/01/2022',100
insert into #temp1 (name,date,dollars) select 'A','03/01/2022',475
insert into #temp1 (name,date,dollars) select 'B','06/01/2022',180
insert into #temp1 (name,date,dollars) select 'B','05/01/2022',30
insert into #temp1 (name,date,dollars) select 'B','04/01/2022',360

with cte
as
(
select name,min(date) as date1,GETDATE() as date2,sum(dollars) as dollars
from #temp1
group by name
having min(date)<DATEADD(month,-3,getdate()))

select name,date2 as date,dollars 
from cte 

Could this work?

喜爱纠缠 2025-02-16 21:34:58

您想报告所有几个月的每月总计,但仅针对那些带有数据恢复超过三个月的名称。

with data as (
    <your main data here>
), aggregated as (
    select name, max(date) as date, sum(dollars) as dollars,
        min(max(date)) over (partition by name) as first_date,
        count(*) over (partition by name) as month_count
    from data
    group by name
)
select name, date, dollars, first_date, month_count
from aggregated
where first_date < dateadd(month, -3, getdate());

或者

with data as (
    <your main data here>
)
select name, max(date) as date, sum(dollars) as dollars
from data d1
where exists (
    select 1 from data d2
    where d2.name = d1.name and d2.date < dateadd(month, -3, getdate())
)
group by name;

You want to report monthly totals for all months but only for those names with data going back more than three months.

with data as (
    <your main data here>
), aggregated as (
    select name, max(date) as date, sum(dollars) as dollars,
        min(max(date)) over (partition by name) as first_date,
        count(*) over (partition by name) as month_count
    from data
    group by name
)
select name, date, dollars, first_date, month_count
from aggregated
where first_date < dateadd(month, -3, getdate());

Or

with data as (
    <your main data here>
)
select name, max(date) as date, sum(dollars) as dollars
from data d1
where exists (
    select 1 from data d2
    where d2.name = d1.name and d2.date < dateadd(month, -3, getdate())
)
group by name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文