subtraction

  • Hi,

    I have a table 'aaa' with column 'a1' and another table 'bbb' with column 'b1'.I wanted to compute the value a1-b1.How can i do that,

    Thanks.

  • Assuming two table have common columns ID.

    select aaa.a1 - bbb.b1 as Result

    from aaa

    inner join bbb

    on aaa.id = bbb.id

  • I dont have a ID column,I have only one column each on the two tables.

  • Do they have same number of records? Create identity column in each tables and run the query above.

  • You could add an id field to each table like this, so you could do the join.

    create table aaa (a1 int)

    create table bbb (b1 int)

    insert into aaa values (6)

    insert into aaa values (12)

    insert into aaa values (18)

    insert into aaa values (24)

    insert into bbb values (1)

    insert into bbb values (2)

    insert into bbb values (3)

    insert into bbb values (4)

    go

    alter table aaa add [id] int identity

    go

    alter table bbb add [id] int identity

    go

    select aaa.a1 - bbb.b1 as Result

    from aaa

    inner join bbb

    on aaa.[id] = bbb.[id]

    alter table aaa drop column [id]

    alter table bbb drop column [id]

    drop table aaa

    drop table bbb

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • In general : never make an assumption about the sequence in which data will be returned from a query.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply