Calculated for the two columns of the table

  • I have two columns.

    Name column 1 is F1

    Name column 2 is F2

    I would calculate two columns

    And the third field to be shown

    For example :

    F1

    20000

    0

    55000

    0

    0

    11000

    F2

    0

    5000

    0

    60000

    20000

    0

    F3

    20000

    15000

    70000

    -10000

    -30000

    -19000

  • can you explain a bit ? i am not clear as if you are adding or subtracting columns value :hehe:

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Sorry

    I can not speek English well

    I have two fields

    The account is a person

    In each row I have a previous account

    It may be that the debtor or creditor

    If you do not understand the explanation again

  • kiasystemsoft (11/26/2012)


    Sorry

    I can not speek English well

    I have two fields

    The account is a person

    In each row I have a previous account

    It may be that the debtor or creditor

    If you do not understand the explanation again

    thats not a problem unless you made the requirement or problem celar here . please send any examole that when amount wil get added or subtracted ?

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • If this is a simple debit/credit problem, here is one of the way to solve. I have tried creating a sample table called "Account".

    create table Account(id int identity, F1 int, F2 int,F3 int)

    insert into Account(F1,F2)

    values

    (20000,0),

    (0,5000),

    (55000,0),

    (0,60000),

    (0,20000),

    (11000,0)

    select id,F1,F2,(select SUM(F1-f2) from Account where id <=a.id) F3 from Account a

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Thank you for your answer...

    But my issue is not resolved

    Because of this sort must be based on the Id field

    I used the following method

    SET @SQLStr ='DECLARE @F3 bigint=0

    update viwe1 set @f3=f3=@f3+ (f1-f2),

    f5 = case when @f3<0 then ''c''

    when @f3>0 then "b" when @f3=0 then "a" end '+

    @where

    EXEC(@SQLStr);

    But sometimes makes mistakes

    It is right on the main table

    But the mistaken view

  • That will always make mistakes since you haven't declared @where.

    To solve your problem, please search this site for "running totals". You should be able to find something that you can adapt to your situation.

    If there's anything you don't understand after that, please post back with table DDL, sample data in the form of INSERT statements, and expected results.

    John

  • of Declare @where use for create Condition on table

  • I do not have information to help

  • kiasystemsoft (11/26/2012)


    I do not have information to help

    we dont require any info from your side , just follow what John Mitchell-245523 explained above. and take help from any person who can make you understand all above stuff (from english perspective)

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

Viewing 10 posts - 1 through 9 (of 9 total)

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