1

SQL クエリに問題があります。

次の表があります(少し簡略化しました):

 ChainId    ComponentId    TransmitEndpointId
   1            156               NULL
   1            156               7
   1            157               7
   2            510               6
   2            510               6
   2            511               6
   2            511               8

私がする必要があるのは、「一意の」ChainId で TransmitEndpointId の foreach の「一意の」ComponentId の数を取得することです。したがって、上記のデータの結果は次のようになります: 5 (チェーン 1 の 2 つの一意の componentId & チェーン 2 の 2 つの一意の componentId と 2 つの異なる TransmitId ==> NULL 値はカウントされません)

これは非常に複雑で、このクエリを開始する方法がわかりません。

誰でも助けることができますか?

前もって感謝します。

4

3 に答える 3

2

追加する where TransmitEndpointId is not nullか、NULLのために0を取得します

SELECT ChainId,
       ComponentId,
       count(distinct TransmitEndpointId)
FROM chain
where TransmitEndpointId is not null
GROUP BY ChainId, ComponentId

select sum(endPointNr) from 
(
SELECT count(distinct TransmitEndpointId) as     endPointNr
FROM chain
where TransmitEndpointId is not null
GROUP BY ChainId, ComponentId
) X

ここに画像の説明を入力してください

編集(追加する理由 TransmitEndpointId is not null

ここに画像の説明を入力してください

于 2012-05-09T10:00:08.093 に答える
2

これにより、ChainID/ComponentIdの組み合わせごとに一意のカウントが得られます。

SELECT ChainId,
       ComponentId,
       count(distinct TransmitEndpointId)
FROM your_table
GROUP BY ChainId, ComponentId

これで、派生テーブル内でそれを使用して、合計数を取得できます。

SELECT sum(distinct_count)
FROM  (
  SELECT ChainId,
         ComponentId,
         count(distinct TransmitEndpointId) as distinct_count
  FROM your_table
  GROUP BY ChainId, ComponentId
) t
于 2012-05-09T09:45:57.797 に答える
0

私はそれがあなたが望むものだと思います

このコードを試してください

SELECT COUNT(DISTINCT ChainId,ComponentId,TransmitEndpointId)
FROM your_table
WHERE TransmitEndpointId IS NOT NULL
于 2012-05-09T11:05:46.660 に答える