Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 How to Calculate a column value, based on previous column value Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, April 10, 2013 1:18 PM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, April 10, 2013 1:59 PM 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
Post #1440991
 Posted Wednesday, April 10, 2013 1:26 PM
 SSChampion Group: General Forum Members Last Login: Yesterday @ 2:23 PM Points: 10,854, Visits: 10,012
 Is it as simple as this?`select ActNo, PlanName, COUNT(*)from #tempo1group by ActNo,PlanNameorder 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 Moden's 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)
Post #1440994
 Posted Wednesday, April 10, 2013 1:38 PM
 Forum Newbie Group: General Forum Members Last Login: Wednesday, April 10, 2013 1:59 PM 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 2101 Keeper 1101 Valuer 1102 Valuer 2103 Keeper 2104 Keeper 1104 Valuer 1105 Keeper 1105 Valuer 1106 Valuer 1110 Keeper 1here if expecting one look like is (see acct no is apears only once)ActNo JulyResult OctorberResult----- ---------- --------------100 Keeper Keeper 2101 Keeper Valuer 1102 Valuer Valuer 2103 Keeper Keeper 2104 Valuer Keeper 1 105 Keeper Valuer 1106 Valuer 1110 Keeper 1Thank you againmilano
Post #1440996
 Posted Wednesday, April 10, 2013 1:58 PM
 SSChampion Group: General Forum Members Last Login: Yesterday @ 2:23 PM Points: 10,854, Visits: 10,012
 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 endfrom #tempo1group by ActNoorder 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 Moden's 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)
Post #1441005

 Permissions