如何在SQL中添加统计值

时间:2015-06-29 05:07:04

标签: sql

现在我的数据表就像

uid mid  ..
1   xx   ..
2   xx   ..
-   a    ..
-   b    ..
...

我写了两个SQL查询:

select count(distinct uid) from T where uid!='-'select count(distinc mid) from T where uid='-'

现在我想在一个SQL查询中获得这两个值的总和。有人有任何想法帮助我吗?

2 个答案:

答案 0 :(得分:2)

尝试此查询

select count(distinct uid) from T where uid!='-'
union all
select count(distinc mid) from T where uid='-'

答案 1 :(得分:2)

你可以这样:

 even
      2      4      6      8     10     12     14
 lessThan10
      1      2      3      4      5      6      7      8      9
 divBy3
      3      6      9     12
 notDivBy3
      1      2      4      5      7      8     10     11     13     14
End

SELECT (SELECT count(DISTINCT uid) FROM T WHERE uid <> '-') +
       (SELECT count(DISTINCT mid) FROM T WHERE uid = '-') As sumOfTwo

或[推荐]

;WITH t1 AS (
    SELECT count(DISTINCT uid) cnt FROM T WHERE uid <> '-'
), t2 AS (
    SELECT count(DISTINCT mid) cnt FROM T WHERE uid = '-'
)
SELECT t1.cnt + t2.cnt As sumOfTwo
FROM t1 CROSS JOIN t2