SQL计数行和内部联接

马丁·文森特

我有这个查询:

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

这将返回两个状态的数量,我想同时获取两个计数的总和。

目前,我承认我的第一行

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

我想查看15(两个州的计数总和)。

SqlZim
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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章