计算 SQL 结果的差异

Count the difference in SQL result

我有一个table这样的。

+-------------+-------+
|    vtype    | isreq |
+-------------+-------+
| Near        |     0 |
| Near        |     1 |
| Far         |     0 |
| Near to far |     0 |
+-------------+-------+

我想获得 isreq 不同但类型相同的号码。有人要吗?

这将满足您的要求。尝试测试是否有更多数据。

select count(*) from
    yourTable a 
        inner join 
    yourTable b
on a.vtype=b.vtype
and a.isreq < b.isreq
group by a.vtype,a.isreq

在 MySQL

上查看 fiddle 演示

http://sqlfiddle.com/#!9/b51bf8/11

我得到了我需要的东西。

DECLARE @table as table(vtype varchar(15),isreq varchar(5))

insert into @table values ('Near','0')
insert into @table values ('Near','1')
insert into @table values ('Far','0')
insert into @table values ('Near to Far','0')

SELECT count(*) FROM @table WHERE vtype = vtype and isreq = isreq 
SELECT DISTINCT(COUNT(*)-1) AS result FROM @table WHERE vtype = vtype and isreq = isreq GROUP BY vtype HAVING (COUNT(*)-1)>0