簡體   English   中英

SQL Server中的條件語句

[英]Condition statement in SQL Server

我試圖根據他/她的角色來檢索他/她的收藏。 代理可以扮演兩個角色,即收藏家和團隊負責人。 在這種情況下,我只需要檢索團隊領導的行。

嘗試使用case語句。 但是沒有運氣。

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 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 (select c.*,
             sum(case when c.team = 'Team Lead' then 1 else 0 end) over (partition by agent_id) as num_team_lead
      from ambank.dbo.commissions c
      where commission_date = '2019-06-30' and
            (target > 0 or final_payout > 0) and
            agent_id in (4446, 2267) 
     ) c
where c.team = 'Team Lead' or num_team_lead = 0
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'

簡而言之,我們也可以使用OUTER APPLY來實現,我們可以檢查該負責人是否存在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM