MS SQL + Percentage from List - Random

I am trying to create a view from a table  where the view is a percentage and random.

So the view always has 38% of the list in the table

ie:  select 38% of table where submitdate between '08/01/2015' and '08/30/2015' and CustomerScore < 7

But i want the 38% to be stored in another table so that value can adjust from month to month.
dkilbyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
and the question is?
Somewhat pedantic perhaps, sorry, but there is no actual question, just a series of statements.

I suspect you will need a function, not a view, for this.
& are you also expecting these to be flexible?
where submitdate between '08/01/2015' and '08/30/2015' and CustomerScore < 7
are these the only likely filtering conditions?

{+ edit}
oh, and can you expend on what you mean by random?
for example, do you mean, that the 38% any be any percentage (0 to 100)
or, do you mean, the rows returned to meet the 38% are randomly chosen
0
dkilbyAuthor Commented:
I want to pull back 38% of the rows that meet the criteria of

where submitdate between '08/01/2015' and '08/30/2015' and CustomerScore < 7

and the questions is this possible and if so how would the query look ?

thanks
0
PortletPaulfreelancerCommented:
http://sqlfiddle.com/#!3/5a5c5/1
declare @pct int = 38 -- our 38% variable

select
*
from (
  select *
  , count(*) over() as totsample
  , row_number() over(order by newid()) as rn
  from table1
  ) x
where rn <= (@pct * 100) / x.totsample
order by rn
;

Open in new window

| Val | totsample | rn |
|-----|-----------|----|
|  82 |       100 |  1 |
|  66 |       100 |  2 |
|  18 |       100 |  3 |
|  98 |       100 |  4 |
|  24 |       100 |  5 |
|  72 |       100 |  6 |
|  62 |       100 |  7 |
|  34 |       100 |  8 |
|  78 |       100 |  9 |
|  65 |       100 | 10 |
|  45 |       100 | 11 |
|  94 |       100 | 12 |
|   7 |       100 | 13 |
|  90 |       100 | 14 |
|   3 |       100 | 15 |
| 100 |       100 | 16 |
|  58 |       100 | 17 |
|  73 |       100 | 18 |
|  29 |       100 | 19 |
|  71 |       100 | 20 |
|  92 |       100 | 21 |
|  33 |       100 | 22 |
|  22 |       100 | 23 |
|  51 |       100 | 24 |
|  11 |       100 | 25 |
|  36 |       100 | 26 |
|  96 |       100 | 27 |
|  32 |       100 | 28 |
|  60 |       100 | 29 |
|  39 |       100 | 30 |
|  85 |       100 | 31 |
|  59 |       100 | 32 |
|  55 |       100 | 33 |
|  30 |       100 | 34 |
|   5 |       100 | 35 |
|  77 |       100 | 36 |
|  84 |       100 | 37 |
|  56 |       100 | 38 |
        

Open in new window

CREATE TABLE Table1
    ([Val] int)
;
    
INSERT INTO Table1
    ([Val])
VALUES
    (1),
    (2),
    (3),
    (4),
    (5),
    (6),
    (7),
    (8),
    (9),
    (10),
    (11),
    (12),
    (13),
    (14),
    (15),
    (16),
    (17),
    (18),
    (19),
    (20),
    (21),
    (22),
    (23),
    (24),
    (25),
    (26),
    (27),
    (28),
    (29),
    (30),
    (31),
    (32),
    (33),
    (34),
    (35),
    (36),
    (37),
    (38),
    (39),
    (40),
    (41),
    (42),
    (43),
    (44),
    (45),
    (46),
    (47),
    (48),
    (49),
    (50),
    (51),
    (52),
    (53),
    (54),
    (55),
    (56),
    (57),
    (58),
    (59),
    (60),
    (61),
    (62),
    (63),
    (64),
    (65),
    (66),
    (67),
    (68),
    (69),
    (70),
    (71),
    (72),
    (73),
    (74),
    (75),
    (76),
    (77),
    (78),
    (79),
    (80),
    (81),
    (82),
    (83),
    (84),
    (85),
    (86),
    (87),
    (88),
    (89),
    (90),
    (91),
    (92),
    (93),
    (94),
    (95),
    (96),
    (97),
    (98),
    (99),
    (100)
;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkilbyAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.