Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-01-20 : 12:10:53
|
I have a table like this:CREATE TABLE #Test ( [InstNumber] VARCHAR(4) ,[GLLineNumber] VARCHAR(10) ,[GLLineDescription] VARCHAR(100) ,[NovemberBalance] DECIMAL(23,2) ,[DecemberBalance] DECIMAL(23,2) ,[JanuaryBalance] DECIMAL(23,2) ,[FebruaryBalance] DECIMAL(23,2) ,[MarchBalance] DECIMAL(23,2) ,[AprilBalance] DECIMAL(23,2) ,[MayBalance] DECIMAL(23,2) ,[JuneBalance] DECIMAL(23,2) ,[JulyBalance] DECIMAL(23,2) ,[AugustBalance] DECIMAL(23,2) ,[SeptemberBalance] DECIMAL(23,2) ,[OctoberBalance] DECIMAL(23,2) ,[CurrentBalance] DECIMAL(23,2)) The column CurrentBalance is supposed to be the value contained in the appropriate column for LAST month. For example, CurrentBalance as of January should be the value in the column DecemberBalance. It could be, I suppose, done with a trigger or an update statement, or maybe a computed column. Any ideas?Duane |
|
X002548
Not Just a Number
15586 Posts |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-01-20 : 13:37:02
|
Good idea, but the structure is pretty rigid as I am migrating a data mart from SQL Server 2000 to 2008 with some limited enhancements. These are actually import and stage tables brought in through flat files with this structure.Duane |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-01-20 : 13:38:42
|
[code][UPDATE #TableSET DecemberBalance = JanuaryBalance[/code]If that doesn't work can you post sample data and expected output? |
 |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-01-20 : 14:04:58
|
What I want is something like this in pseudocode:DECLARE @ReportDate DateTimeSET @ReportDate = GetDate()IF Month(@ReportDate()) = 'January' THEN CurrentBalance = DecemberBalanceELSEIF Month(@ReportDate()) = 'February' THEN CurrentBalance = JanuaryBalanceELSEIF Month(@ReportDate()) = 'March' THEN CurrentBalance = FebruaryBalanceELSEIF Month(@ReportDate()) = 'April' THEN CurrentBalance = MarchBalanceELSEIF Month(@ReportDate()) = 'May' THEN CurrentBalance = AprilBalanceELSEIF Month(@ReportDate()) = 'June' THEN CurrentBalance = MayBalanceELSEIF Month(@ReportDate()) = 'July' THEN CurrentBalance = JuneBalanceELSEIF Month(@ReportDate()) = 'August' THEN CurrentBalance = JulyBalanceELSEIF Month(@ReportDate()) = 'September' THEN CurrentBalance = AugustBalanceELSEIF Month(@ReportDate()) = 'October' THEN CurrentBalance = SeptemberBalanceELSEIF Month(@ReportDate()) = 'November' THEN CurrentBalance = OctoberBalanceELSEIF Month(@ReportDate()) = 'December' THEN CurrentBalance = NovemberBalanceEND IFThe data really can be any numerical random values in the monthly columns. Right now the last column is empty and I want to fill it with the appropriate value from the correct montly column. Let's say for a given row or InstNumber, that NovemberBalance = $1,000, DecemberBalance is $159.34, and DecemberBalance = $229.11. If the column gets inserted/updated in January, then CurrentBalance will be cleared and set to $159.34. Next month, in February, it will be set to $229.11. LAST month (in December, it would have been set to $1,000, the value in the NovemberColumn. No sums or other calculations. Just a copy of the value in the appropriate Month column to CurrentBalance.I think that I am almost answering my own question. Maybe I need a CASE statement if I can figure out how to get it correctly.Duane |
 |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-01-20 : 15:08:19
|
Actually, I figured it out and it works. Let me know if you see any pitfalls or improvements.DECLARE @strReportDate VARCHAR(10)SET @strReportDate = CAST(DATENAME(MONTH,GETDATE()) AS VARCHAR(10))SELECT [InstNumber] ,[GLLineNumber] ,[GLLineDescription] ,[NovemberBalance] ,[DecemberBalance] ,[JanuaryBalance] ,[FebruaryBalance] ,[MarchBalance] ,[AprilBalance] ,[MayBalance] ,[JuneBalance] ,[JulyBalance] ,[AugustBalance] ,[SeptemberBalance] ,[OctoberBalance] ,[CurrentBalance] ,CASE @strReportDate WHEN 'January' THEN DecemberBalance WHEN 'Febuary' THEN JanuaryBalance WHEN 'March' THEN FebruaryBalance WHEN 'April' THEN MarchBalance WHEN 'May' THEN AprilBalance WHEN 'June' THEN MayBalance WHEN 'July' THEN JuneBalance WHEN 'August' THEN JulyBalance WHEN 'September' THEN AugustBalance WHEN 'October' THEN SeptemberBalance WHEN 'November' THEN OctoberBalance WHEN 'December' THEN NovemberBalance ELSE Null END AS CurrentBalanceFROM #Test Duane |
 |
|
|
|
|
|
|