我试图以格式
获取数据的水平输出查询是:
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"
当我执行此查询时,我收到了回复
Msg 4104,Level 16,State 1,Line 1
无法绑定多部分标识符“Solution_id”。
Solution_id
是Result_Distribution_Table
表中的一列。
请帮助我做错了什么,解决方案是什么?
答案 0 :(得分: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次。 如果是这种情况,您可以获得与<; p>相同的结果
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"
答案 1 :(得分:0)
问题是因为查询中没有表格:
您似乎忘了在查询中包含该表。