SQL Server中的条件语句
我正在尝试根据代理的角色检索代理的集合。代理可以有两个角色,即催收员和团队负责人。在这种情况下,我只需要检索团队领导的行 尝试使用case语句。但是没有运气SQL Server中的条件语句,sql,sql-server,Sql,Sql Server,我正在尝试根据代理的角色检索代理的集合。代理可以有两个角色,即催收员和团队负责人。在这种情况下,我只需要检索团队领导的行 尝试使用case语句。但是没有运气 select commission_date, agent_id, agent, team, role, target, collection, commission, final_payout from ambank.dbo.commissions where commission_date =
select
commission_date, agent_id, agent, team, role, target, collection,
commission, final_payout
from
ambank.dbo.commissions
where
commission_date = '2019-06-30'
and (target > 0 or final_payout > 0)
and agent_id in (4446, 2267)
order by
agent
当前结果:
com_date agent_id agent team role target collection commission final_payout
2019-06-30 4446 AIZAT Team A Collector 130000 100 0 0
2019-06-30 4446 AIZAT Team B Collector 130000 18767.68 0 0
2019-06-30 2267 ERIC Team C Collector 130000 34200 0 0
2019-06-30 2267 ERIC Team Lead Non-collector 650000 209789.99 0 0
预期结果:
com_date agent_id agent team role target collection commission final_payout
2019-06-30 4446 AIZAT Team A Collector 130000 100 0 0
2019-06-30 4446 AIZAT Team B Collector 130000 18767.68 0 0
2019-06-30 2267 ERIC Team Lead Non-collector 650000 209789.99 0 0
我想你需要一个窗口功能
select *
from (
select commission_date, agent_id, agent, team, [role], [target], [collection], commission, final_payout
, row_number() over(partition by agent_id order by case when team = 'Team Lead' then 1 else 0 end desc) row#
from ambank.dbo.commissions
where commission_date = '2019-06-30' and ([target] > 0 or final_payout > 0)
and agent_id in (4446,2267)
) x
where row# = 1
order by agent;
我想你需要一个窗口功能
select *
from (
select commission_date, agent_id, agent, team, [role], [target], [collection], commission, final_payout
, row_number() over(partition by agent_id order by case when team = 'Team Lead' then 1 else 0 end desc) row#
from ambank.dbo.commissions
where commission_date = '2019-06-30' and ([target] > 0 or final_payout > 0)
and agent_id in (4446,2267)
) x
where row# = 1
order by agent;
我认为这是一个优先级查询。选择团队领导(如果有),如果没有,则选择所有行 如果是:
select c.*
from ambank.dbo.commissions c
where commission_date = '2019-06-30' and
(target > 0 or final_payout > 0) and
agent_id in (4446, 2267) and
(c.team = 'Team Lead' or
not exists (select 1
from ambank.dbo.commissions c2
where c2.agent_id = c.agent_id and
c2.commission_date = c.commission_date and
(c2.target > 0 or c2.final_payout > 0) and
c2.team <> 'Team Lead'
)
)
order by agent ;
我认为这是一个优先级查询。选择团队领导(如果有),如果没有,则选择所有行 如果是:
select c.*
from ambank.dbo.commissions c
where commission_date = '2019-06-30' and
(target > 0 or final_payout > 0) and
agent_id in (4446, 2267) and
(c.team = 'Team Lead' or
not exists (select 1
from ambank.dbo.commissions c2
where c2.agent_id = c.agent_id and
c2.commission_date = c.commission_date and
(c2.target > 0 or c2.final_payout > 0) and
c2.team <> 'Team Lead'
)
)
order by agent ;
您可以选择下面的选项-
SELECT * FROM your_table
WHERE agent_id NOT IN (
SELECT agent_id FROM your_table
WHERE team = 'team lead'
)
UNION ALL
SELECT * FROM your_table
WHERE agent_id IN(
SELECT agent_id FROM your_table
WHERE team = 'team lead'
)
AND team = 'team lead'
您可以选择下面的选项-
SELECT * FROM your_table
WHERE agent_id NOT IN (
SELECT agent_id FROM your_table
WHERE team = 'team lead'
)
UNION ALL
SELECT * FROM your_table
WHERE agent_id IN(
SELECT agent_id FROM your_table
WHERE team = 'team lead'
)
AND team = 'team lead'
简单地说,我们可以使用
外部应用
来实现这一点,我们还可以检查该代理是否存在团队领导
,然后相应地在where
子句中添加条件
select
commission_date,
agent_id,
agent,
team,
role,
target,
collection,
commission,
final_payout
from ambank.dbo.commissions c
outer apply(select count(*) cnt
from ambank.dbo.commissions c1
where c.agent_id = c1.agent_id
and c1.team = 'Team Lead') c1
where commission_date = '2019-06-30'
and (target > 0 or final_payout > 0)
and agent_id in (4446, 2267)
AND (c1.cnt = 0 OR c.team = 'Team Lead')
order by agent
简单地说,我们可以使用
外部应用
来实现这一点,我们还可以检查该代理是否存在团队领导
,然后相应地在where
子句中添加条件
select
commission_date,
agent_id,
agent,
team,
role,
target,
collection,
commission,
final_payout
from ambank.dbo.commissions c
outer apply(select count(*) cnt
from ambank.dbo.commissions c1
where c.agent_id = c1.agent_id
and c1.team = 'Team Lead') c1
where commission_date = '2019-06-30'
and (target > 0 or final_payout > 0)
and agent_id in (4446, 2267)
AND (c1.cnt = 0 OR c.team = 'Team Lead')
order by agent