Subtraction between two sql queries

前端 未结 11 2059
情歌与酒
情歌与酒 2020-12-10 00:54

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 on

相关标签:
11条回答
  • 2020-12-10 01:22
    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; 
    
    0 讨论(0)
  • 2020-12-10 01:23

    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);
    
    0 讨论(0)
  • 2020-12-10 01:26

    This can be done in a single query:

    SELECT COUNT(col_name) - COUNT(DISTINCT col_name) as Difference from table_name;
    
    0 讨论(0)
  • 2020-12-10 01:29
    SELECT (SELECT COUNT(*) FROM t1) - (SELECT COUNT(*) FROM t2)
    
    0 讨论(0)
  • 2020-12-10 01:29

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

    0 讨论(0)
  • 2020-12-10 01:29
    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
    
    0 讨论(0)
提交回复
热议问题