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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Selection Volume Control

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 details

create 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 extracts

create 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 ALL
SELECT 'COUNTY','COUNTY',2 UNION ALL
SELECT 'CITY','CITY',1


INSERT INTO employee_load
(
employee_id,
state,
age,
title
)
SELECT 1,'CA',24,'Developer' UNION ALL
SELECT 2,'CA',23,'Developer' UNION ALL
SELECT 3,'CA',22,'Developer' UNION ALL
SELECT 4,'CA',25,'Developer' UNION ALL
SELECT 5,'CA',26,'Developer' UNION ALL
SELECT 6,'CA',27,'Developer' UNION ALL
SELECT 7,'CA',28,'Developer' UNION ALL
SELECT 8,'CA',22,'Developer' UNION ALL
SELECT 9,'CA',23,'Developer' UNION ALL
SELECT 10,'CA',25,'Developer' UNION ALL
SELECT 11,'CA',26,'Developer' UNION ALL

SELECT 12,'GA',24,'Developer' UNION ALL
SELECT 13,'GA',23,'Developer' UNION ALL
SELECT 14,'GA',22,'Developer' UNION ALL
SELECT 15,'GA',25,'Developer' UNION ALL
SELECT 16,'GA',26,'Developer' UNION ALL
SELECT 17,'GA',27,'Developer' UNION ALL
SELECT 18,'GA',28,'Developer' UNION ALL
SELECT 19,'GA',22,'Developer' UNION ALL
SELECT 20,'GA',23,'Developer' UNION ALL
SELECT 21,'GA',25,'Developer' UNION ALL
SELECT 22,'GA',26,'Developer' UNION ALL

SELECT 23,'NY',24,'Developer' UNION ALL
SELECT 24,'NY',23,'Developer' UNION ALL
SELECT 25,'NY',22,'Developer' UNION ALL
SELECT 26,'NY',25,'Developer' UNION ALL
SELECT 27,'NY',26,'Developer' UNION ALL
SELECT 28,'NY',27,'Developer' UNION ALL
SELECT 29,'NY',28,'Developer' UNION ALL
SELECT 30,'NY',22,'Developer' UNION ALL
SELECT 31,'NY',23,'Developer' UNION ALL
SELECT 32,'NY',25,'Developer' UNION ALL
SELECT 33,'NY',26,'Developer' [/code]

employee_extract table needs to be populated with a certain volume

The 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 each
extract type For example the employee_extract will be populated for "CA" as:

SELECT 1,'CA',24,'Developer',1 UNION ALL
SELECT 2,'CA',23,'Developer',1 UNION ALL
SELECT 3,'CA',22,'Developer',1 UNION ALL
SELECT 4,'CA',25,'Developer',1 UNION ALL
SELECT 5,'CA',26,'Developer',1 UNION ALL
SELECT 6,'CA',27,'Developer',2 UNION ALL
SELECT 7,'CA',28,'Developer',2 UNION ALL
SELECT 8,'CA',22,'Developer',1 UNION ALL


similarly 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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-01 : 12:44:27
In 2000, it will be like

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
)t
INNER JOIN extract_type et
ON et.volume<=t.Seq
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-01 : 12:45:41
and in sql 2005

SELECT 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
)t
INNER JOIN extract_type et
ON et.volume<=t.Seq
Go to Top of Page

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
)t
INNER JOIN extract_type et
ON et.volume<=t.Seq

-- For CA it should show only 8 records and 5 should have extract_type_id = 1,2 records should have
extract_type_id = 2 and 1 record with extract_type_id = 3



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 10:24:51
what does this return?
select * from extract_type

Go to Top of Page

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.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 12:40:07
ok
Go to Top of Page
   

- Advertisement -