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、haing、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 に答える