How to fix this error 'The column 'DistrictID' was specified multiple times for 'piv'

时间:2019-04-08 13:07:33

标签: sql-server join pivot-table inner-join

I want to show the inspector name, inspector post, district and project with inspection done by the inspector according to months for which i'm using pivot but i'm getting "The column 'DistrictID' was specified multiple times for 'piv'." this error...

Please help me to get over this error

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

i want result as following...

enter image description here

1 个答案:

答案 0 :(得分:0)

A PIVOT effectively performs a GROUP BY on all columns which are currently in the result set and which aren't mentioned in the pivot.

If you've done a bit of joining, it's highly likely (as here) that you'll end up with multiple columns of the same name (DistrictID). Even though we know that both DistrictID values are equal on every row, the optimizer doesn't, and generates this error.

What we need to do is project away the columns that we don't want included in this grouping operation. But there's no convenient way to express this "inline". We need to have a SELECT clause and we need the PIVOT to be on the "other side" of that SELECT clause.

Typically, we'd do this using a subquery or 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