I have one table in relational database Sybase ASE, with few columns. Three of them looks like this example:
_____________
| Product |
---------------
| ProductId |
| Name |
| Quantity |
_____________
So we have some records:
__________________________________
| ProductId | Name | Quantity |
----------------------------------
| 1 | pants | 2 |
| 2 | shirt | 1 |
| 3 | sweater | 3 |
----------------------------------
I need to get every name as many times as 'Quantity' of this product.
So the result should looks like:
- pants
- pants
- shirt
- sweater
- sweater
- sweater
If somebody have any idea how can I do this, please help me.
EDIT 2014-01-24 14:17 UTC+1
I'd like to thanks everybody. Gordon's solution is realy nice, but for my situation (bigger Quantity) I can't use that sql. I try do somethnig like 333kenshin's and simon's solutions but without cursor. I do somthnig like this:
IF OBJECT_ID('#TEMP') is not null
DROP TABLE #TEMP
create TABLE #TEMP (Name varchar(255))
DECLARE @Name varchar(255)
DECLARE @Quant INT
DECLARE @prodId INT
SET @prodId = 1
WHILE (EXISTS(SELECT 1 FROM product WHERE productID = @prodId))
BEGIN
SELECT
@Name = Name
@Quant = Quantity
FROM Product
DECLARE @i INT
SET @i = 1
WHILE @i <= @Quant
BEGIN
insert into #TEMP
values(@Name)
SELECT @i=@i+1
END
SELECT @prodId = @prodId + 1
END
select * from #TEMP
drop table #TEMP
For me, and my DB it was fastest solution. So thanks a lot for every answers.