3

現在、次のクエリがあります

SELECT organisation.organisationID, COUNT(organisation.organisationID)
FROM position, positionLocation, organisation
WHERE position.positionLocationID = positionLocation.positionLocationID AND
positionLocation.organisationID = organisation.organisationID AND
position.status = 'Open'
GROUP BY organisation.organisationID;

このクエリは出力します

organisationID | countOrganisationID
1                3
3                2
5                3

最大 countOrganisationID を持つレコードを表示したいと思います。理想的には、可能であれば、organisationID と対応する organizationName を出力したいと思います。

の線に沿った何か

organisationID | organisatioName
1                name1
5                name2

どんな助けでも感謝します

ありがとう

4

3 に答える 3

5

Barrett の言うとおりです。RANK() が適しています。たとえば、次のようになります。

SELECT organisationID, c FROM (
  SELECT organisationID
        ,c
        ,RANK() OVER (ORDER BY c DESC) r
  FROM (
    SELECT organisation.organisationID
          ,COUNT(organisation.organisationID) AS c
    FROM position, positionLocation, organisation
    WHERE position.positionLocationID = positionLocation.positionLocationID
    AND positionLocation.organisationID = organisation.organisationID
    AND position.status = 'Open'
    GROUP BY organisation.organisationID
  )
) WHERE r = 1;
于 2012-04-23T04:41:20.410 に答える
4

それをサブクエリするだけです:

WITH counts AS (
    SELECT organisation.organisationID
          ,organisation.organisationName
          ,COUNT(organisation.organisationID) the_count
      FROM position, positionLocation, organisation
      WHERE position.positionLocationID = positionLocation.positionLocationID
        AND positionLocation.organisationID = organisation.organisationID
        AND position.status = 'Open'
      GROUP BY organisation.organisationID, organisation.organisationName
)
SELECT organisationID, organisationName
  FROM counts
  WHERE the_count = (SELECT MAX(the_count) FROM counts)
于 2012-04-22T22:05:46.620 に答える
1

これはうまくいくはずです。

SELECT organisationID,  organisatioName
FROM position, positionLocation, organisation
WHERE position.positionLocationID = positionLocation.positionLocationID AND
positionLocation.organisationID = organisation.organisationID AND
position.status = 'Open'
AND COUNT(organisation.organisationID) =
    SELECT MAX(cnt) AS MaxCnt FROM
        SELECT organisation.organisationID, COUNT(organisation.organisationID) AS cnt
        FROM organisation
        WHERE position.status = 'Open'
        GROUP BY organisation.organisationID

GROUP BY organisation.organisationID, organisation.organisatioName;
于 2012-04-22T22:03:47.663 に答える