I currently have a table that looks like this:
Id // Key
General
platformId
platformName
Products [
Repeated Product {
Country
URL
Offers [
Repeated Offer
Type
Price
Currency
]
}
]
I need to convert it to a different format:
Record ID // Key
Country
Providers [
Repeated provider
platformName
Offers [
Repeated Offer
Type
Price
Currency
]
]
I originally flatten the table and get something like this:
id,platformId,platformName,products.product.country,products.product.offers.offer.price,products.product.offers.offer.type,products.product.offers.offer.currency
1,123,AWS,US,1.99,CPU,USD
1,123,AWS,US,1.99,HDD,USD
1,123,AWS,US,1.99,RAM,USD
2,123,AWS,CA,2.99,CPU,CAN
2,123,AWS,CA,2.99,HDD,CAN
2,123,AWS,CA,2.99,RAM,CAN
3,123,GOOG,US,3.99,CPU,GBP
3,123,GOOG,US,3.99,HDD,GBP
3,123,GOOG,US,3.99,RAM,GBP
I would like to group the following fields by country and by platform name:
1,123,AWS,US,1.99,CPU,USD
1,123,AWS,US,1.99,HDD,USD
1,123,AWS,US,1.99,RAM,USD
3,123,GOOG,US,1.99,CPU,GBP
3,123,GOOG,US,1.99,HDD,GBP
3,123,GOOG,US,1.99,RAM,GBP
The field structure should look like this:
123,US,AWS
CPU,1.99,USD
HDD,1.99,USD
RAM,1.99,USD
GOOG
CPU,3.99,USD
HDD,3.99,USD
RAM,3.99,USD
Any pointers? Currently Im not able to group by the country:
+---------+---------------+--------+--------+----------+
| country | platformName | type | price | currency |
+---------+---------------+--------+--------+----------+
| US | AWS | CPU | 1.99 | USD |
| | | HDD | 1.99 | USD |
| | | RAM | 1.99 | USD |
| CA | AWS | CPU | 2.99 | CAN |
| | | HDD | 2.99 | CAN |
| | | RAM | 2.99 | CAN |
| US | GOOG | CPU | 3.99 | USD |
| | | HDD | 3.99 | USD |
| | | RAM | 3.99 | USD |
--------------------------------------------------------
This is my query
SELECT
country,
platformName,
NEST(type) AS type,
NEST(price) AS price,
CASE
WHEN NEST(currency) = '' THEN NULL
ELSE NEST(currency)
END AS currency,
FROM
tbl
WHERE
master_id = 123
GROUP BY
platform_name,
country