Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi,I have a table which has following columns:ID AssetTypeID Hour UtilizationFactor1 1 1 .2 2 1 2 .33 1 3 .44 1 4 .5ID is the PK..it keeps incrementing.I have data in this format. ie. For every AssetTypeID, I have to enter values for 4 hours and utilization factor for each hour.AssetTypeID range is 1 to 25.Is there any way to insert data for 25 different AssetTypeIDs by sql code and not manually?Thanks,Mavericky
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2011-11-09 : 17:46:30
maybe something like this - this assumes you want the Hours and UtilizationFactors repeating for each AssetTypeID:
select number + 1 as ID ,(number / 4) + 1 as AssetTypeID ,(number % 4) + 1 as Hour ,((number % 4) + 2)/10.0 as UtilizationFactorfrom master..spt_valueswhere type = 'P'and number between 0 and 99EDIT:here's the output:ID AssetTypeID Hour UtilizationFactor----------- ----------- ----------- ---------------------------------------1 1 1 0.2000002 1 2 0.3000003 1 3 0.4000004 1 4 0.5000005 2 1 0.2000006 2 2 0.3000007 2 3 0.4000008 2 4 0.5000009 3 1 0.20000010 3 2 0.300000....95 24 3 0.40000096 24 4 0.50000097 25 1 0.20000098 25 2 0.30000099 25 3 0.400000100 25 4 0.500000(100 row(s) affected)
Be One with the OptimizerTG
mavericky
Posting Yak Master
117 Posts
Posted - 2011-11-09 : 20:47:35
Thanks TG but can anybody give example using cte?
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2011-11-10 : 10:02:49
Why? This gives the same result:
with cte (ID,AssetTypeID,[hour],UtilizationFactor)as( select 1,1,1,.2 union all select [id] + 1 ,case when ([id]-1)/4 = id/4 then AssetTypeID else Assettypeid + 1 end ,case when ([id]-1)/4 = id/4 then [hour] + 1 else 1 end ,case hour when 1 then .3 when 2 then .4 when 3 then .5 when 4 then .2 end from cte where id < 100)select * from cte