0

我有一个包含以下数据的 Sybase 表(示例):

Users:

 ID        Type         PlayerID
 --------  -----------  ------------------- 
 65823     1            (null)              
 65823     2            187                 
 91817     1            (null)                         
 37950     1            773                 
 37950     1            (null)              
 37968     1            (null)              
 37968     1            773                                
 72576     1            (null)              
 72576     1            (null)                

我想返回用户和类型的所有组合,但如果有多个特定用户/类型组合的示例,则只显示不为 null的记录。

例如,上表应返回以下内容

 ID        Type         PlayerID
 --------  -----------  ------------------- 
 65823     1            (null)           - although this is null the type/id is unique   
 65823     2            187              
 91817     1            (null)           - this is null but is a unique type/id              
 37950     1            773                                         
 37968     1            773              - this is included and the id/type that has a null player id isn't                              
 72576     1            (null)           - this is a unique type/id

到目前为止,我已经研究了使用 group by、have 和 inner joins 的查询,但一直无法找到一种方法来匹配我正在寻找的结果。

我还查看了 group by 之类的东西,然后在 PlayerID 上使用 max,但聚合函数忽略了 null 值。

如何返回带有玩家 ID 的唯一 ID/类型对?

--

保罗的问题:

ID        Type         PlayerID
 --------  -----------  ------------------- 
 65823     2            187                        
 37950     1            773                                         
 37968     1            773    
4

2 回答 2

1

在 SQL 方面,您可以这样做:

select * from mytable t1 where playerId is not null
union
select * from mytable t2 where playerid is null 
   and not exists (
          select * from mytable t3 where t2.id=t2.id and t2.type=t3.type
       )

我不知道它会有多有效,但它会给你你想要的结果。

于 2013-02-06T17:22:40.367 回答
0

做什么

select  ID, type, max(PlayerID)
from    Users
group by ID, type

返回?(我没用过sybase)

于 2013-02-06T17:03:42.333 回答