Subtraction between two sql queries

前端 未结 11 2060
情歌与酒
情歌与酒 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:34

    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)
    
    0 讨论(0)
  • 2020-12-10 01:36
    select @result = (select count(0) from table1) - (select count(0) from table2)
    
    0 讨论(0)
  • 2020-12-10 01:37

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

    This will return the difference

    SELECT COUNT(Attribute) - COUNT(DISTINCT Attribute) FROM table_name;
    
    0 讨论(0)
  • 2020-12-10 01:44

    The query is like below :

    SELECT (select COUNT(FIRSTNAME) FROM TRMDW.EMPLOYEE1) - (SELECT COUNT(DISTINCT FIRSTNAME) FROM TRMDW.EMPLOYEE1) as difference from dual;
    
    0 讨论(0)
提交回复
热议问题