【问题标题】:Returning a value of a column as null or empty when a value of other column in the same table is 1当同一表中的其他列的值为 1 时,将列的值返回为 null 或空
【发布时间】:2014-07-03 17:57:20
【问题描述】:

我正在处理一个 SQL 查询,它有一些连接,会产生一定数量的行和列。我试图以这样一种方式返回结果,即当某一行的特定列中的值为 1 时,应将同一行但不同列中的其他值作为空值返回。

SQL Server 2012中我的SQL查询如下

select a.id,a. Date, c.companyname as 'Active Company', h.employee_fullname as ‘Client',j.displayname as 'Seller Employee', d.companyName as 'CounterParty Company',g.broker_fullname as 'Buyer Trader', a.transactiontype
from confirmations a with (nolock)
left join company c with (nolock) on c.company_id = a.activecompany
left join company d with (nolock) on d.company_id = a.counterCompany
left join bprod e with (nolock) on e.id = a.productID
left join btype f with (nolock) on  f.id = a.quantitytypeid
left join companybroker g with (nolock) on g.companybroker_id = a.counter
left join companybroker h with (nolock) on h.companybroker_id = a.active
left join users i with (nolock) on i.id = a.buyemp
left join users j with (nolock) on j.id = a.sellemp
Where JoinDate > '2011-06-04 00:00:00' 
and a.disabled = 0 
order by dealid

以上查询返回以下结果

Id Active Company Client Seller Employee CounterPartyCompany Buyer Trader Transaction Type
1  ABC             XYZ      PQR             CHEV                 John           0
2  ABC2            2XYZ    2PQR            2CHEV                 John11         0
3  3ABC            3XYZ     3PQR           3CHEV                 John12         1
4  3ABC            3XYZ     AAA              CCC                 John12         1

一切看起来都很好,但是当Transaction Type 为 1 时,我试图将 CounterPartyCompany 作为 Empty 返回,如下所示

 Id Active Company Client Seller Employee CounterPartyCompany Buyer Trader Transaction Type
    1  ABC             XYZ      PQR             CHEV                 John           0
    2  ABC2            2XYZ    2PQR            2CHEV                 John11         0
    3  3ABC            3XYZ     3PQR                                 John12         1
    4  3ABC            3XYZ     AAA                                  John12         1

我可以知道一个更好的方法来解决这个问题

【问题讨论】:

    标签: sql sql-server sql-server-2012-express


    【解决方案1】:

    case 语句的完美使用:

    select a.id, a.Date, c.companyname as 'Active Company', 
    h.employee_fullname as 'Client', j.displayname as 'Seller Employee', 
    case when a.transactionType =1 then null
    else d.companyName end as 'CounterPartyCompany',
    g.broker_fullname as 'Buyer Trader', a.transactiontype
    from confirmations a with (nolock)
    left join company c with (nolock) on c.company_id = a.activecompany
    left join company d with (nolock) on d.company_id = a.counterCompany
    left join bprod e with (nolock) on e.id = a.productID
    left join btype f with (nolock) on  f.id = a.quantitytypeid
    left join companybroker g with (nolock) on g.companybroker_id = a.counter
    left join companybroker h with (nolock) on h.companybroker_id = a.active
    left join users i with (nolock) on i.id = a.buyemp
    left join users j with (nolock) on j.id = a.sellemp
    Where JoinDate > '2011-06-04 00:00:00' 
    and a.disabled = 0 
    order by dealid
    

    这确实假设只有 1 应该为空/空。所有其他出现的 transactionType 都将导致显示 d.companyName。

    【讨论】:

      猜你喜欢
      • 2023-01-19
      • 2013-03-24
      • 2014-10-08
      • 1970-01-01
      • 2017-08-03
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多