How do i compare 2 rows from the same table (SQL Server)

后端 未结 6 568
太阳男子
太阳男子 2020-12-02 16:15

I need to create a background job that processes a table looking for rows matching on a particular id with different statuses. It will store the row data in a string to comp

6条回答
  •  春和景丽
    2020-12-02 16:47

    You can join a table to itself as many times as you require, it is called a self join.

    An alias is assigned to each instance of the table (as in the example below) to differentiate one from another.

    SELECT a.SelfJoinTableID
    FROM   dbo.SelfJoinTable a
           INNER JOIN dbo.SelfJoinTable b
             ON a.SelfJoinTableID = b.SelfJoinTableID
           INNER JOIN dbo.SelfJoinTable c
             ON a.SelfJoinTableID = c.SelfJoinTableID
    WHERE  a.Status = 'Status to filter a'
           AND b.Status = 'Status to filter b'
           AND c.Status = 'Status to filter c' 
    

提交回复
热议问题