How do I 'subtract' sql tables?

后端 未结 14 1959
死守一世寂寞
死守一世寂寞 2020-12-05 13:12

Its not really a subtraction I\'m looking for. And I know its not a union or intersection... I have been given a long and complex stored procedure that returns a table of

相关标签:
14条回答
  • 2020-12-05 13:55

    All good answers, but missing one point: The questioner (OP) has stored procedures...

    You have to define temporary tables (based on your platform) to load the data

    INSERT ...
    EXEC getActive
    
    INSERT ...
    EXEC getInactive
    

    Then use EXCEPT/EXISTS/MINUS/IN/OUTER JOIN/etc...

    0 讨论(0)
  • 2020-12-05 13:55
    SELECT roll_number FROM profile WHERE(catagory='Attest and Eat' or catagory='Live and Eat') and status='OK' EXCEPT SELECT roll_number from meal_status  WHERE date='29' AND month='1'
    

    You can try this kind of command to subtract a table from another one.

    0 讨论(0)
  • 2020-12-05 13:57

    What's your DB engine?

    In Oracle, you could use MINUS set operation.

    In MS SQLServer 2005 and newer you can use EXCEPT.

    0 讨论(0)
  • 2020-12-05 13:57

    For doing the subtraction between three tables I have used the following query:

    Basically I have three tables.. table 1, table 2, table 3. Firstly I have done the subtraction of table 1 and table 2 and then done the subtraction between the the result of previous query and table 3.

    select v3.Material, ((v1.Qty-v2.Qty)-v3.Qty) as Quantity
      from table1 v1, table2 v2, table3 v3
     where (v1.Material=v2.Material
        and v1.Material=v3.Material
        and v2.Material=v3.Material)
    
    0 讨论(0)
  • 2020-12-05 13:58

    The set operation you are looking for is called MINUS, but in SQL Server the keyword is EXCEPT

      SELECT ... // all documents
      EXCEPT
      SELECT ... // active documents
    

    I believe that the EXCEPT set operation became available in SQL Server 2005.

    0 讨论(0)
  • 2020-12-05 13:59
    select * from MyTable1
    where MyTable1.Field1 not in (
      select Field1 from MyTable2)
    
    0 讨论(0)
提交回复
热议问题