This relates to "Query that combines columns from multiple rows..." but I couldn't figure out how to add all this to it.
Using the tools from the two pivot solutions from the other question on this site, I came up with this query below. I've made both pivot solutions work actually, but they both return the same results. And that is 5798 rows (the amount of phonenumber entries in that table and not the 1445 distinct users' accountIDs.
In the result of this query, we see AccountID, phone1, phone2, phone3, and phone4 are column headings and there is a row for every combination of accoundID and phonenumber. Row 1, for example has 1 phone number in column phone1. Row 2 and 3 are for the next accountID. row 2 has a phone1 entry and row 3 has a phone2 entry. I want all the phone entries on the same row with one accountID. What am I doing wrong? I did keep coming across problems with mixing data types, especially aggregating text and int.
Create table t3 (AccountID uniqueidentifier, phonenumber varchar(255)) insert t3 Select accountID, phonenumber from phonenumbers GO select AccountID, (case when sno=1 then phonenumber else '0' end) as phone1, (case when sno=2 then phonenumber else '0' end) as phone2, (case when sno=3 then phonenumber else '0' end) as phone3, (case when sno=4 then phonenumber else '0' end) as phone4 from ( select row_number() over(partition by AccountID order by AccountID) as sno, * from t3 ) t3 group by AccountID, t3.sno, t3.phonenumber Drop table t3