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


注意!

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



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