Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Calculated for the two columns of the table Expand / Collapse
Author
Message
Posted Monday, November 26, 2012 12:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 11:05 PM
Points: 18, Visits: 33
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

Post #1388453
Posted Monday, November 26, 2012 12:55 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 14, 2014 4:48 AM
Points: 2,834, Visits: 3,950
can you explain a bit ? i am not clear as if you are adding or subtracting columns value

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1388456
Posted Monday, November 26, 2012 1:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 11:05 PM
Points: 18, Visits: 33
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
Post #1388464
Posted Monday, November 26, 2012 2:23 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 14, 2014 4:48 AM
Points: 2,834, Visits: 3,950
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
Post #1388475
Posted Monday, November 26, 2012 3:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
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

Post #1388488
Posted Monday, November 26, 2012 3:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 11:05 PM
Points: 18, Visits: 33
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
Post #1388500
Posted Monday, November 26, 2012 3:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:53 AM
Points: 5,228, Visits: 9,443
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
Post #1388504
Posted Monday, November 26, 2012 4:10 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 11:05 PM
Points: 18, Visits: 33
of Declare @where use for create Condition on table
Post #1388509
Posted Monday, November 26, 2012 5:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 11:05 PM
Points: 18, Visits: 33
I do not have information to help
Post #1388562
Posted Monday, November 26, 2012 10:39 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 14, 2014 4:48 AM
Points: 2,834, Visits: 3,950
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
Post #1388942
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse