Page 87 - SQL
P. 87

RESULTS



          city_name        avg_population


          New York City    8,250,754


              Note: The AVG() function will convert values to numeric types. This is especially
              important to keep in mind when working with dates.


        List Concatenation


        Partial credit to this SO answer.


        List Concatenation aggregates a column or expression by combining the values into a single string
        for each group. A string to delimit each value (either blank or a comma when omitted) and the
        order of the values in the result can be specified. While it is not part of the SQL standard, every
        major relational database vendor supports it in their own way.


        MySQL




         SELECT ColumnA
              , GROUP_CONCAT(ColumnB ORDER BY ColumnB SEPARATOR ',') AS ColumnBs
           FROM TableName
          GROUP BY ColumnA
          ORDER BY ColumnA;



        Oracle & DB2





         SELECT ColumnA
              , LISTAGG(ColumnB, ',') WITHIN GROUP (ORDER BY ColumnB) AS ColumnBs
           FROM TableName
          GROUP BY ColumnA
          ORDER BY ColumnA;



        PostgreSQL




         SELECT ColumnA
              , STRING_AGG(ColumnB, ',' ORDER BY ColumnB) AS ColumnBs
           FROM TableName
          GROUP BY ColumnA
          ORDER BY ColumnA;



        SQL Server




        https://riptutorial.com/                                                                               69
   82   83   84   85   86   87   88   89   90   91   92