Log in  ::  Register  ::  Not logged in

## How to multiply rows in one table based on the value on another table

 Author Message _simon_ Mr or Mrs. 500 Group: General Forum Members Points: 597 Visits: 776 Data:`declare @t1 table (Id int)insert into @t1 (Id)select 1 union select 2 union select 3declare @t2 table (Id int, X int)insert into @t2 (Id, X)select 1, 10 unionselect 2, 5 unionselect 3, 0`Is it possible to get those results (see below) from my @t1 table with a single select without loops?`select 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 1 union allselect 2 union allselect 2 union allselect 2 union allselect 2 union allselect 2` _simon_ Mr or Mrs. 500 Group: General Forum Members Points: 597 Visits: 776 Hm... I could create table valued function which gets an id from first table and the number of repeats from second table and performs a cross join... Will try it out now. _simon_ Mr or Mrs. 500 Group: General Forum Members Points: 597 Visits: 776 Got it First we need a table function which returns all integers between x and y (the function is stolen from somewhere, don't remember where...):`create function [dbo].[f_table_numbers] (@st bigint, @end bigint)returns tableasreturnwith a as (select convert(bigint,0) as n union all select 0), b as (select 0 as n from a as a cross join a as b), c as (select 0 as n from b as a cross join b as b), d as (select 0 as n from c as a cross join c as b), e as (select 0 as n from d as a cross join d as b), f as (select 0 as n from e as a cross join e as b), nums as (select row_number() over (order by (select 1)) as num from f as a cross join f as b)select @st + num - 1 as numfrom numswhere num <= @end-@st+1`Then my table function:`create function Temp (@Id int, @N int) returns @t table (Id int)asbegin insert into @t (Id) select x.Id from (select @Id Id) x cross join (select Num from dbo.f_table_numbers(1, @N)) y returnend`Test:`declare @t1 table (Id int)insert into @t1 (Id)select 1 union select 2 union select 3declare @t2 table (Id int, X int)insert into @t2 (Id, X)select 1, 10 unionselect 2, 5 unionselect 3, 0;with temp as ( select t1.Id, t2.X from @t1 t1 join @t2 t2 on (t1.Id = t2.Id))select t1.Idfrom temp t1 cross apply dbo.Temp(t1.Id, t1.X)`Edit - talking to my self, sorry, if this is agains some rules you can delete the topic... Phil Parkin SSC Guru Group: General Forum Members Points: 52590 Visits: 21190 Here's another possibility:`declare @t1 table (Id int)insert into @t1 (Id)select 1 union select 2 union select 3declare @t2 table (Id int, X int)insert into @t2 (Id, X)select 1, 10 unionselect 2, 5 unionselect 3, 0;with t3 as (select id, x, row_number() over (partition by id order by id) ro from @t2cross join sys.all_columns)select id, x from t3 where ro <= x` Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot. _simon_ Mr or Mrs. 500 Group: General Forum Members Points: 597 Visits: 776 @Phil - thanks, much simpler than my solution Eugene Elutin SSChampion Group: General Forum Members Points: 12686 Visits: 5478 I think that one could be slightly faster:`select t1.idfrom @t1 t1inner join @t2 t2 on t2.Id = t1.Id and t2.X > 0cross apply (select top (t2.X) 1 from sys.columns) n(n)` _____________________________________________"The only true wisdom is in knowing you know nothing""O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D(So many miracle inventions provided by MS to us...)How to post your question to get the best and quick help _simon_ Mr or Mrs. 500 Group: General Forum Members Points: 597 Visits: 776 Beautiful, thanks

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         SQLCentric         Golden Gate Software         Lumigent         Red Gate Software         Quest Software         ApexSQL         Idera      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 7.0 Books         Discuss SQL Server 2000 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2017 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.