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 |
asford3188
Starting Member
2 Posts |
Posted - 2013-10-25 : 17:20:05
|
I’m not sure how to phase my question, which is probably why I couldn’t find a good answer on Google. I want to use a couple numbers pulled from columns in a table that I already have, and create another table with a number of records equal to one of those numbers divided by the other. The best way to explain is probably by example, so here goes:This is the table that I already have:Product|UnitsPerCase|TotalUnitsABC|4|20DEF|2|16GHI|8|16This is the table I want to create programmatically in SQL Server, based on the table above:Product|UnitsPerCase|CaseNumberABC|4|1ABC|4|2ABC|4|3ABC|4|4ABC|4|5DEF|2|1DEF|2|2DEF|2|3DEF|2|4DEF|2|5DEF|2|6DEF|2|7DEF|2|8GHI|8|1GHI|8|2I don’t even have a guess as to how to do this. Any help is appreciated! |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2013-10-25 : 18:35:16
|
This may not be the most efficient way to do this, but it was the only thing that came to mind. I hate suggesting cursors, but here you go:use tempdbgocreate table test(c1 varchar(3),c2 int,c3 int)insert into test values('ABC', 4, 20)insert into test values('DEF', 2, 16)insert into test values('GHI', 8, 16)SELECT * INTO NewTest FROM test where 1=0DECLARE @c1 varchar(3)DECLARE @c2 intDECLARE @c3 intDECLARE mycur cursor for select c1, c2, c3 from testopen mycurfetch next from mycur into @c1, @c2, @c3WHILE @@FETCH_STATUS = 0BEGINDECLARE @i intSELECT @i = @c3/@c2WHILE (@i > 0)BEGININSERT INTO NewTest Values(@c1, @c2, @i)SET @i -= 1ENDFETCH NEXT From mycur into @c1, @c2, @c3ENDSELECT * FROM NewTestORDER BY c1 asc, c3 ascCLOSE mycurdeallocate mycurDROP table testDROP table NewTest-ChadMicrosoft Certified Master SQL Server 2008 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-26 : 04:58:01
|
You can just do this without using a cursor;With NumbersAS(SELECT 1 AS NUNION ALLSELECT N + 1FROM NumbersWHERE N + 1 <=1000)SELECT t.Product, t.UnitsPerCase, ROW_NUMBER() OVER (PARTITION BY t.Product ORDER BY t.Product) AS CaseNumberFROM Table tCROSS JOIN Numbers nWHERE N BETWEEN 1 AND (TotalUnits/UnitsPerCase) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
asford3188
Starting Member
2 Posts |
Posted - 2013-10-28 : 10:26:09
|
quote: Originally posted by visakh16 You can just do this without using a cursor;With NumbersAS(SELECT 1 AS NUNION ALLSELECT N + 1FROM NumbersWHERE N + 1 <=1000)SELECT t.Product, t.UnitsPerCase, ROW_NUMBER() OVER (PARTITION BY t.Product ORDER BY t.Product) AS CaseNumberFROM Table tCROSS JOIN Numbers nWHERE N BETWEEN 1 AND (TotalUnits/UnitsPerCase) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
That is excellent. Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-28 : 13:20:29
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|