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 |
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 05:39:37
|
| Hi all,I am facing a pretty challenging task on SQL. We have a table, the structure is like below:Date Sales_VolumeWeek01 1,000Week02 1,200Week03 1,400Week06 2,000Week07 1,700Week08 1,500Week11 1,200Week12 1,300Week13 1,400Week14 1,600From above table you would see the week numbers are not consecutive. What I am trying to do is separating all those weeks are not consecutive and at the mean time, giving a sequence number to those consecutive weeks. Here is my desired output.Date Sales_Volume Sequence_NoWeek01 1,000 1Week02 1,200 2Week03 1,400 3Week06 2,000 1Week07 1,700 2Week08 1,500 3Week11 1,200 1Week12 1,300 2Week13 1,400 3Week14 1,600 4I am not sure is there a way I can achieve this with SQL, or I need PL/SQL to do this. Our database environment is MS SQL2000. I appreciate any thought you have on it. Many thanks for your time and help.Pete |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-06-02 : 06:07:48
|
| What makes you think you can do something in pl/sql but not t-sql. Usually you can do anything you need in either. The problem is usually knowing what you want to do.Not efficient but my first thought is (this would be a lot simpler in v2005 with a common table expression I feel)To get the week numberconvert(int,right(date,2))everything that follows I've used d for the above expressionTo get the start of sequencesselect *from tbl t1where not exists (select * from tbl t2 where t2.d+1 = t1.d)so to get the resultselect t.*, (select count(*) from tbl t2 where t2.d <= t.d and t2.d >= a.strtseq)fromtbl tjoin(select t.d, strtseq = max(t1.d) from tbl t, tbl t1 where not exists (select * from tbl t2 where t2.d+1 = t1.d) and t1.d <= t.d group by t.d) aon t.d = a.d==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-02 : 06:17:34
|
declare @table table( weekno int)insert into @tableselect 1 unionselect 2 unionselect 3 unionselect 6 unionselect 7 unionselect 8 unionselect 11 unionselect 12 unionselect 13 unionselect 14select t.weekno, t.weekno - ( select max(weekno) as max_weekno from ( select weekno, (select count(*) from @table x where x.weekno = t1.weekno -1) as flag from @table t1 ) a where flag = 0 and weekno <= t.weekno) + 1from @table t KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-02 : 06:30:29
|
And another option...  --datadeclare @t table (Date varchar(10) primary key, Sales_Volume int, Sequence_No int)insert @t (Date, Sales_Volume) select 'Week01', 1000union all select 'Week02', 1200union all select 'Week03', 1400union all select 'Week06', 2000union all select 'Week07', 1700union all select 'Week08', 1500union all select 'Week11', 1200union all select 'Week12', 1300union all select 'Week13', 1400union all select 'Week14', 1600--calculation - note that the order 'can not be guaranteed', though I've never yet seen an example of this not working.declare @i int, @PreviousDateNumber intupdate @t set @i = case when cast(right(Date, 2) as int) = @PreviousDateNumber + 1 then @i + 1 else 1 end, Sequence_No = @i, @PreviousDateNumber = right(Date, 2)select * from @t/*resultsDate Sales_Volume Sequence_No ---------- ------------ ----------- Week01 1000 1Week02 1200 2Week03 1400 3Week06 2000 1Week07 1700 2Week08 1500 3Week11 1200 1Week12 1300 2Week13 1400 3Week14 1600 4*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 08:08:12
|
| Thanks very much for your help, that is what I was looking for. But our case is slightly different. Instead of week no in the first column, it's actually the Week Commencing Date. For instance I want it to display as:Date Sales_Volumn Sequence_No02/01/2005 1,000 109/01/2005 1,000 216/01/2005 1,200 323/01/2005 1,150 4-------11/12/2005 1,300 118/12/2005 1,200 2---------01/01/2006 1,000 108/01/2006 1,000 215/01/2006 1,200 322/01/2006 1,300 4How I can apply the SQL to these as I can’t use MAX function and do the calculations.Thanks very much for your time and help! |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-02 : 08:28:57
|
Ah, we have a case of moving goal posts 1. Are you absolutely certain that you last post specifies what you want as your final output?2. Is your actual data structure what you originally posted (i.e. Date with actual values of 'Week01' etc...)?Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 08:38:25
|
| Hi Ryan Randall,I am sorry if I comfused you. Yes, my last post is actually what I want exactly. I put Week No instead just wanted to make things easier to understand. Thanks very much for your help.Pete |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-02 : 08:44:23
|
| And my 2nd question?Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-06-02 : 08:55:27
|
| You can use datediff(ww,'20050101',Date)to get the week number to use in the sequence.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 09:46:07
|
| 2.Is your actual data structure what you originally posted (i.e. Date with actual values of 'Week01' etc...)?No, I put Week Number instead just wanted to make things easier to understand, it's actually Week Commencing Date. |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 09:51:34
|
| You can use datediff(ww,'20050101',Date)to get the week number to use in the sequence.I am afraid I can't, because I have more than 3 years history data, I can't use Week Number to identify them. If it's week number, it has to be like: 2005 Week01, 2006 Week2 etc. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-02 : 09:55:01
|
then use YYYYWWselect year(date) * 100 + datepart(week, date) KH |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-02 : 10:23:42
|
| That really helps, thanks a lot! |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-07 : 05:57:12
|
| Hi all, I got a new problem with the SQL. Can I change the output a little bit, it's like below now:Week_No_Volumn Sequence_No01 1,000 102 1,000 203 1,200 304 1,150 4-------07 1,300 108 1,200 2---------11 1,000 112 1,000 213 1,200 314 1,300 4How can I make it as:Week_No Sales_Volumn Sequence_No01 1,000 402 1,000 303 1,200 204 1,150 1-------07 1,300 208 1,200 1---------11 1,000 412 1,000 313 1,200 214 1,300 1Basically what I want is instead of ascend, can I order the SQ_No descend? How should I change the SQL:select t.weekno, t.weekno - ( select max(weekno) as max_weekno from ( select weekno, (select count(*) from @table x where x.weekno = t1.weekno -1) as flag from @table t1 ) a where flag = 0 and weekno <= t.weekno) + 1from @table tThanks very much for your help. |
 |
|
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2006-06-07 : 06:02:18
|
| I found the solution, the SQL should change as:select t.weekno, ( select min(weekno) as max_weekno from ( select weekno, (select count(*) from @table x where x.weekno = t1.weekno +1) as flag from @table t1 ) a where flag = 0 and weekno >= t.weekno) - t.weekno + 1from @table tThanks very much! |
 |
|
|
|
|
|
|
|