【问题标题】:How to fix this error 'The column 'DistrictID' was specified multiple times for 'piv'如何修复此错误“为“piv”指定了多次“区域 ID”列
【发布时间】:2019-04-08 13:07:33
【问题描述】:

我想显示检查员姓名、检查员职位、地区和项目,检查员根据我正在使用枢轴但我得到的月份进行检查 “为‘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

我想要的结果如下...

enter image description here

【问题讨论】:

  • 我假设您的结果的最后 12 列是 pivot 生成的 1-12 列。您能否翻译前 5 个列标题(它们如何与此处显示的查询匹配并不一定对所有人都很明显)
  • 声明一个与你的表名相同的别名是没有意义的。别名的意义在于为您的对象提供一个简洁的名称。所以对于Supervision_Checklist,一个好的别名可能是SC
  • @Damien_The_Unbeliever Districtid 和 projectid 是检查发生的地区和项目,检查员类型是检查员的职位,检查员姓名是检查员的姓名
  • @Larnu 感谢您的建议.. 但我的错误是别的。如果你能帮我解决这个问题,那就太好了......
  • 我的评论是很有帮助的建议,它将帮助您将来编写更好的SQL,而不是回答问题。如果您正在学习或使用 SQL,则必须编写可读且格式良好的 SQL。不是长表名的别名(这实际上是您在此处所做的)并且缺少空格和换行符会使您的 SQL 比应有的更难阅读,这将使志愿者无法回答您的问题。

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


【解决方案1】:

PIVOT 有效地对 当前在结果集中在数据透视表中提及的所有列执行 GROUP BY

如果您已经加入了一些操作,那么您很可能(如这里)最终会得到多个同名的列 (DistrictID)。即使我们知道DistrictID 的值在每一行上都是相等的,但优化器却不会,并会生成此错误。

我们需要做的是投影掉我们不希望包含在此分组操作中的列。但是没有方便的方式来表达这种“内联”。我们需要有一个SELECT 子句,并且我们需要PIVOT 位于该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

【讨论】:

  • AllResults 是在此查询的第一行引入的 CTE。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
相关资源
最近更新 更多