Im trying to write a query that pulls out data from a couple of other tables as columns in the query.
Here is the data:
User table
ID ACCOUNT_ID FIRST_NAME LAST_NAME
1 1 Joe SMITH
2 1 Bill WALTERS
3 1 Bill JOHNSON
User Fields Table
ID ACCOUNT_ID NAME
1 1 CITY
2 1 STATE
3 2 EMPLOYEE_NUMBER
4 3 MIDDLE_NAME
User Fields Data Table
ID USER_FIELD_ID USER_ID DATA
1 1 1 LINCOLN
2 2 1 NEBRASKA
I would like a query something like:
SELECT FIRST_NAME, LAST_NAME FROM users WHERE ACCOUNT_ID=1
But I would like it to include CITY and STATE as columns, but these will be different based on the ACCOUNT_ID and the data in the User Fields table.
I'm thinking this might be accomplished with PIVOT, but I am having a hard time getting it to work. Thanks!