【发布时间】: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