SQL 计算行数和内连接

SQL Count Lines and Inner join

我有这个问题:

select s.LastState
      ,count(s.LastState) as sumS
from table1 t1
    join table2 t2
        on t1.ID = t2.ID
    join (select LastState
                ,count(LastState) as sum
          from table1
          where ID = X
            and LastState = 1
             or LastState = 2
          group by LastState
         ) s
        on s.LastState = t1.LastState
group by s.LastState

这 returns 两个状态的数量,我想要两个状态的总和。

目前我看到第一行让我们承认

我的 ID X 有 10 个状态 1 和 5 个状态 2

我希望看到 15(两个州的计数总和)。

select --s.LastState
      LastState='LastState1and2'
      , count(s.LastState) as sumS
from table1 t1
    join table2 t2
        on t1.ID = t2.ID
    join (select LastState
                ,count(LastState) as sum
          from table1
          where ID = X
            and LastState = 1
             or LastState = 2
          group by LastState
         ) s
        on s.LastState = t1.LastState
--group by s.LastState