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 |
|
elektrobank
Starting Member
15 Posts |
Posted - 2003-02-17 : 17:35:16
|
| How can I get back multiple copies of the same results from a table? For instance if I have a table like this:inventory:ID|Description|Price------------------------1 | Item 1 | $15.002 | Item 2 | $18.00and I want to be able to get back results with like 3 of item 1 and 2 of item 2, so the result set from my query would be like:ID|Description|Price------------------------1 | Item 1 | $15.001 | Item 1 | $15.001 | Item 1 | $15.002 | Item 2 | $18.002 | Item 2 | $18.00Or I may want to get back 5 of item 1 and 0 of item 2. How can I do this with a single query? All the work has to be done within the query, I can't build this set with code from the results or use temp tables or anything like that.I just want to be able to get back multiple copies of the same item how ever many I want. |
|
|
chadmat
The Chadinator
1974 Posts |
Posted - 2003-02-17 : 18:00:37
|
| select id, description, pricefrom tablewhere id=1unionselect id, description, pricefrom tablewhere id=1-Chad |
 |
|
|
elektrobank
Starting Member
15 Posts |
Posted - 2003-02-17 : 18:03:58
|
I had already tried that. A union wont work since the results are the same so it only shows one of them.quote: select id, description, pricefrom tablewhere id=1unionselect id, description, pricefrom tablewhere id=1-Chad
|
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-02-17 : 18:15:42
|
| Do a cross join with a table of numbers or pretty much anything; that will multiply the # of records you return by the number of records in the table you cross join with.That is, if you have a table called "Numbers" with 3 records,SELECT id, description, priceFROM YourTableCROSS JOINNumberswill return 3 times as many records as there are in YourTable.You could also set up a table with each item id, and how many of that item you'd like to return:ID,ReturnCount1,32,2..etc...Call the above table "ItemReturnCounts".Then you can:SELECT A.id, A.description, A.priceFROM YourTable AINNER JOINItemReturnCounts IRCON A.ID = IRC.IDINNER JOINNumbersON Numbers.Number <= IRC.ReturnCountOr something along those lines; that allows you to control how many records of each ID you'd like to return based on the values in the ItemReturnCounts table.Finally, you could just put duplicate dummy records another table; call this "DummyTable":ID111222Then,SELECT A.ID, A.Description, A.PriceFROM YourTable AINNER JOIN DummyTable BON A.ID = B.IDwill return as many records for each ID as there are records for each ID in the DummyTable.- Jeff |
 |
|
|
chadmat
The Chadinator
1974 Posts |
Posted - 2003-02-17 : 18:18:26
|
| Sorry,I meant "Union all"select id, description, price from table where id=1 union all select id, description, price from table where id=1 -Chad |
 |
|
|
elektrobank
Starting Member
15 Posts |
Posted - 2003-02-17 : 18:21:36
|
Yup, that's just what I needed, thanks!!!!quote: Sorry,I meant "Union all"select id, description, price from table where id=1 union all select id, description, price from table where id=1 -Chad
|
 |
|
|
|
|
|
|
|