2017-09-28 40 views
0

希望有人比我聰明能幫助我在這裏...收到錯誤「具有相同的鍵SSRS項目已被添加」在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 
+0

可能重複[在SSRS中,爲什麼我得到錯誤「項目使用相同的鍵已被添加」,當我正在做一個新的報告?](https://stackoverflow.com/questions/14466874/在-SSRS-爲什麼-DO-I-GET-的誤差項與 - 相同的琴鍵具有-已經被添加-WH) – ViKiNG

回答

0

你有兩個字段稱爲「A類」 - 一個「公司」後一個「MEMBER_TYPE」之後。 SSRS將只允許唯一的列名稱。

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看起來是重複的,所以答案可能只是刪除第二個,而不是別名。

相關問題