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 |
Sqlnewbie24
Starting Member
3 Posts |
Posted - 2011-09-10 : 20:26:59
|
I need help to produce a table of different equipment and there rental duration from 0 days to 365 days as fields. To go from table1 to table2 with a loop or a set based approach ??help??javascript:insertsmilie(' ')javascript:insertsmilie(' ')Background:Table01 view -StartSample of field below|Store_location|equipment|rent_start_date|rent_end_date|duration|Details to table01Rental can last anywhere from 0 days to 365 days and over.This table holds a row for every equipment type rented out with it duration by store location. so multiple occurrences of the same equipment with different dates.The duration is calculated as =>> datadiff(dd, rent_start_date, rent_end_date)Table02: End result|Store_location|equipment|0_days to 365_days duration fields|Details for table02In this table each equipment by store is only represented once.The day fields 0_days to 365_days need be a count of all the same equipment type that share the same duration(as days rented out for). |
|
Sqlnewbie24
Starting Member
3 Posts |
Posted - 2011-09-11 : 04:24:41
|
Hi allFollowing my earlier post . here is my solution.Any comments and different solution would be greatly welcomed.Table01 layoutSelect store, equip_type , rent_start_date, rent_end_date, duration from table01I have not test the code below, so it may contain errorsSelect ,store,,equip_typeInto dbo.table02From dbo.table01Declare @counter as intDeclare @New_column as varchar (9)Set @counter = 0/***************************************************************appending 366 field on the end of table02 with a loop************************************************************** */While @counter < 366Begin/* *************************************************************creating new column name to add on to the end of table02***************************************************************/Set @New_column = cast(@counter char (3))+’_days’ALTER TABLE dbo.table02_duration ADD @New_column /***************************************************************** upate new column with count of the same duration events ******************************************************************/Update aSet a.@New_column = sum( case when b.duration = @counter then 1 else 0 end)From dbo. table02 aJoin table01 bOn a. store = b.store,And a.equip_type = b. equip_type@counter = @counter + 1end |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
Sqlnewbie24
Starting Member
3 Posts |
Posted - 2011-09-11 : 16:44:35
|
Thanks visakh16 for the link.I will go over the code. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-12 : 00:49:43
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|