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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 creating a new row based on value in a column

Author  Topic 

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-09-10 : 12:29:15
Hello all...

I have a question.

Suppose I have a table that contains the following two records:

Item |Qty
Shirts |2
Socks |3

I want to create a view that takes that table and shows it like this:

No |Item |Total |Counter
1 |Shirts |2 |1 of 2
2 |Shirts |2 |2 of 2
3 |Socks |3 |1 of 3
4 |Socks |3 |2 of 3
5 |Socks |3 |3 of 3


PS- I used Pipes "|" to delimit the columns only to make it easier to read on this site.

I basically want to look at the quantity field and create a new record for whatever the qty column shows...

Your help is much appreciated - thanks.

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-10 : 12:48:22

create table #mdelgado (item char(6) not null primary key, qty tinyint not null)
insert #mdelgado (item,qty)
select 'Shirts',2 union select 'Socks',3

create table #numbers (n tinyint)
declare @i tinyint
select @i = 1
while @i <= 10
begin
insert #numbers select @i
select @i = @i + 1
end
go

select
identity(int,1,1) as no,
m.item,
m.qty as total,
convert(varchar,n.n) + ' of ' + convert(varchar,m.qty) as counter
into
#final
from
#mdelgado m
inner join #numbers n
on m.qty >= n.n
go

select * from #final
go

 


Jay White
{0}
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-09-10 : 12:54:48
You presumably want to display this and may be better doing it in the presentation app.

It's not going to be easy in a view.

Create a table Nums with all numbers up to as high as necessary ( say 1 - 100) - you can use a derived table instead if you which which is created in the view.

then

select No = (select sum(qty) from tbl t2 where t2.Item < tbl.Item) + Num , Item, Total = qty, Counter = convert(varchar(10),num) + ' of ' + convert(varchar(10),qty)
from tbl, nums
where tbl.qty >= nums.num
order by Item, num



==========================================
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.
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2002-09-10 : 12:57:11

SELECT
(SELECT SUM(Qty) FROM YourTable AS T1 WHERE T1.Item < T2.Item) + n AS No,
Item,
Qty AS Total
CAST(N AS varchar) + ' of ' + CAST(Qty AS varchar)
FROM YourTable
INNER JOIN (
SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 ...
) AS Tally ON n <= Qty
ORDER BY Item, n

 
You might want to consider doing the No column some other way as this will be slow if there are many rows in YourTable. If Qty gets big then it's probably worth making Tally a real table... it's probably worth it anyway.

Edit: Too slow!


Edited by - Arnold Fribble on 09/10/2002 12:59:53
Go to Top of Page

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-09-10 : 14:27:24
Thank you Arnold, your solution was the best that worked for me.

Thanks to all for the quick reply!

Go to Top of Page
   

- Advertisement -