Create a dataset

Query service provides the Create Table As Select (CTAS) functionality to create a table based on the output of a SELECT statement, or as in this case, by using a reference to an existing XDM schema in Adobe Experience Platform. Displayed below is the XDM schema for Final_subscription created for this example.

A diagram of the final_subscription schema.

The following example demonstrates the SQL used to create the final_subscription_test2 dataset. final_subscription_test2 is created using the Final_subscription schema. Data is extracted from the source using a SELECT clause to populate some rows.

CREATE TABLE final_subscription_test2 with(schema='Final_subscription') AS (
        SELECT struct(userid, collect_set(subscription) AS subscription) AS _lumaservices3 FROM(
            SELECT user AS userid,
                   struct( last(eventtime) AS last_eventtime,
                           last(status) AS last_status,
                           offer_id,
                           subsid AS subscription_id)
                   AS subscription
             FROM (
                   SELECT _lumaservices3.msftidentities.userid user
                        , _lumaservices3.subscription.subscription_id subsid
                        , _lumaservices3.subscription.subscription_status status
                        , _lumaservices3.subscription.offer_id offer_id
                        , TIMESTAMP eventtime

                   FROM
                        xbox_subscription_event
                   UNION
                   SELECT _lumaservices3.msftidentities.userid user
                        , _lumaservices3.subscription.subscription_id subsid
                        , _lumaservices3.subscription.subscription_status status
                        , _lumaservices3.subscription.offer_id offer_id
                        , TIMESTAMP eventtime
                   FROM
                        office365_subscription_event
             )
             GROUP BY user,subsid,offer_id
             ORDER BY user ASC
       ) GROUP BY userid)

In the initial dataset final_subscription_test2, the struct data type is used to contain both the subscription field and the userid which is unique to each user. The subscription field describes the product subscriptions for a user. There can be multiple subscriptions but a table can only contain the information for one subscription per row.