简体   繁体   中英

Join two tables returning only one row from the second table

I have this query:

SELECT   t_ticket.ticketID, t_ticket.addedDate, t_ticket.question,
t_ticket.code, t_ticket.priority, t_actionTicket.addedDateAction, t_actionTicket.title 

FROM            t_actionTicket INNER JOIN
                     t_ticket ON t_actionTicket.ticketID_FK = t_ticket.ticketID INNER JOIN
                         (SELECT        ticketID_FK, MAX(addedDateAction) AS maxDate
                            FROM            t_actionTicket AS t_actionTicket_1
                            WHERE        (t_actionTicket.userID_FK <> @userid)
                            GROUP BY ticketID_FK) AS b ON t_actionTicket.ticketID_FK = b.ticketID_FK AND t_actionTicket.addedDateAction = b.maxDate
WHERE        (t_ticket.supporterID_FK IN
                         (SELECT        supporterID
                            FROM            t_Supporter
                            WHERE        (userID_FK = @userid)))

I want to return just the latest record in t_actionTicket table for each row in t_ticket table that t_actionTicket.userID_FK <> @userid. but I have this error:

The multi-part identifier "t_actionTicket.userID_FK" could not be bound.

You can write this logic using row_number() instead of additional nested queries:

SELECT t.ticketID, t.addedDate, t.question, t.code, t.priority,
       ta.addedDateAction, ta.title AS Expr1
FROM  t_Ticket t INNER JOIN
      (SELECT ta.*,
              ROW_NUMBER() OVER (PARTITION BY ta.ticketID_FK ORDER BY ta.addedDateAction DESC) as seqnum
       FROM t_actionTicket ta
      ) ta
      ON t.ticketId = ta.ticketId_FK and ta.seqnum = 1
WHERE t.supporterID_FK IN (SELECT supporterID
                           FROM t_Supporter
                           WHERE userID_FK = @userid
                          );

Note that table aliases make the query easier to write and to read.

Problem in your query is

FROM            t_actionTicket AS t_actionTicket_1
WHERE        t_actionTicket.userID_FK <> @userid -- here

You cannot use t_actionTicket alias name inside inner join select query. You need to use t_actionTicket_1 . It is possible only in sub-query

Try this better way of doing it

;WITH cte
     AS (SELECT t_ticket.ticketID,
                t_ticket.addedDate,
                t_ticket.question,
                t_ticket.code,
                t_ticket.priority,
                t_actionTicket.addedDateAction,
                t_actionTicket.title,
                Row_number()
                  OVER(
                    partition BY ticketID_FK
                    ORDER BY addedDateAction DESC) RN
         FROM   t_actionTicket
                INNER JOIN t_ticket
                        ON t_actionTicket.ticketID_FK = t_ticket.ticketID
         WHERE  t_ticket.supporterID_FK IN (SELECT supporterID
                                            FROM   t_Supporter
                                            WHERE  userID_FK = @userid))
SELECT *
FROM   cte
WHERE  rn = 1 

Try this query

SELECT   t_ticket.ticketID, t_ticket.addedDate, t_ticket.question,
t_ticket.code, t_ticket.priority, t_actionTicket.addedDateAction, t_actionTicket.title 

FROM            t_actionTicket
 INNER JOIN t_ticket ON t_actionTicket.ticketID_FK = t_ticket.ticketID 
 INNER JOIN (SELECT        ticketID_FK, MAX(addedDateAction) AS maxDate
                            FROM            t_actionTicket AS t_actionTicket_1
                            WHERE        (t_actionTicket_1.userID_FK <> @userid)
                            GROUP BY ticketID_FK) AS b ON t_actionTicket.ticketID_FK = b.ticketID_FK AND t_actionTicket.addedDateAction = b.maxDate

WHERE        (t_ticket.supporterID_FK IN
                         (SELECT        supporterID
                            FROM            t_Supporter
                            WHERE        (userID_FK = @userid)))

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