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 |
|
GaryE
Starting Member
1 Post |
Posted - 2011-02-02 : 15:46:35
|
| Hello!I am trying to return a set of values based on the current year GL Week.I am looking at the MAX GL week and then want to subtract X number of weeks from "MAX(gl_week)" to return a list of weeks.The format is: "201130" (that equals the current year * 100 + the gl_week.) @PROMOWKS is the number of weeks to subtract.DECLARE @PROMOWKS intSET @PROMOWKS= '5'SELECT DISTINCT (DatePart(YYYY,GETDATE())*100) + (gl_week) AS WeekDateFROM pfg_dm.dbo.dim_opco_time_tbl ot WHERE gl_year=DatePart(yyyy,Getdate()) AND week_end_flag = 'Y'GROUP BY ot.gl_weekHAVING gl_week between (max(ot.gl_week)-@PROMOWKS) AND (max(ot.gl_week))My output is currently starting with week 01 and it should start with week 15. (max(gl_week) which is currently 30. @PROMOWKS= '5' so i should be starting at 201115.)201101201102201103201104201105201106201107201108201109201110201111201112201113201114201115201116201117201118201119201120201121201122201123201124201125201126201127201128201129201130 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-02-02 : 16:07:40
|
select TOP(@PROMOWKS) ....ORDER BY gl_Week desc N 56°04'39.26"E 12°55'05.63" |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-02-03 : 13:51:24
|
| Why not put the GL calendar in a look-up table with the start and end dates of each GL week? It would allow the use of an index. UOu can also juliainize thwe weeks to do math on them.--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|