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


How to Calculate a column value, based on previous column value


How to Calculate a column value, based on previous column value

Author
Message
aswani1
aswani1
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Hello All,

Could you please help me here with this sql query,

drop table #tempo1
create table #tempo1 (ActNo int, PlanName varchar(20), ProcessMonth varchar(8))

insert into #tempo1 values (100,'Keeper','2012-Jul')
insert into #tempo1 values (101,'Keeper','2012-Jul')
insert into #tempo1 values (102,'Valuer','2012-Jul')
insert into #tempo1 values (103,'Keeper','2012-Jul')
insert into #tempo1 values (104,'Valuer','2012-Jul')
insert into #tempo1 values (105,'Keeper','2012-Jul')
insert into #tempo1 values (110,'Keeper','2012-Jul')

insert into #tempo1 values (100,'Keeper','2012-Oct')
insert into #tempo1 values (101,'Valuer','2012-Oct')
insert into #tempo1 values (102,'Valuer','2012-Oct')
insert into #tempo1 values (103,'Keeper','2012-Oct')
insert into #tempo1 values (104,'Keeper','2012-Oct')
insert into #tempo1 values (105,'Valuer','2012-Oct')
insert into #tempo1 values (106,'Valuer','2012-Oct')

output will show below threecolumns, first two is straight , but the fourth column shoud be calculate based on second column (if secondvalue is same as fourth then just add literal 'Cnt 2' otherwie 'Cnt 1' so that i.e., if secondcolumn value is 'Keeper' and 4th column value is also 'Keeper' then show as 'Keeper Cnt2' sameway with Valuer as well, if it is different in july and oct then just oct column value with 'Cnt 1' literal ),

for exmple

AcctNo, JulyPtcpt, OctPtcpt
100, 'Keeper', 'Keeper Cnt 2' ---> (Both July and Oct same so added Cnt 2 at the end )
101, 'Keeper','Valuer Cnt 1' ---> (Both July and Oct not same, so added Cnt 1 at the end )
102, 'Valuer','Valuer Cnt 2'
103, 'Keeper','Keeper Cnt 2'
104, 'Valuer','Keeper Cnt 1'
105, 'Keeper','Valuer Cnt 1'
106, '' ,'Valuer Cnt 1' ---> because Not exist in prior month just leave blank or just leave with Cnt1
110, 'Keeper','' --> not exist in oct so leave blank or Just leave it as Cnt1


please help me here, i am stuck at the processmonth separation for jul/oct

Kindly assist me here,

milan
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25879 Visits: 17510
Is it as simple as this?


select ActNo, PlanName, COUNT(*)
from #tempo1
group by ActNo,PlanName
order by ActNo



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
aswani1
aswani1
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Thank you for your response,


i tried your sample, it is showing two rows i mean each row is showing twice (because of plan name)please see below
ActNo PlanName (No column name)
100 Keeper 2
101 Keeper 1
101 Valuer 1
102 Valuer 2
103 Keeper 2
104 Keeper 1
104 Valuer 1
105 Keeper 1
105 Valuer 1
106 Valuer 1
110 Keeper 1



here if expecting one look like is (see acct no is apears only once)


ActNo JulyResult OctorberResult
----- ---------- --------------
100 Keeper Keeper 2
101 Keeper Valuer 1
102 Valuer Valuer 2
103 Keeper Keeper 2
104 Valuer Keeper 1
105 Keeper Valuer 1
106 Valuer 1
110 Keeper 1


Thank you again
milano
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25879 Visits: 17510
Ahh gotcha. Don't understand the usefulness of something like this but this is how you would do the query.


select ActNo, max(case when ProcessMonth = '2012-Jul' then PlanName else '' end) as JulyResult
, MAX(case when ProcessMonth = '2012-Oct' then PlanName else '' end) as OctResult
, case when max(case when ProcessMonth = '2012-Jul' then PlanName else '' end) = MAX(case when ProcessMonth = '2012-Oct' then PlanName else '' end) then 2 else 1 end
from #tempo1
group by ActNo
order by ActNo



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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