I have two tables
Table 1:
-----------------------------------------------
| iFirstID | fkSomeID | cText | bStatus |
| 1 | 59 | 'blah' | 1 |
| 2 | 100 | 'text' | 0 |
-----------------------------------------------
Table 2:
-------------------------------------------------------------------------
| iSecondID | fkFirstID | fkOtherID | cSomeText | bSomeBool |
| 1 | 1 | 1 | "text" | 1 |
| 2 | 1 | 2 | "more text" | 0 |
| 3 | 1 | 3 | "more text" | 0 |
| 4 | 2 | 1 | "blah" | 1 |
| 5 | 2 | 2 | "test" | 1 |
| 6 | 2 | 3 | "data" | 0 |
-------------------------------------------------------------------------
I'd like to write a query which will return the following dataset based on the above
Result:
-----------------------------------------------------------------------------------------------------
| fkFirstID | fkSomeID | cText | bSomeBool[1] | bSomeBool[2] | bSomeBool[3] | bStatus |
| 1 | 59 | 'blah' | 1 | 0 | 0 | 0 |
| 2 | 100 | 'text' | 1 | 1 | 0 | 0 |
-----------------------------------------------------------------------------------------------------
As you can see in the above, bSomeBool will be reported for every row in table 2 with the same fkFirstID.
I'm not familiar with pivots at all, but am pretty sure this is what I'll need to use to accomplish what I'm looking for. I've looked at a few suggestions online (like this one: SQL server join tables and pivot) but can't wrap my head around it, as most differ a little from what I'm trying to accomplish.
There is a third table not included which is where fkOtherID comes into play. We can assume this table to have two columns: iOtherID and cColumnName
In Table 2, we will never have a duplicate fkOtherID for the same fkFirstID. So for each fkOtherID value, we would go to table 3, and set the column name as cColumnName. Not sure if this will affect the final solution too much.
I've tried using an inner join, but the result was that it would only look at the first row of Table 2.
I tried using Group By, but it kept returning errors for my other columns as they weren't a part of an aggregate function, but even still I don't think this would be what I want as I think it'd return two rows, one with a 1, and the other with a 0 for the bSomeBool column.