我收到此错误
信息8156,第16层,状态1,第67行
多次为“ cte”指定了“ MANDT”列。”
当尝试运行下面的代码时,但是我没有在查询中包括列MANDT
。我要调用的两个表都具有列MANDT
,但它们也都具有列STAT
,并且另一个表尝试进行相同的联接时我没有问题,唯一的问题是该表没有MANDT
,只有STAT
是相同的。
我尝试同时包含别名为MANDT
和JCDS_SOGR.MANDT as Client
的两个列TJ30T.MANDT as Client2
,但并没有成功。得到了相同的错误消息。
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY STAT ORDER BY UDATE) AS Rn,
*,
LAG(UDATE) OVER (PARTITION BY STAT ORDER BY UDATE) AS PrevUDate,
COUNT(*) OVER (PARTITION BY STAT) AS [Count]
FROM
JCDS_SOGR
JOIN
TJ30T on JCDS_SOGR.STAT = TJ30T.ESTAT
WHERE
OBJNR = 'IE000000000010003137'
)
SELECT
MAX(rn) AS [Count],
OBJNR, STAT, TXT30,
SUM(CASE
WHEN rn % 2 = 0
THEN DATEDIFF(d, PrevUDate, UDATE)
WHEN rn = [Count]
THEN DATEDIFF(d, UDATE, GETDATE())
ELSE 0
END) AS DIF
FROM
cte
GROUP BY
OBJNR, STAT, TXT30
这是我提到的另一个查询,该查询在相同的代码下也可以正常工作。
;with cte
AS
(
select ROW_NUMBER() OVER(partition by STAT Order by UDATE ) as Rn
, *
, LAG(UDATE) OVER(partition by STAT Order by UDATE ) As PrevUDate
, COUNT(*) OVER(partition by STAT) As [Count]
from JCDS_SOGR
join TJ02T on JCDS_SOGR.STAT = TJ02T.ISTAT
where OBJNR = 'IE000000000010003137'
and TJ02T.SPRAS = 'E'
)
select Max(rn) As [Count]
, OBJNR,STAT,TXT30
, SUM(CASE WHEN rn%2=0 THEN DATEDIFF(d,PrevUDate,UDATE)
WHEN rn=[Count] THEN DATEDIFF(d,UDATE,getDate())
ELSE 0 END) as DIF
from cte
group BY OBJNR, STAT,TXT30
预期结果是
[COUNT OBJNR STAT TXT30 DIF
1 IE000000000010003137 I0099 Available 2810][1]
答案 0 :(得分:1)
在CTE中,您选择*。因此,如果有两个名为MANDT的列,则可能会导致冲突。去掉 *。那应该可以解决您描述的问题。