两个sql查询之间的减法 [英] Subtraction between two sql queries

查看:54
本文介绍了两个sql查询之间的减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MS SQL 中有 2 个查询,它们使用 COUNT 函数返回许多结果.

I have 2 queries in MS SQL that return a number of results using the COUNT function.

我可以运行第一个查询得到第一个结果,然后运行另一个查询得到另一个结果,减去它们并找到结果;但是有没有办法组合所有 3 个函数并获得 1 个整体结果

I can run the the first query and get the first result and then run the other one to get the other result, subtract them and find the results; however is there a way to combine all 3 functions and get 1 overall result

如:run sql1 run sql2 run SQL3 (sql1-sql2)?....

As in: run sql1 run sql2 run SQL3 (sql1-sql2)?....

我尝试使用 xxxx 作为函数,但没有成功.

I tried them with xxxx as a function but no luck.

推荐答案

您应该能够为此使用子查询:

You should be able to use subqueries for that:

SELECT
    (SELECT COUNT(*) FROM ... WHERE ...)
  - (SELECT COUNT(*) FROM ... WHERE ...) AS Difference

刚刚测试了一下:

Difference
-----------
45

(1 row(s) affected)

这篇关于两个sql查询之间的减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆