Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 2008_Distinct - Fatal编程技术网

Sql 得到明显的结果

Sql 得到明显的结果,sql,sql-server-2008,distinct,Sql,Sql Server 2008,Distinct,我有下列表格 Product--为productid存储 ProductRelation--存储链接的产品id DECLARE @Product table(ProductID int) DECLARE @ProductRelation TABLE (FirstProductID int,SecondProductID int) INSERT INTO @Product SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SE

我有下列表格

Product--为productid存储 ProductRelation--存储链接的产品id

DECLARE @Product table(ProductID int)

DECLARE @ProductRelation TABLE (FirstProductID int,SecondProductID int)

INSERT INTO @Product 

SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9
UNION ALL
SELECT 10

--SELECT * FROM @Product

INSERT INTO @ProductRelation

SELECT 1,2
UNION ALL
SELECT 3,5
UNION ALL
SELECT 2,6
UNION ALL
SELECT 1,4
UNION ALL
SELECT 1,4

--SELECT * FROM @ProductRelation

SELECT ProductID,'Not Linked' AS 'Relation' FROM @Product
UNION
SELECT FirstProductID,'Linked' from @ProductRelation
UNION
SELECT SecondProductID ,'Linked' FROM @ProductRelation
以上查询结果重复ProductID

我想选择不同的产品id…如果产品id之间存在关系,则应显示带有“链接”的产品id 如果没有关系,则ProductID与“未链接”

我想要这样的预期结果

ProductID          Relation

1           Linked
2           Linked
3           Linked
4           Linked
5           Linked
6           Linked
7           Not Linked
8           Not Linked
9           Not Linked
10          Not Linked
试试这个:

SELECT
   P.ProductID,
   CASE WHEN COUNT(R.FirstProductID) > 0
        THEN 'Linked'
        ELSE 'Not Linked'
   END Relation
FROM Product P
   LEFT JOIN ProductRelation R
      ON P.ProductID = R.FirstProductID
        OR P.ProductID = R.SecondProductID
GROUP BY P.ProductID

在SQL Server中,可以将左连接与大小写结合使用

SELECT DISTINCT 
   ProductID,
   CASE WHEN @ProductRelation.FirstProductId IS NULL THEN 'Not Linked' ELSE 'Linked' END [Status]
FROM @Product 
LEFT JOIN @ProductRelation ON @Product.ProductID = @ProductRelation.FirstProductId
RIGHT JOIN @ProductRelation ON @Product.ProductID = @ProductRelation.SecondProductId

嗯。。。我喜欢有机会使用一些非皮沃特技

select P.ProductID
,isnull(L.relation,'Not Linked') as relation
from @Product P
left outer join (select U.ProductID, cast('Linked' as varchar(max)) as relation
                from @ProductRelation
                unpivot (ProductID for prod in (FirstProductID,SecondProductID))U
                group by U.ProductID
)L
on L.ProductID = P.ProductID

你在使用什么数据库?