I have:
SELECT dv.VariableID ,
ds.DataSourceID ,
p.DataVariableDataSourceParamId ,
p.ParamCode ,
p.ParamDisplayName ,
p.DVDSParamControlType ,
p.DependentOnDVDSParamId ,
pv.ParamValue
FROM dbo.DataVariable dv
INNER JOIN dbo.DataVariableDataSource ds ON dv.DataSourceId = ds.DataSourceID
INNER JOIN dbo.DataVariableDataSourceParam p ON ds.DataSourceID = p.DataSourceId
INNER JOIN dbo.DataVariableDataSourceParamValue pv ON p.DataVariableDataSourceParamId = pv.DataVariableDataSourceParamId
WHERE dv.VariableID = @vid
ORDER BY dv.VariableID
When I just have the first two joins, I get what I want: 6 results. When I add the third, I get 660. I just want the ParamValue for the 6 records from the first 2 joins and I can’t seem to figure out why this is breaking. I’m on my 12th hour of coding and I’m sure this is insanely obvious, but I could use a hand. Thanks in advance.
This is going to be because you have numerous rows in your
pvtable that match onDataVariableDataSourceParamIdYou can verify by adding a
SELECT DISTINCT. You may need to clean that table up or keep the distinctHowever, the distinct will only help if
pv.ParamValueis the same for all, otherwise you are rightfully getting more matches as what is happening is that you are finding all the matches forDataVariableDataSourceParamIdand displaying them. If all those matches are the same value, then the distinct will indeed help, though