-1

I have look through the duplicated question but the solution there does not work for me.

I have two columns:

InstanceID 
ProductID

and want to get the unique ProductIDs only.

From others questions asked here I try this solution:

SELECT *
FROM  
   (SELECT CAST(A.Col001 AS int) AS SurveyInstanceID , 
           CAST(A.Col002 AS nvarchar(25)) AS ProductID , 
           ROW_NUMBER() OVER(PARTITION BY A.Col002 ORDER BY A.Col001  DESC) rn
    FROM DataSetsMaterializedDataSqlvariant A 
    WHERE DataSetsMaterializedInternalRowsetID = 5 
   ) a
WHERE rn = 1

but I get as a result duplicated values.

EDIT:

For more information, this is what I have in my table:

InstanceID  ProductID


1           10

1           11

1           12

1           13

2           10

2           A1

3           10

3           11  

3           B1  

3           C1  

3           D1

3           E1
......

I need to get the unique Product IDs. I am sorry I did not provided example at the beg.

4

2 回答 2

0

如果要删除重复项,可以使用distint

SELECT DISTINCT CAST(A.Col001 AS int) AS SurveyInstanceID , 
       CAST(A.Col002 AS nvarchar(25)) AS ProductID , 
       ROW_NUMBER() OVER(PARTITION BY A.Col002 ORDER BY A.Col001  DESC) rn
FROM DataSetsMaterializedDataSqlvariant A 
WHERE DataSetsMaterializedInternalRowsetID = 5 
于 2012-06-04T15:56:39.397 回答
0

这似乎很复杂。尝试这样的事情:

SELECT DISTINCT ProductId from DataSetsMaterializedDataSqlvariant 
WHERE DataSetsMaterializedInternalRowsetID = 5 

或者,如果您想要两列,则需要指定如果有多个列,您将选择哪个 InstanceId:

SELECT ProductId, MAX(InstanceId)
FROM DataSetsMaterializedDataSqlvariant 
WHERE DataSetsMaterializedInternalRowsetID = 5 
Group BY ProductId

如果这不是您要查找的内容,请发布您的示例数据和所需的输出。

于 2012-06-04T15:56:28.220 回答