April 7, 2016 at 6:49 am
Hi,
I have to design solution that will help me out to load data into 4 tables from 1 master table.
It's simple and I guess that's why it's hard!!
all that function or ssis package suppose to do is following.
1. Count total number of rows in a master table
2. Divide by 4
3. Load into table 1,2,3 and 4.
every time we run this function wipe out 4 tables and do the above process again and name of the main table and destination tables will be always same.
example:
Master Table has 4200 records than,
Table A will get 1-1000
Table B will get 1001-2000
Table C will get 2001-3000
Table D will get 3001-4200.
Please help!
Thanks.
April 7, 2016 at 7:24 am
SSIS and Conditional Split on a computed 1-4 sequence. 4 outputs from the conditional split, one into each of your 4 tables. Easy-peasy, lemon-squeezy!! 😀
https://www.simple-talk.com/sql/ssis/ssis-basics-using-the-conditional-split/
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
April 7, 2016 at 7:29 am
You could use the NTILE function.
WITH
E(n) AS(
SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))E(n)
),
E2(n) AS(
SELECT a.n FROM E a, E b
),
E4(n) AS(
SELECT a.n FROM E2 a, E2 b
),
cteTally(n) AS(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) n
FROM E4
)
SELECT n, NTILE(4) OVER(ORDER BY n)
FROM cteTally;
April 7, 2016 at 7:29 am
nTile window function can create a column with numbers 1 to 4. After that it should be easy to do an INSERT ... WHERE Tile = 1...
CREATE TABLE #Test (
SomeNumber INT
)
INSERT INTO #Test
(SomeNumber
)
SELECT TOP(100) ROW_NUMBER() OVER (ORDER BY (SELECT 1)) FROM sys.columns
-- query
SELECT SomeNumber, NTILE(4) OVER (ORDER BY SomeNumber) AS Tile
FROM #Test
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 7, 2016 at 8:00 am
THANKS
April 7, 2016 at 8:00 am
THANKS
April 7, 2016 at 8:01 am
GilaMonster (4/7/2016)
nTile window function can create a column with numbers 1 to 4. After that it should be easy to do an INSERT ... WHERE Tile = 1...
CREATE TABLE #Test (
SomeNumber INT
)
INSERT INTO #Test
(SomeNumber
)
SELECT TOP(100) ROW_NUMBER() OVER (ORDER BY (SELECT 1)) FROM sys.columns
-- query
SELECT SomeNumber, NTILE(4) OVER (ORDER BY SomeNumber) AS Tile
FROM #Test
THANK YOU
April 7, 2016 at 9:27 am
Since you are wanting to put 1/4 rows in each of 4 different tables you MUST ensure your "sequence generator", whatever you use, is REPEATABLY RIGOROUS, or you run the risk of bad data. Specifically. any form of ordering must be completely repeatable because you will need to do it 4 times on the main table if you do this in SQL Server. That is one of the reasons I mentioned SSIS, since it can do this in a single pass.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply