The query result is as follows:
| name | CurencyName | Debtor | Creidtor | 
|---|---|---|---|
| agent1 | Currency1 | 20 | 0 | 
| agent1 | Currency2 | 0 | 10 | 
| agent2 | Currency1 | 0 | 50 | 
| agent2 | Currency2 | 0 | 10 | 
However, I want the result in the following format:
| name | currency1Debtor | currency1Creidtor | currency2Debtor | currency2Creidtor | 
|---|---|---|---|---|
| agent1 | 20 | 0 | 0 | 10 | 
| agent2 | 0 | 50 | 0 | 10 | 
The TSQL that the linq would generate would be something like,
;WITH T1 AS
(
SELECT
        Name,
        Currency1 Currency1Debtor,
        Currency2 Currency2Debtor
    FROM
        (
        SELECT
                    Name,
                    CurrencyName,
                    Debtor
            FROM
                    #Temp
        ) AS SourceTable
    PIVOT
        (
            SUM(Debtor) FOR CurrencyName IN (Currency1, Currency2)
        ) AS PivotTable
),
T2 AS
(
SELECT
            Name ,
            Currency1 Currency1Creditor,
            Currency2 Currency2Creditor
    FROM
            (
            SELECT
                        Name,
                        CurrencyName,
                        creditor
                FROM
                        #Temp
            ) AS SourceTable
    PIVOT
            (
                SUM(creditor) FOR CurrencyName IN (Currency1, Currency2)
            ) AS PivotTable
)
SELECT
            T1.*,
            T2.Currency1Creditor,
            T2.Currency2Creditor
    FROM
            T1
        INNER JOIN
            T2
                ON T1.Name = T2.Name
 
     
    