使用相同的密钥获取错误" SSRS项目已添加"在SQL Server 2014中

时间:2017-09-28 16:14:58

标签: reporting-services sql-server-2014

希望比我聪明的人可以帮助我...

我收到的错误SSRS项目已经添加了相同的密钥以下SQL我试图澄清代码的问题,但是还没有能够找到它。

有人可以帮助我:)

SELECT 
    Name.ID, Name.COMPANY, Name.CATEGORY, 
    HOUSEPOP.HH_INV AS 'Current Year HH_INV',
    HOUSEPOP_1.HH_INV AS 'Last Year HH_INV', 
    name.MEMBER_TYPE, name.CATEGORY, 
    HOUSEPOP.TOTAL_POP,
    (HOUSEPOP.HH_INV - HOUSEPOP_1.HH_INV) AS 'Difference', 
    ((HOUSEPOP.HH_INV - HOUSEPOP_1.HH_INV)/HOUSEPOP_1.HH_INV) AS '% Change'
FROM            
    Name 
INNER JOIN
    HOUSEPOP ON Name.ID = HOUSEPOP.ID 
INNER JOIN
    HOUSEPOP AS HOUSEPOP_1 ON Name.ID = HOUSEPOP_1.ID
WHERE
    HOUSEPOP.BILL_YEAR = @Bill_Year 
    AND HOUSEPOP_1.BILL_YEAR = @Bill_Year -1
ORDER BY   
    ID

2 个答案:

答案 0 :(得分:1)

您有2个CATEGORY列。 SQL Server / SSMS将允许查询中的重复列,但SSRS不能具有重复项。你需要别名。

SELECT 
        Name.ID, 
        Name.COMPANY, 
        Name.CATEGORY,                --CATEGORY column 1 here
        HOUSEPOP.HH_INV AS 'Current Year HH_INV', 
        HOUSEPOP_1.HH_INV AS 'Last Year HH_INV', 
        name.MEMBER_TYPE, 
        name.CATEGORY AS 'CATEGORY2', --I aliased your 2nd CATEGORY column here
        HOUSEPOP.TOTAL_POP,
        (HOUSEPOP.HH_INV - HOUSEPOP_1.HH_INV) as 'Difference',
        ((HOUSEPOP.HH_INV - HOUSEPOP_1.HH_INV)/HOUSEPOP_1.HH_INV) as '% Change'
FROM           Name 
INNER JOIN     HOUSEPOP 
ON             Name.ID = HOUSEPOP.ID 
INNER JOIN     HOUSEPOP AS HOUSEPOP_1 
ON             Name.ID = HOUSEPOP_1.ID
WHERE          HOUSEPOP.BILL_YEAR = @Bill_Year 
AND            HOUSEPOP_1.BILL_YEAR = @Bill_Year -1
ORDER BY       ID

我还花了一些时间来格式化您的查询,使其可读。这将帮助您调试(至少对我有帮助)。

编辑:现在我再次查看您的查询,CATEGORY似乎是重复的,所以答案可能只是删除第二个而不是别名。

答案 1 :(得分:0)

您有两个名为“类别”的字段 - 一个在“公司”之后,一个在“Member_Type”之后。 SSRS只允许使用唯一的列名称。