I have 3 tables
Table1 (a view) contains the record ID, Person's Name, Attribute ID and a new record for all attributes entered into the database pertaining to the person.
Table2 is a descriptive identifier name for the Attribute Type ID in Table1
Table3 (a view) contains the data entry timestamp for the record ID in Table1, used as selection criteria range
Table1 --SQL Server 2014 View--
ID [char(18)]
Name [nvarchar(50)]
AttributeTypeID [int]
AttributeValue [nvarchar(2000)]
Table2 --SQL Server 2014 Table--
AttrTypeID [int]
AttrName [nvarchar(50)]
Table3 --SQL Server 2014 View--
EntryID [char(18)]
EntryDateTime [datetime2(3)]
My current query brings them together (however I would like a different output):
SELECT DISTINCT
Table1.ID
Table1.Name
Table2.AttrTypeID
Table2.AttrName
Table1.AttributeValue
FROM
Table1 RIGHT JOIN Table2 ON Table1.AttributeTypeID = Table2.AttrTypeID
WHERE
Table1.ID IN (SELECT DISTINCT
EntryID
FROM Table3
WHERE
EntryDateTime BETWEEN '2018-05-01 00:00:00.000' AND '2018-05-07 00:00:00.000')
ORDER BY
Table1.ID ASC
Returns the following records:
ID | Name | AttrTypeID | AttrName | AttributeValue
123456 | Bob | 201 | Hair Color | Blonde
123456 | Bob | 202 | Eye Color | Blue
123456 | Bob | 203 | Height CM | 180
123456 | Bob | 204 | Weight KG | 77.1
123456 | Bob | 205 | Email | [email protected]
123456 | Bob | 206 | Profile | Text...2000characters...
789000 | Sarah | 201 | Hair Color | Brown
789000 | Sarah | 202 | Eye Color | Brown
789000 | Sarah | 203 | Height CM | 155
789000 | Sarah | 204 | Weight KG | 50.5
789000 | Sarah | 206 | Profile | Text...2000characters...
Note Sarah does not have an entry for email (AttrTypeID = 205) in table 1
My desired output is create a query that returns:
ID | Name | Hair Color | Eye Color | Height CM | Weight KG | Email | Profile
123456 | Bob | Blonde | Blue | 180 | 77.1 | [email protected] | Text...2000characters...
789000 | Sarah | Brown | Brown | 155 | 50.5 | NULL | Text...2000characters...
Your assistance is very much appreciated!
PS I'm new here so if you need more info or require a different format for asking this question please let me know.