已添加相同密钥的项目(SQL)

时间:2015-06-22 13:39:30

标签: sql visual-studio reporting-services

我正在编写一些SQL问题。虽然我知道错误意味着我已经两次引用相同的列,但我似乎无法找到它。它在SQL Server Management中运行良好,但是当我尝试将其放入Visual Studio以从中创建报表时,它会给我错误。

这是我的代码:

SELECT 
    TC.CorrespondanceName1
    , LC.Address1
    , LC.Address2
    , LC.Address3
    , LC.Address4
    , LC.PostCode
    , TC.Telephone
    , (DATEDIFF(yy,PC.DateofBirth,GetDate()) - (CASE WHEN GetDate() < DATEADD(yy,DATEDIFF(yy,PC.DateofBirth,GetDate()), PC.DateofBirth) THEN 1 ELSE 0 END)) AS Age
    , CASE WHEN PAC.AttribCode = 'CON1' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Telephone]
    , CASE WHEN PAC.AttribCode = 'CON2' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Post]
    , CASE WHEN PAC.AttribCode = 'CON3' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Email]
    , CASE WHEN PAC.AttribCode = 'CON4' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Text]
    , CASE WHEN PAC.AttribCode = 'CON5' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Facebook]
    , CASE WHEN PAC.AttribCode = 'CON6' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Website]
    , CASE WHEN PAC.AttribCode = 'CON7' AND PAC.LogValue = 1 THEN 'Yes' ELSE 'No' END AS [Meeting]
FROM 
    vwTenancyCurrent TC 
INNER JOIN
    vwLocationCurrent LC ON LC.PlaceRef = TC.PlaceRef 
INNER JOIN
    vwTenancyPersonCurrent TPC ON TPC.TenancySystemRef = TC.TenancySystemRef 
INNER JOIN
    vwPersonCurrent PC ON PC.PersonRef = TPC.PersonRef 
INNER JOIN
    vwPersonAttributesCurrent PAC ON PAC.PersonRef = PC.PersonRef 
WHERE 
    TC.TenancyStatus <> 'FOR'
    AND PC.DateofBirth <> ''

我已经做了一些挖掘,但错误肯定与我的Case语句有关,但我之前使用过Case语句,从来没有遇到过这个问题。

有人可以帮忙吗?

由于

2 个答案:

答案 0 :(得分:3)

/** * @param \Acme\Demo\Domain\Model\CoffeeBean $coffeeBean */ public function update($coffeeBean) { \TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed before"); parent::update($coffeeBean); \TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed after"); } 列在您的查询中出现两次。

SSMS可以处理具有多个相同列名的查询,但您无法在报告中使用此类查询。
这是设计使然,列名用作键。

重命名其中一个,问题就会消失。

答案 1 :(得分:2)

TC.Telephone,
, CASE WHEN PAC.AttribCode = 'CON1' AND PAC.LogValue = 1 
         THEN 'Yes' ELSE 'No' 
  END AS `[Telephone]`

将第二个别名更改为另一个名称Telephone_home等等。然后您的代码将起作用