简体   繁体   中英

how to make inner join in this query

I'm in trouble when making this inner join query

Original:

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

Trying to inner join:

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

this error appears: The column 'IDMicro' was specified multiple times for 'initialResultSet'.

Thank You!

Why do you not just specify the columns you want. Like this:

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

That's because your using Select * when clearly you have the same column name ( IDMicro ) in both tables. Try to "filter" the output selection to only the columns you are interested in OR use column aliases if you need the same column names from both tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM