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
 General SQL Server Forums
 New to SQL Server Programming
 sample values

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-10-09 : 12:02:40
hi,

i have a table:

create table percent
(id int,
gender int,
city int)


and i would like to have a query that will - based on input parameteres - return me desired rows.

input parameters are:
1. number_of_rows_returned
2. percent of gender (based on value 1)
3. percent of city (based on value 1)

some sample data:

insert into percent
select 1, 1, 1 union all
select 2, 1, 2 union all
select 3, 2, 1 union all
select 4, 2, 1 union all
select 5, 1, 2 union all
select 6, 2, 2 union all
select 7, 2, 1 union all
select 8, 1, 1 union all
select 9, 1, 2 union all
select 10, 2,1


i want to have returned all rows that fit criteria, based on input parameters:
1. number_of_rows_returned = 4
2. percent of gender = 50% (meaning 50% male, 50% female; male=1, female=2)
3. percent of city = 75% (meaning 75% city, 25% village; city=1, village=2)

so i want to have returned four rows,
where by column gender i have 2 rows with value 1 and 2 rows with value 2
and by column city i have 3 rows with value 1 and 1 row with value 2.

expected output:
id | gender | city
------------------
6 | 2 | 2
7 | 2 | 1
1 | 1 | 1
8 | 1 | 1

or any other combination. it does not really matter. e.g.
id | gender | city
------------------
1 | 1 | 1
5 | 1 | 2
3 | 2 | 1
4 | 2 | 1

thank you

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-10-11 : 12:31:51
khm... :)
Go to Top of Page
   

- Advertisement -