Attached is the result that I required (the line I highlighted in blue that contains the correct info that I need).
As you can see, I have tons of rows, which includes those infos from the previously deleted files.
I need to know how to exclude those infos from those deleted files from my final output.
Actually I have use the same query for my previous project, and it works just fine. Only this time that it happened this way where the infos from deleted files still appear.
By the way, it's a Windows file.
Unless you have a mechanism in place for reading file system information into SQL server and then updating your projects table, your results are not going to change. Without more information, all I can offer is an improvement on your query. This runs 10x faster in a simple test harness:
“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw
FROM Projects AS P
OUTER APPLY (
[Drawing Number] = MAX(CASE WHEN v.VariableName = 'Whole Number' THEN vv.ValueText END),
[Project Name] = MAX(CASE WHEN v.VariableName = 'Folder Name' THEN vv.ValueText END),
[Folder Description] = MAX(CASE WHEN v.VariableName = 'Folder Description' THEN vv.ValueText END),
[Start Date] = MAX(CASE WHEN v.VariableName = 'Start Date' THEN vv.ValueText END),
[End Date] = MAX(CASE WHEN v.VariableName = 'End Date' THEN vv.ValueText END),
[Designer] = MAX(CASE WHEN v.VariableName = 'Author' THEN vv.ValueText END)
FROM VariableValue vv
INNER JOIN Variable v ON v.VariableID = vv.VariableID
WHERE vv.ProjectID = p.ProjectID -- CORRELATION
AND v.VariableName IN ('Whole Number','Folder Name','Folder Description','Start Date','End Date','Author')
GROUP BY vv.ProjectID
WHERE P.Deleted = 0
For fast, accurate and documented assistance in answering your questions, please read this article
.Understanding and using APPLY, (I)
and (II) Paul White Hidden RBAR: Triangular Joins
/ The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps