请帮我写下面的更新查询。我正在使用SQL Server 2014。
要求:根据项目ID更新表C,获取相应的答案权利'相应项目ID和最新记录的值。表A'最新' ='是'意味着它是最新的记录。基于' Link_Id'表A和B是链接的。表A和C基于' Project_Id'
表A:
<table>
<tr>
<th>
Request Type |
</th>
<th>Project ID | </th>
<th>Latest |</th>
<th> Link_ID</th>
</tr>
<tr>
-----------------------------------------------
</tr>
<tr>
<td>
a
</td>
<td>1000</td> <td>No</td> <td>1</td>
</tr>
<tr>
<td>b</td> <td>1005</td> <td>Yes </td> <td>2</td>
<tr>
<td>123</td> <td>1000</td><td>Yes </td> <td>4</td></tr>
<tr>
<td>c </td><td>1005</td> <td>No</td> <td>3</td></tr>
</table>
&#13;
表B
<table>
<tr>
<th>
Question</th> <th>Description</th> <th>Answer_weightage</th> <th>Link_ID
</th>
</tr>
<tr><td>
6 </td><td>Question6 </td><td>0</td> <td>2</td>
</tr>
<tr>
<td>2</td> <td>Question2</td> <td>5</td> <td>4</td></tr>
<tr>
<td>3</td> <td>Question3 </td><td>5</td> <td>4</td></tr>
<tr>
<td>4</td> <td>Question4</td><td> 5</td> <td>4</td></tr>
<tr>
<td>5 </td><td>Question5 </td><td>2</td> <td>4</td></tr>
<tr>
<td>6</td> <td>Question6 </td><td>2 </td><td>4</td></tr>
<tr>
<td>7</td> <td>Question7</td> <td>9 </td><td>4</td></tr>
<tr>
<td>1</td> <td>Question1 </td><td>5</td> <td>1</td></tr>
<tr>
<td>2</td> <td>Question2 </td><td>9</td> <td>1</td></tr>
<tr>
<td>3 </td><td>Question3 </td><td>5</td><td> 1</td></tr>
<tr>
<td>4</td> <td>Question4</td> <td>2 </td><td>1</td></tr>
<tr>
<td>5</td> <td>Question5</td> <td>5 </td><td>1</td></tr>
<tr>
<td>6</td> <td>Question6</td> <td>5 </td><td>1</td></tr>
<tr>
<td>7</td> <td>Question7</td> <td>2</td> <td>1</td></tr>
<tr>
<td>1</td> <td>Question1</td> <td>2 </td><td>2</td></tr>
<tr>
<td>2</td> <td>Question2</td> <td>0 </td><td>2</td></tr>
<tr>
<td>3 </td><td>Question3</td> <td>9 </td><td>2</td></tr>
<tr>
<td>4</td> <td>Question4 </td><td>9</td><td> 2</td></tr>
<tr>
<td>5 </td><td>Question5 </td><td>9</td><td> 2</td></tr>
<tr>
<td>7 </td><td>Question7</td> <td>5 </td><td>2</td></tr>
<tr>
<td>1</td> <td>Question1</td><td> 2</td> <td>4</td></tr>
</table>
&#13;
表C:
<table>
<tr>
<th>Project ID</th>
<th>Question1</th>
<th>Question2</th>
<th>Question3</th>
<th>Question4</th>
<th>Question5</th>
<th>Question6</th>
<th>Question7</th>
</tr>
<tr>
<td>1000</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>1005</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
&#13;
期望的结果(表C):
<table>
<tr>
<th>Project ID</th>
<th>Question1</th>
<th>Question2</th>
<th>Question3</th>
<th>Question4</th>
<th>Question5</th>
<th>Question6</th>
<th>Question7</th>
</tr>
<tr>
<td>1000</td>
<td>2</td>
<td>5</td>
<td>5</td>
<td>5</td>
<td>2</td>
<td>2</td>
<td>9</td>
</tr>
<tr>
<td>1005</td>
<td>2</td>
<td>0</td>
<td>9</td>
<td>9</td>
<td>0</td>
<td>9</td>
<td>5</td>
</tr>
</table>
&#13;
答案 0 :(得分:0)
您想要更新表C的问题1,将值设置为等于从B中选择的Answer_weightage,其中b.Description =&#39; Question1&#39;和linkId =(来自A的Link_Id,其中最新的=&#39;是&#39;并且具有与C相同的projectId)。
Update C set C.Question1 =
(select Answer_weightage
from B
where Description='Question1' and Link_ID =
(select Link_ID
from A
where A.ProjectID = C.ProjectID and Latest='Yes'))