Sql server 如何修复此错误“为“piv”多次指定了列“DistrictID”

Sql server 如何修复此错误“为“piv”多次指定了列“DistrictID”,sql-server,join,pivot-table,inner-join,Sql Server,Join,Pivot Table,Inner Join,我想显示检查员姓名、检查员职位、地区和项目,检查员根据我使用pivot的月份进行检查,但我得到 为“piv”多次指定了列“DistrictID”。这个错误 请帮我克服这个错误 Declare @SQLQuery nvarchar(MAX) If(OBJECT_ID('tempdb..#TBL1') Is Not Null) Begin Drop Table #TBL1 End CREATE TABLE #TBL1 ( ID int, InspPost nvarchar (MAX),

我想显示检查员姓名、检查员职位、地区和项目,检查员根据我使用pivot的月份进行检查,但我得到 为“piv”多次指定了列“DistrictID”。这个错误

请帮我克服这个错误

Declare @SQLQuery nvarchar(MAX)
If(OBJECT_ID('tempdb..#TBL1') Is Not Null)
Begin
    Drop Table #TBL1
End

CREATE TABLE #TBL1
(

 ID int,
InspPost nvarchar (MAX),
InspPostHin nvarchar(MAX)

)
SET @SQLQuery ='INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (1, N''Child Development Project Officer'', N''??? ????? ???????? ?????????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (2, N''Lady Superviser'', N''????? ????????????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (3, N''Other'', N''???? ?????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (4, N''District Program Officer'', N''???? ????????? ?????????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (5, N''J.P.C/State Level Officer'',N''??.??.??../???? ???????? ????? ?????? ???????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (6, N''S.P.M.U/Technical Consultant'', N''??.??..??.??. - ?????? ???????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (7, N''District Coordinator'', N''???? ???????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (8, N''Project Coordinator'', N''?????? ???????'')
INSERT into #TBL1 ([ID], [InspPost], [InspPostHin]) VALUES (9, N''Swasth Bharat Prerak'', N''?????? ???? ??????'')'

exec (@SQLQuery)
select * from 
(
    select Districtmaster.DistrictID,ProjectMaster.ProjectID,Districtmaster.DistrictNameHn,ProjectMaster.ProjectNameHn from Districtmaster Districtmaster 
    inner join ProjectMaster ProjectMaster on Districtmaster.DistrictID=ProjectMaster.DistID

) a1
inner join
(
  select Supervision_Checklist.ID,Supervision_Checklist.Inspector_Name,
  Supervision_Checklist.DistrictID,Supervision_Checklist.ProjectID,
  Supervision_Checklist.Inspector_Type,(#TBL1.InspPost) as inptype ,Supervision_Checklist.Month
  from Supervision_Checklist Supervision_Checklist 
  inner join #TBL1 #TBL1 
  on Supervision_Checklist.Inspector_Type=#TBL1.ID
) src on a1.DistrictID=src.DistrictID and a1.ProjectID=src.ProjectID 
pivot (count(id) for Month in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) piv
我想要的结果如下

透视有效地对当前在结果集中且透视中未提及的所有列执行GROUP BY

如果您进行了一点连接,很可能最终会得到多个同名的列DistrictID。即使我们知道每一行上的两个DistrictID值都相等,优化器却不这样做,并生成此错误

我们需要做的是将不希望包含在此分组操作中的列投射出去。但是没有一种方便的方式来表达这一点。我们需要有一个SELECT子句,我们需要枢轴位于该SELECT子句的另一侧

通常,我们会使用子查询或CTE执行此操作:

;With AllResults as (
    select
      id,
      month,
      /* We cannot use * here. We need only those columns needed by the pivot
         or which should appear in the final result */
    from 
    (
        select Districtmaster.DistrictID,ProjectMaster.ProjectID,Districtmaster.DistrictNameHn,ProjectMaster.ProjectNameHn from Districtmaster Districtmaster 
        inner join ProjectMaster ProjectMaster on Districtmaster.DistrictID=ProjectMaster.DistID

    ) a1
    inner join
    (
      select Supervision_Checklist.ID,Supervision_Checklist.Inspector_Name,
      Supervision_Checklist.DistrictID,Supervision_Checklist.ProjectID,
      Supervision_Checklist.Inspector_Type,(#TBL1.InspPost) as inptype ,Supervision_Checklist.Month
      from Supervision_Checklist Supervision_Checklist 
      inner join #TBL1 #TBL1 
      on Supervision_Checklist.Inspector_Type=#TBL1.ID
    ) src on a1.DistrictID=src.DistrictID and a1.ProjectID=src.ProjectID 
)
select *
from AllResults
pivot (count(id) for Month in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) piv

我假设结果的最后12列是由pivot生成的1-12列。请翻译前5列标题好吗?它们与此处显示的查询的匹配方式对所有人来说都不一定很明显。顺便说一句,声明与表名相同的别名是毫无意义的。别名的目的是为对象提供简洁的名称。因此,对于监督检查表来说,一个好的别名可能是SC.@Damien_The_unsiver districtid和projectid是检查发生的地区和项目,检查员类型是检查员的职位,检查员名称是inspector@Larnu谢谢你的建议。。但我的错误是另一回事。如果你能帮我解决这个问题,那就太好了……我的评论是很有帮助的建议,这将帮助你在将来编写更好的SQL,而不是回答这个问题。如果您正在学习或使用SQL,则必须编写可读且格式良好的SQL。没有太长的表名,这实际上就是您在这里所做的,并且缺少空格和换行符会使您的SQL比应该的更难阅读,这将使志愿者无法回答您的问题。AllResults是此查询第一行中引入的CTE。