Subtraction between two sql queries
Asked Answered
S

11

36

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

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

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

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

Sondra answered 19/10, 2009 at 14:41 Comment(2)
Tell us a little more about the structure of the tables and what data you want to get. Maybe the queries can be re-written to select what you want with only one quesry.Wreath
If you don't give table structures, sample data, expected output, how do you think we are suppose to help you????Broccoli
S
59

You should be able to use subqueries for that:

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

Just tested it:

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

(1 row(s) affected)
Specht answered 19/10, 2009 at 14:44 Comment(4)
Hi, I tried this method and it gives me ERROR 1242 (21000) at line 4: Subquery returns more than 1 row. Any suggestions?Stocky
Yes. Make sure that you only get a single row back, otherwise you cannot use it as a scalar. The error message is fairly clear in that regard. Maybe you should check what the difference between your problem and the one stated in the question is.Specht
I'm solving the same question and this is my query: SELECT (SELECT COUNT(CITY) FROM STATION ) - (SELECT COUNT(CITY) FROM STATION GROUP BY (CITY)) AS DifferenceStocky
Do you perchance spot an additional group by in your query? Did you try the sub-queries on their own? Have you noticed something that might cause them to be unable to be treated as a scalar?Specht
P
30
SELECT (SELECT COUNT(*) FROM t1) - (SELECT COUNT(*) FROM t2)
Purely answered 19/10, 2009 at 14:46 Comment(4)
THANKS this solves it:)...the simplest thing...ahhh sometimesSondra
Thanks! It was so simple, yet all over the web people are suggesting doing extravagant SQL logic to get it when a simple minus suffices.Beaman
also adding DISTINCT to the query, this is how you would do it SELECT (SELECT COUNT(CITY) FROM STATION) - (SELECT COUNT(DISTINCT CITY) FROM STATION)Hollingshead
@Hollingshead what a spoiler of the HackerRank challenge ;)Umberto
I
16

This will return the difference

SELECT COUNT(Attribute) - COUNT(DISTINCT Attribute) FROM table_name;
Interbreed answered 26/11, 2019 at 21:41 Comment(1)
I believe that this answer is not correct. Perhaps I have misunderstood something. Could you elaborate a bit?Petigny
I
5

I know this is an old post but here is another solution that fit best to my needs (tested on firebird)

SELECT c1-c2 from (select count(*) c1 from t1), (SELECT COUNT(*) c2 from t2);
Ilyse answered 9/12, 2016 at 8:12 Comment(0)
A
3

The query is like below :

SELECT (select COUNT(FIRSTNAME) FROM TRMDW.EMPLOYEE1) - (SELECT COUNT(DISTINCT FIRSTNAME) FROM TRMDW.EMPLOYEE1) as difference from dual;
Arsphenamine answered 3/10, 2018 at 13:42 Comment(0)
M
2

This can be done in a single query:

SELECT COUNT(col_name) - COUNT(DISTINCT col_name) as Difference from table_name;
Moskow answered 24/3, 2020 at 9:1 Comment(1)
Hi Sanjay. True, but you're only echoing another answer here which was posted some time ago. Please do not echo existing answers.Artefact
I
1

Just create an inline function with your query logic, and have it return the result. Pass in parameters as needed.

Inenarrable answered 19/10, 2009 at 14:44 Comment(0)
B
1
select @result = (select count(0) from table1) - (select count(0) from table2)
Baeyer answered 19/10, 2009 at 14:45 Comment(0)
V
1
SELECT
   t1.HowManyInTable1
  ,t2.HowManyInTable2
  ,t1.HowManyInTable1 = t2.HowManyInTable2  Table1_minus_Table2
 from (select count(*) HowManyInTable1 from Table1) t1
  cross join (select count(*) HowManyInTable2 from Table2) t2
Vociferate answered 19/10, 2009 at 14:51 Comment(0)
S
1
SELECT (count(*) from t1) - (count(*) from t2);

this worked for me.

Also if there is only one table you can also do:

SELECT (count(column1)) - count(column2)) from table; 
Sail answered 28/3, 2018 at 0:43 Comment(0)
S
0

The query is like below :

((SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(m,'/',2),'/',-1) 
FROM ms WHERE ms.id=t.m_id)-(SELECT COUNT(id) FROM t AS tr WHERE tr.m_id=t.m_id)) AS remaining
Signalize answered 22/10, 2017 at 7:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.