I'm in the middle of "translating" Power BI queries into SQL queries. One of the key queries does the following:
- Takes the following table:
+-----+-----+----------+
| PID | FID | Quantity |
+-----+-----+----------+
| 1 | A | 15 |
+-----+-----+----------+
| 1 | B | 2 |
+-----+-----+----------+
| 2 | B | 3 |
+-----+-----+----------+
| 2 | D | 8 |
+-----+-----+----------+
| 3 | C | 2 |
+-----+-----+----------+
- Group by PID, keeping all rows (each
Datais a table):
+-----+-------+
| PID | Table |
+-----+-------+
| 1 | Data |
+-----+-------+
| 2 | Data |
+-----+-------+
| 3 | Data |
+-----+-------+
- Runs some custom logic/manipulation (some of which are very complicated, so not just
COUNTorMAX, etc.) perData, and turns eachDatainto a new table per row:
+-----+-------+-------------+
| PID | Table | Transformed |
+-----+-------+-------------+
| 1 | Data | Data |
+-----+-------+-------------+
| 2 | Data | Data |
+-----+-------+-------------+
| 3 | Data | Data |
+-----+-------+-------------+
- Expand the
Transformedcolumn:
+-----+----------------+----------------+
| PID | ResultColumn-1 | ResultColumn-2 |
+-----+----------------+----------------+
| 1 | SomeResult-1 | SomeResult-1b |
+-----+----------------+----------------+
| 2 | SomeResult-2 | SomeResult-2b |
+-----+----------------+----------------+
| 3 | SomeResult-3 | SomeResult-3b |
+-----+----------------+----------------+
Is this procedure possible to do in SQL? I was thinking we could use Group By with some custom function, but not entirely sure how to go about doing that - any guidance would be greatly appreciated.
Thanks!
Sample Output:
+-----+------------------+----------------+
| PID | ResultColumn-1 | ResultColumn-2 |
+-----+------------------+----------------+
| 1 | There are 2 of B | and 15 of A |
+-----+------------------+----------------+
| 2 | There are 8 of D | and 3 of B |
+-----+------------------+----------------+
| 3 | There are 2 of C | |
+-----+------------------+----------------+