agrégat

  • Rversion finale: Xanadu
  • Mis à jour 1 août 2024
  • 1 minute de lecture
  • Interrogez une table à l’aide d’une fonction d’agrégat incluant SUM, COUNT, MIN, MAX, LAST et AVG.

    Remarque :
    La fonctionnalité décrite ici nécessite le module d’extension Agrégat Web Service .

    Champs d'entrée

    N’importe quel élément de la table cible. En outre, une ou plusieurs des fonctions d’agrégat (SUM, COUNT, MIN, MAX, LAST et AVG).

    Une clause GROUP BY et une clause HAVING peuvent également être ajoutées.

    Champs de sortie

    Élément aggregateResponse encapsulant toutes les valeurs de champ de l’enregistrement récupéré.

    Exemples de messages SOAP

    Exemple de demande SOAP utilisant la fonction d’agrégat COUNT.

    <?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>

    La réponse résultante d’un appel de fonction d’agrégat COUNT ressemble à ceci :

    <?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>

    Exemple de demande SOAP utilisant la fonction d’agrégat AVG avec une clause GROUP BY.

    <?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>

    La réponse résultante d’un appel de fonction d’agrégat AVG avec une clause GROUP BY ressemble à ceci :

    <?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>
    

    Exemple de demande SOAP utilisant une requête codée pour filtrer l’agrégat :

    <?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>

    Exemple de demande d’agrégat utilisant HAVING pour affiner les résultats.

    HAVING prend quatre champs. Chaque champ est délimité par « ^ » : le type d’agrégat, le champ de l’agrégat, le type d’opération et la valeur à comparer.

    Plusieurs HAVING peuvent être ajoutés à la demande, vous pouvez donc utiliser des expressions HAVING, mais il n’y a pas de prise en charge pour 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>