Author |
Topic |
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2009-02-01 : 01:50:33
|
[code]create table employee_load(employee_load_id INT IDENTITY(1,1) NOT NULL,employee_id INT NOT NULL,state CHAR(2) NOT NULL,age INT NOT NULL,title VARCHAR(15))employee_load table is loaded with all the employee detailscreate table extract_type(extract_type_id INT IDENTITY(1,1) NOT NULL,extract_type VARCHAR(25),extract_type_description VARCHAR(100) NOT NULL,volume INT NOT NULL)extract_type table is loaded with all the extractscreate table employee_extract(employee_id INT NOT NULL,state CHAR(2) NOT NULL,age INT NOT NULL,title VARCHAR(15)extract_type_id INT)INSERT INTO extract_type(extract_type,extract_type_description,volume)SELECT 'FED','Fedral',5 UNION ALLSELECT 'COUNTY','COUNTY',2 UNION ALLSELECT 'CITY','CITY',1INSERT INTO employee_load(employee_id,state,age,title)SELECT 1,'CA',24,'Developer' UNION ALLSELECT 2,'CA',23,'Developer' UNION ALLSELECT 3,'CA',22,'Developer' UNION ALLSELECT 4,'CA',25,'Developer' UNION ALLSELECT 5,'CA',26,'Developer' UNION ALLSELECT 6,'CA',27,'Developer' UNION ALLSELECT 7,'CA',28,'Developer' UNION ALLSELECT 8,'CA',22,'Developer' UNION ALLSELECT 9,'CA',23,'Developer' UNION ALLSELECT 10,'CA',25,'Developer' UNION ALLSELECT 11,'CA',26,'Developer' UNION ALLSELECT 12,'GA',24,'Developer' UNION ALLSELECT 13,'GA',23,'Developer' UNION ALLSELECT 14,'GA',22,'Developer' UNION ALLSELECT 15,'GA',25,'Developer' UNION ALLSELECT 16,'GA',26,'Developer' UNION ALLSELECT 17,'GA',27,'Developer' UNION ALLSELECT 18,'GA',28,'Developer' UNION ALLSELECT 19,'GA',22,'Developer' UNION ALLSELECT 20,'GA',23,'Developer' UNION ALLSELECT 21,'GA',25,'Developer' UNION ALLSELECT 22,'GA',26,'Developer' UNION ALLSELECT 23,'NY',24,'Developer' UNION ALLSELECT 24,'NY',23,'Developer' UNION ALLSELECT 25,'NY',22,'Developer' UNION ALLSELECT 26,'NY',25,'Developer' UNION ALLSELECT 27,'NY',26,'Developer' UNION ALLSELECT 28,'NY',27,'Developer' UNION ALLSELECT 29,'NY',28,'Developer' UNION ALLSELECT 30,'NY',22,'Developer' UNION ALLSELECT 31,'NY',23,'Developer' UNION ALLSELECT 32,'NY',25,'Developer' UNION ALLSELECT 33,'NY',26,'Developer' [/code]employee_extract table needs to be populated with a certain volumeThe need is to populate employee_extract with data from employee_load and extract_type.The data selected from employee_load table is based on state and it has to distribute the volume to eachextract type For example the employee_extract will be populated for "CA" as:SELECT 1,'CA',24,'Developer',1 UNION ALLSELECT 2,'CA',23,'Developer',1 UNION ALLSELECT 3,'CA',22,'Developer',1 UNION ALLSELECT 4,'CA',25,'Developer',1 UNION ALLSELECT 5,'CA',26,'Developer',1 UNION ALLSELECT 6,'CA',27,'Developer',2 UNION ALLSELECT 7,'CA',28,'Developer',2 UNION ALLSELECT 8,'CA',22,'Developer',1 UNION ALLsimilarly all the other states based on the volume and extract type.Should we use SET ROWCOUNT.If we use this what happens when we migrate to 2005 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-01 : 12:39:29
|
you can use SET ROWCOUNT ON or subquery in 2000. ANd if you're planning to migrate to 2005, you may use windowing function ROW_NUMBER() for this. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-01 : 12:44:27
|
In 2000, it will be likeSELECT t.employee_id,t.state,t.age,t.title,et.extract_type_id FROM(SELECT (SELECT COUNT(*) FROM employee_load WHERE state=e.state AND employee_id<=e.employee_id) AS Seq,*FROM employee_load e)tINNER JOIN extract_type etON et.volume<=t.Seq |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-01 : 12:45:41
|
and in sql 2005SELECT t.employee_id,t.state,t.age,t.title,et.extract_type_id FROM(SELECT ROW_NUMBER() OVER (PARTITION BY state ORDER BY employee_id) AS Seq,*FROM employee_load e)tINNER JOIN extract_type etON et.volume<=t.Seq |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2009-02-02 : 18:24:38
|
When I execute this query I'm not getting the correct volume :SELECT t.employee_id,t.state,t.age,t.title,et.extract_type_id FROM(SELECT (SELECT COUNT(*) FROM employee_load WHERE state=e.state AND employee_id<=e.employee_id) AS Seq,*FROM employee_load e)tINNER JOIN extract_type etON et.volume<=t.Seq-- For CA it should show only 8 records and 5 should have extract_type_id = 1,2 records should haveextract_type_id = 2 and 1 record with extract_type_id = 3 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-03 : 10:24:51
|
what does this return?select * from extract_type |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2009-02-03 : 10:49:41
|
Thanks this change (et.volume>=t.Seq ) will give me my results. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-03 : 12:40:07
|
ok |
|
|
|
|
|