Showing posts with label balance. Show all posts
Showing posts with label balance. Show all posts

Monday, March 19, 2012

How to Take a Column Value to Make it a Field ?

if a customer can have up to 10 acounts and each account has a balance,

in customer_account table, the same custID repeats and relates to an

accountID and each account has its own balance.

How to display each customer in ONE row and list his account one next to another ?

for example:

custID, acct#1, acct#1Balance, acct#2, acct#2Balance, acct#3, acct#3Balance

how to do write SQl to achive it ?

This is chanllenging to me...

Your advise is greatly appreciated !

Most likely, you will find value in the new PIVOT keyword.

This previous thread may help direct you.

|||

just to add what Arnie has said.. PIVIOT is only available in SQL Server 2005.

Madhu

|||

Since you have to pivot more than one column, I recommend to use the Case..When (legacy approach).

Here the sample, (this sample is for SQL Server 2005 only – let me know if you SQL Server 2000)

Code Snippet

Create Table #accounts (

[CustomerId] int ,

[AccountNo] int ,

[Balance] money

);

Insert Into #accounts Values('1','272837282','100000');

Insert Into #accounts Values('1','272837298','20000');

Insert Into #accounts Values('1','272837390','1000');

Insert Into #accounts Values('1','272937282','100000');

Insert Into #accounts Values('2','272437282','10000');

Insert Into #accounts Values('3','572837282','83734937');

Insert Into #accounts Values('3','572837285','28292');

;With Grouped

as

(

Select * ,Row_Number() Over(Partition By CustomerId Order BY CustomerId) [Acct#] from #accounts

)

Select

CustomerId

,Max(Case When [Acct#] =1 Then [AccountNo] End) [acct#1]

,Max(Case When [Acct#] =1 Then [Balance] End) [acct#1Balance]

,Max(Case When [Acct#] =2 Then [AccountNo] End) [acct#2]

,Max(Case When [Acct#] =2 Then [Balance] End) [acct#2Balance]

,Max(Case When [Acct#] =3 Then [AccountNo] End) [acct#3]

,Max(Case When [Acct#] =3 Then [Balance] End) [acct#3Balance]

,Max(Case When [Acct#] =4 Then [AccountNo] End) [acct#4]

,Max(Case When [Acct#] =4 Then [Balance] End) [acct#4Balance]

,Max(Case When [Acct#] =5 Then [AccountNo] End) [acct#5]

,Max(Case When [Acct#] =5 Then [Balance] End) [acct#5Balance]

--You can add upto n accounts as per your data

From

Grouped

Group By

CustomerId

Friday, March 9, 2012

how to sum begining balance

Hi, i'm new in using crystal report 8.5 and SQL 7, now i'm trying to create report begining balance, see below details:
date item qty
12/1/05 001 150
12/12/05 002 200
12/23/05 003 250
1/1/06 001 10
1/12/06 002 15
1/23/06 003 20
2/10/06 001 100
2/15/06 002 150
2/27/06 003 200

how to sum begining balance quantity every month?
i want like these:

jan 06
beg item
150 001
200 002
250 003

feb 06
beg item
160 001
215 002
270 003

million thanks in advancegroup by date , for every month

u will get
dec 05
beg item
150 001
200 002
250 003

suppress dec 05 and put a formula in group header

dateadd('m',-1,suppresseddatefield)

hope it helps|||Sraheem thanks for your reply but sorry is not like i want i'm bit confius to describe, let me try like below:

Item Description Beg In Out End
1614 0.125 X 60 mm 346 131
1671 0.075x60mm 3,196 340
1626 Dia 0.85 mm 13,114 0

i need to create the formula for beg balance.
i have question, is it possible to create sum formula using three condition like sum ({qty},{item},cut off date)
in which to calculate beg balance I must sum qty until cut off date
then after i find beg balance i can calculate end balance.