Make many values in the sql-server cell.



  • We need to get information on the order-- id order е information on the order.

    Order information should be one cell.
    Example:
    INTEGRATED FRAMEWORK Quantity: bucoliness in order of mainland.
    INTEGRATED FRAMEWORK Quantity: bucoliness in order of mainland.
    INTEGRATED FRAMEWORK Quantity: bucoliness in order of mainland.
    ♪ ♪ ♪ ♪
    How do you do that? Here's the sign from where the information comes from.

    Табличка



  • In general, we consider that if there are a few things in the order in this table, there are a few identical lines (not even). Then we first count the quantity of the product in each. Then we build lines.

    WITH  qdata AS (
        SELECT order_id, name, count(name) AS qty
        FROM t
        GROUP BY order_id, name
    ),
    data AS (
        SELECT order_id, CONCAT(name,  ' количество ' , qty) AS x
        FROM qdata
    ) 
    

    Next in the new versions 2017+ string_agg

    SELECT order_id, string_agg(x, ', ')
    FROM data
    GROUP BY order_id
    

    In the old ones, it's usually XML - either a request or a cross application.

    SELECT order_id, LEFT(res, len(res)-1) AS res
    FROM data AS t1
    CROSS APPLY (
        SELECT x + ',' 
        FROM data  AS t2
        WHERE t2.order_id = t1.order_id
        FOR XML PATH('')
    ) AS z (res)
    GROUP BY order_id, res
    

    Instead of commas, you might need to. char(13)


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2