SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculated for the two columns of the table


Calculated for the two columns of the table

Author
Message
kiasystemsoft
kiasystemsoft
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 79
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

Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12926 Visits: 4077
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;-)
kiasystemsoft
kiasystemsoft
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 79
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
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12926 Visits: 4077
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;-)
Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3272 Visits: 1599
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


kiasystemsoft
kiasystemsoft
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 79
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
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33833 Visits: 16649
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
kiasystemsoft
kiasystemsoft
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 79
of Declare @where use for create Condition on table
kiasystemsoft
kiasystemsoft
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 79
I do not have information to help
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12926 Visits: 4077
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;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search