SQL Server查询类似于excel中的VLOOKUP逻辑

[英]SQL Server Query similar to VLOOKUP logic in excel


Table A:

  • AccountID (PK, int, not null)
  • AccountID(PK,int,not null)

  • Agent (varchar(50), null)
  • 代理(varchar(50),null)

  • AccountType (varchar(50), null)
  • AccountType(varchar(50),null)

Sample Table:

╔═══════════╦════════════════╦═══════╗
║ AccountID ║  AccountType   ║ Agent ║
╠═══════════╬════════════════╬═══════╣
║  413393   ║  Invoice       ║  A    ║
║  417811   ║  Credit        ║  NULL ║
╚═══════════╩════════════════╩═══════╝

Table B:

  • AccountID(int, not null) - This is the foreign key, and I am extracting the data from both the tables based on matching AccountID records.
  • AccountID(int,not null) - 这是外键,我根据匹配的AccountID记录从两个表中提取数据。

  • Ref_AccountID (int, null)
  • Ref_AccountID(int,null)

Sample Table:

╔═══════════╦════════════════╦
║ AccountID ║  Ref_AccountID ║ 
╠═══════════╬════════════════╬
║  413393   ║  NULL          ║  
║  417811   ║  413393        ║  
╚═══════════╩════════════════╩

Description: If the AccountType is invoice, then there would be a Agent associated with it. From Table A, you can see that it is associated with Agent A.

描述:如果AccountType是发票,则会有与之关联的代理。从表A中,您可以看到它与代理A相关联。

Current Output:

╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║  413393   ║ Invoice     ║    NULL       ║ A     ║
║  417811   ║ Credit      ║    413393     ║ NULL  ║
╚═══════════╩═════════════╩═══════════════╩═══════╝

Expected Output:

╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║  413393   ║ Invoice     ║    NULL       ║ A     ║
║  417811   ║ Credit      ║    413393     ║ A     ║
╚═══════════╩═════════════╩═══════════════╩═══════╝

The Agent should be displayed based on the Ref_AccountID. In this example, the Ref_AccountID is 413393, and for this AccountID in table A, the Agent is "A".

应根据Ref_AccountID显示代理。在此示例中,Ref_AccountID为413393,对于表A中的此AccountID,代理为“A”。

Thanks

2 个解决方案

#1


1  

This might be the answer you are seeking. I have included a complete query. It first joins tables A and B together, and then does a self join back to table A. From your updated information, it appears that when a record has a null value for the Agent column, you want to instead use the Agent value corresponding to the record whose AccountID matches the Ref_AccountID of the former record with the null Agent. The self join is necessary in the query because it makes this alternative value for the Agent potentially available in each record with a possible null Agent column. Here is the query:

这可能是您正在寻求的答案。我已经包含了一个完整的查询。它首先将表A和B连接在一起,然后自行连接回表A.从更新的信息中可以看出,当记录具有代理列的空值时,您希望改为使用对应的代理值。 AccountID与前一个记录的Ref_AccountID与空代理匹配的记录。自联接在查询中是必需的,因为它使代理的备用值可能在每个记录中可能具有可能的空代理列。这是查询:

SELECT t1.AccountID, t1.AccountType, t2.Ref_AccountID,
    CASE WHEN t1.Agent IS NOT NULL THEN t1.Agent ELSE t3.Agent END AS Agent
FROM TableA t1 INNER JOIN TableB t2 ON t1.AccountID = t2.AccountID
LEFT JOIN TableA t3 ON t2.Ref_AcccountID = t3.AccountID

Here is a working SQL Fiddle where you can test this query with the test data you gave in your original question.

这是一个有效的SQL小提琴,您可以使用原始问题中提供的测试数据测试此查询。

#2


0  

You're looking for

您正在寻找

SELECT * FROM yourTable
WHERE
AccountID = 466361  

Look here for more examples:

在这里查看更多示例:

https://msdn.microsoft.com/en-us/library/ms187731.aspx

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/11/11/720725852ae9f4c43f969b74b13b94e1.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告