I have a database with tables GridData, FormList and FormData.
I need to pull a field called Value from the FormData table, but cannot figure out how to structure the query. I can run it one-by-one and it gives me the result, but need to include it together in a table output.
The query, when I run it step-by-step is:
Select * from GridData where GridName = 'uwg' -- returns:
FormId GridName Example Field
1244135 uwg 9
1244135 uwg 10
1244135 uwg 11
1244135 uwg 7
1244135 uwg 66
Using that FormID field, I lookup the FormList table
Select * from FormList where FormID = 1244135 -- returns:
JobId FormName FormId
1241899 ExampleForm 1244135
Then querying the SAME FormList table where FormID = JobID from above..
Select * from FormList where FormID = 1241899 -- returns:
JobId FormName FormId
1241894 ExampleForm2 1241899
AGAIN.. querying the SAME FormList table where FormID = JobID from above..
Select * from FormList where FormID = 1241894 -- returns:
JobId FormName FormId
1241893 ExampleForm3 1241894
The JobID from this result is the ID I need to reference in the FormData table to return the value I need...
Select Value from FormData where FormID = 1241893 and Name = 'ProductName' -- returns:
Value
12345
My goal here is to be able to return all the values from the GridData table, with the corresponding entry in the FormData "Value" field.
I'm really struggling with how to write this query!
I've tried the following, but it returns multiple duplicate entries. The Value field contains multiple data types, and I'm only interested on the value from this table where FormData.Name = 'ProductName'
SELECT Value,
RecordId,
GridData.FormId,
GridName,
ExampleField
FROM GridData
JOIN FormData
ON GridData.FormId = FormData.FormID
WHERE GridData.FormId IN
(SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN
(SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN
(SELECT FormList.FormId FROM FormList WHERE FormList.FormId IN
(SELECT FormData.FormID FROM FormData))))
AND FormData.DataItemName = 'JobProductName'
JobID, not theFormId. Also, put theANDclause in the parentheses with the FormData-SELECT)