SQL two field subtraction statements

Source: Internet
Author: User
Keywords Network programming Database synthesis
Tags codes create function network network programming programming searches select

SQL two field subtraction statements
This article searches for a large number of SQL two field subtraction statements and function codes from the network, illustrating two field subtraction practices.

Select a. field 1, Field 2=a. Field 2-isnull ((select field 2 from Table 2 where a. Field 1 = Field 1), 0) from table 1 a method two

CREATE TABLE Table 1 (Field 1 varchar (50), field 2 int)
Insert INTO Table 1
Select ' 111001 ', UNION ALL
Select ' 111002 ', 9 UNION ALL
Select ' 111003 ', UNION ALL
Select ' 111004 ', 23

CREATE TABLE Table 2 (Field 1 varchar (50), field 2 int)
INSERT INTO Table 2
Select ' 111001 ', 3 UNION ALL
Select ' 111002 ', 2 UNION ALL
Select ' 111003 ', 12

Select a. Field 1, (a.[Field 2]-(IsNull (b.[field 2],0)) As Field 2 from table 1 a LEFT JOIN table 2 b
On a.[field 1] = b.[Field 1]

Result: Field 1 Field 2
111001 7
111002 7
111003 0 (not shown)
111004 23

Method Three


There are two identical tables T1 and T2, all with field a (character), B (numeric), C (numeric), as follows:
T1 T2
A b c a B C
A1 A2 2 10
A2 A3 3 15
A3 10 20
A4 10 20

Select T1.a as A, (T1.b-isnull (t2.b,0)) as B, (T1.c-isnull (t2.c,0)) as C from [T1] left join [T2] on (t1.a = t2.a)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.