2

我正在尝试以格式获取数据的水平输出

查询是:

SELECT RDT1.County_Name
      ,RDT1.DistributionNumber as Dist_No
      ,RDT1.Vac_Allocated
      ,RDT1.Priority,RDT2.DistributionNumber as Dist_No
      ,RDT2.Vac_Allocated as Vac_Allocated
      ,RDT3.DistributionNumber as Dist_No
      ,RDT3.Vac_Allocated as Vac_Allocated 
FROM Result_Distribution_Table AS RDT1
    ,Result_Distribution_Table AS RDT2
    ,Result_Distribution_Table AS RDT3 
WHERE RDT1.County_Name = RDT2.County_Name AND
      RDT1.DistributionNumber = 1 AND 
      RDT2.DistributionNumber = 2 AND 
      RDT3.DistributionNumber = 3 AND 
      RDT1.County_Name = RDT3.County_Name 
WHERE Solution_id= "10" 

当我执行这个查询时,我得到一个响应

消息 4104,级别 16,状态 1,行 1
无法绑定多部分标识符“Solution_id”。

Solution_id是中的一列Result_Distribution_Table

请帮助我做错了什么以及解决方案是什么?

4

2 回答 2

3

每个查询只能有一个WHERE子句。还要避免使用旧式连接而不是使用JOIN. 注意:solution_id为col填写正确的表格。

SELECT RDT1.County_Name
      ,RDT1.DistributionNumber as Dist_No
      ,RDT1.Vac_Allocated
      ,RDT1.Priority,RDT2.DistributionNumber as Dist_No
      ,RDT2.Vac_Allocated as Vac_Allocated
      ,RDT3.DistributionNumber as Dist_No
      ,RDT3.Vac_Allocated as Vac_Allocated 
FROM Result_Distribution_Table RDT1 JOIN  Result_Distribution_Table RDT2
      ON RDT1.County_Name = RDT2.County_Name JOIN Result_Distribution_Table RDT3 
      ON RDT1.County_Name = RDT3.County_Name
WHERE RDT1.DistributionNumber = 1 AND 
      RDT2.DistributionNumber = 2 AND 
      RDT3.DistributionNumber = 3 AND 
      [...].Solution_id= "10"

添加:刚刚注意到您使用同一张表 3 次。如果是这种情况,您可以获得与以下相同的结果;

SELECT County_Name
          ,DistributionNumber as Dist_No
          ,Vac_Allocated
          ,Priority,RDT2.DistributionNumber as Dist_No
          ,Vac_Allocated as Vac_Allocated
          ,DistributionNumber as Dist_No
          ,Vac_Allocated as Vac_Allocated 
FROM Result_Distribution_Table 
WHERE DistributionNumber IN (1,2,3) AND
          Solution_id= "10"
于 2012-12-04T18:14:06.737 回答
0

问题是因为查询中没有表:

您似乎忘记在查询中包含该表。

于 2014-07-18T09:23:51.393 回答