首页 > 解决方案 > 使用一个查询设置第二个查询的范围

问题描述

早上好

我有以下问题。有效我正在尝试使用从第一个查询中生成的客户端列表传递到第二个查询中以提取我需要提取的事务列表。

我是 sql 新手,所以甚至不确定是否可能。任何人都可以建议一个解决方案吗

--create list of client_accs 

select client_id, client_acc
from client_crm
where client_id = 'LOG0000240'

--select transaction data for list of client generated in first query

Select 
client_id,
test_cash_trans.client_acc,
sub_trans_type,
settle_date,
dbt_crt,
test_cash_trans.currency,
cash_amount,
test_cash_trans.last_update
From test_cash_trans

inner join client_crm
on test_cash_trans.client_acc = client_crm.client_acc

where test_cash_trans.client_acc in --- result of first query

group by client_id,test_cash_trans.client_acc, sub_trans_type, settle_date, dbt_crt, test_cash_trans.currency, cash_amount,test_cash_trans.last_update

标签: azure-sql-database

解决方案


您可能正在寻找IN

test_expression [ NOT ] IN   
    ( subquery | expression [ ,...n ]  
    )

确定指定值是否与子查询或列表中的任何值匹配。

这是一个例子:

SELECT p.FirstName, p.LastName  
FROM Person.Person AS p  
    JOIN Sales.SalesPerson AS sp  
    ON p.BusinessEntityID = sp.BusinessEntityID  
WHERE p.BusinessEntityID IN  
   (SELECT BusinessEntityID  
   FROM Sales.SalesPerson  
   WHERE SalesQuota > 250000);  

详细信息:IN (Transact-SQL)


推荐阅读