Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 若联接存在,则从联接表返回值_Sql_Sql Server_Tsql_Sql Server 2008 R2 - Fatal编程技术网

Sql 若联接存在,则从联接表返回值

Sql 若联接存在,则从联接表返回值,sql,sql-server,tsql,sql-server-2008-r2,Sql,Sql Server,Tsql,Sql Server 2008 R2,我有两个表,下面有列 库存: 和桌子 存货价格: 我只想为每个项目选择一行。 如果存在联接,那么我需要从这个“InventoryPrices”中选择价格,或者从我的第一个表“Inventory”中选择价格。到目前为止,如果“InventoryPrices”与第一个表“Inventory”连接,它会为每个ID返回两行 如何检查联接是否不为空,然后用第二个表price向我显示一行要检查InventoryPrices表中是否存在记录,您需要使用LEFT join为每个Id只获取一行,您可以像下面的查询

我有两个表,下面有列

库存: 和桌子

存货价格: 我只想为每个项目选择一行。 如果存在联接,那么我需要从这个“InventoryPrices”中选择价格,或者从我的第一个表“Inventory”中选择价格。到目前为止,如果“InventoryPrices”与第一个表“Inventory”连接,它会为每个ID返回两行


如何检查联接是否不为空,然后用第二个表price向我显示一行要检查InventoryPrices表中是否存在记录,您需要使用LEFT join为每个Id只获取一行,您可以像下面的查询一样使用行号

 SELECT   *             
         from
         ( 
                   SELECT    t1.id, 
                             t1.NAME, 
                             COALESCE(t2.price,t1.price) AS price ,
                             Row_number() OVER(partition BY t1.id ORDER BY t1.id) rn 
                   FROM      inventory t1 
                   LEFT JOIN inventoryprices t2 
                   ON        t1.id=t2.id 
     ) t 
WHERE    t.rn=1

您可以使用以下查询:

WITH CTE_1 AS (

    SELECT T1.ID, T1.Name, 
    CASE WHEN T2.Price is NOT NULL THEN T2.Price ELSE T1.Price END as [Price], 
    ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) as rn  
    FROM Inventory T1
    LEFT JOIN InventoryPrices T2 
    ON T1.ID = T2.ID)

 SELECT * FROM CTE_1 WHERE rn = 1
“外部应用”(outer apply)实现了一种称为“横向连接”(侧向连接)的功能,完全满足您的需要:

select t1.id, t1.name, coalesce(t2.price, t1.price)
from t1 outer apply
     (select top (1) t2.*
      from t2
      where t2.id = t1.id
     ) t2;

通常情况下,子查询会有一个order by来指定您想要的价格-最小的、最旧的、最新的或通过其他方法进行优先级排序的价格。

在本例中,我在t1.id=t2.id tin有效列名“rn”之后得到错误语法。我忘记将rn移动到内部表,我已经更正了它,它现在应该可以工作了。它可以工作了!谢谢,如果我也使用where语句,它会起作用吗?你需要把它放在t1.id=t2.id和t2.CatalogID=3上,而不是放在where上。如果你想过滤左连接中的记录,你需要把你的条件放在连接条件本身上,而不是放在完整的where上。我不明白。如果在inventory中有一行对应库存ID 1,在inventory Prices中有一行对应库存ID 1,并且您在inventory ID上加入,则仍然有一个结果行,而不是两个。请显示您的查询。在您给出的示例中,两个表中的每个id都没有2条记录,那么您如何在输出中获得每个id的2条记录
WITH CTE_1 AS (

    SELECT T1.ID, T1.Name, 
    CASE WHEN T2.Price is NOT NULL THEN T2.Price ELSE T1.Price END as [Price], 
    ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) as rn  
    FROM Inventory T1
    LEFT JOIN InventoryPrices T2 
    ON T1.ID = T2.ID)

 SELECT * FROM CTE_1 WHERE rn = 1
select t1.id, t1.name, coalesce(t2.price, t1.price)
from t1 outer apply
     (select top (1) t2.*
      from t2
      where t2.id = t1.id
     ) t2;