aggregate

  • リリースバージョン: Xanadu
  • 更新日 2024年08月01日
  • 所要時間:8分
  • aggregate 関数 (SUM、COUNT、MIN、MAX、LAST、AVG など) を使用してテーブルをクエリします。

    注:
    ここで説明する機能には、Aggregate Web Service プラグインが必要です。

    入力フィールド

    ターゲットテーブルの任意の要素。さらに、1 つ以上の aggregate 関数 (SUM、COUNT、MIN、MAX、LAST、AVG)。

    GROUP BY と HAVING 節を追加することもできます。

    出力フィールド

    取得されたレコードのすべてのフィールド値をカプセル化する aggregateResponse 要素。

    サンプル SOAP メッセージ

    COUNT 集計関数を使用したサンプル SOAP 要求。

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/encoding/"
                       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregate>
          <COUNT>number</COUNT>
          <active>true</active>
        </aggregate>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    COUNT 集計関数呼び出しの結果として生じる応答は、次のようになります。

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregateResponse>
            <aggregateResult>
              <avg>2.7200</avg>
            </aggregateResult>
        </aggregateResponse>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    GROUP BY 節を含む AVG 集計関数を使用するサンプル SOAP 要求。

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregate xmlns="http://www.service-now.com">
          <GROUP_BY>category</GROUP_BY>
          <active>true</active>
          <AVG>severity</AVG>
        </aggregate>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    GROUP BY 節を含む AVG 集計関数呼び出しの結果として生じる応答は、次のようになります。

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregateResponse>
            <aggregateResult>
              <avg>1.0000</avg>
              <category>database</category>
            </aggregateResult>
            <aggregateResult>
              <avg>3.0000</avg>
              <category>hardware</category>
            </aggregateResult>
            <aggregateResult>
              <avg>3.0000</avg>
              <category>inquiry</category>
            </aggregateResult>
            <aggregateResult>
              <avg>2.0000</avg>
              <category>network</category>
            </aggregateResult>
            <aggregateResult>
              <avg>2.6923</avg>
              <category>software</category>
            </aggregateResult>
        </aggregateResponse>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    エンコードクエリを使用して集計をフィルタリングするサンプル SOAP 要求:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/encoding/"
                       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregate>
          <COUNT>number</COUNT>
          <active>true</active>
          <__encoded_query>number=INC0000001^ORnumber=INC0000002</__encoded_query>
        </aggregate>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

    HAVING を使用して結果を絞り込むサンプル集計要求。

    HAVING は 4 つのフィールドを取ります。各フィールド (集計タイプ、集計のフィールド、操作タイプ、および比較する値) は「^」で区切られます。

    複数の HAVING を要求に追加できるため、HAVING 式を使用できますが、OR はサポートされていません。

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/encoding/"
                       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:m="http://www.service-now.com"
                       xmlns:tns="http://www.service-now.com/map"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <aggregate>
          <COUNT>sys_id</COUNT>
          <GROUP_BY>internal_type</GROUP_BY> 
          <HAVING>COUNT^*^>^10</HAVING>
          <HAVING>COUNT^*^<^20</HAVING>
          <COUNT>sys_id</COUNT>
          <active>true</active>
        </aggregate>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>