我在制作此内部联接查询时遇到了麻烦
原件:
SELECT *
FROM (
SELECT *, row_number() OVER (PARTITION BY idmicro ORDER BY IdHistMov desc) AS rownum
FROM dbo.T_HistMovEquip
) AS initialResultSet
WHERE initialResultSet.rownum<=2
尝试内连接:
SELECT *
FROM (
SELECT *, row_number() OVER (PARTITION BY c.idmicro ORDER BY c.IdHistMov desc) AS rownum
FROM dbo.T_HistMovEquip c
INNER JOIN T_Micro m ON c.Idmicro=m.Idmicro
) AS initialResultSet
WHERE initialResultSet.rownum<=2
出现此错误:列&#39; IDMicro&#39;已经多次为&quot; initialResultSet&#39;。
指定谢谢!
答案 0 :(得分:0)
为什么不只指定所需的列。像这样:
SELECT *
FROM (
SELECT
c.idmicro,
c.IdHistMov,
/*And so on*/
row_number() OVER (PARTITION BY c.idmicro ORDER BY c.IdHistMov desc) AS rownum
FROM
dbo.T_HistMovEquip c
INNER JOIN T_Micro m
ON c.Idmicro=m.Idmicro
) AS initialResultSet
WHERE initialResultSet.rownum<=2
答案 1 :(得分:0)
那是因为你明确地在两个表中都有相同的列名(IDMicro)时使用Select *
。尝试将输出选择“过滤”为仅您感兴趣的列,或者如果您需要两个表中的相同列名,请使用列别名。