How can I randomly distribute the figure among rows?

I have a table ABC with column A having following values -
A
a1
a2
a3
a4
a5
a6
I have a fixed figure say for example, 175. I want to randomly generate a value against each of the row so that the sum of values would be 175.
So that I would have output like
A value
a1 12
a2 13
a3 25
a4 100
a5 7
a6 18
or I could have something like
A value
a1 5
a2 30
a3 4
a4 120
a5 11
a6 5
The constrain is that the sum of values should be 175 and the values should be generated randomly.
Can I do this with the single query?

Here a variant that does not give negative results:
SQL> create table abc
  2  as
  3  select 'a1' a from dual union all
  4  select 'a2' from dual union all
  5  select 'a3' from dual union all
  6  select 'a4' from dual union all
  7  select 'a5' from dual union all
  8  select 'a6' from dual
  9  /
Tabel is aangemaakt.
SQL> var N number
SQL> exec :N := 175
PL/SQL-procedure is geslaagd.
SQL> select a
  2       ,   r2
  3         + case max(r) over ()
  4           when r then :N - sum(r2) over ()
  5           else 0
  6           end random_value
  7    from ( select a
  8                , r
  9                , round(r * :N / sum(r) over ()) r2
10             from ( select a
11                         , dbms_random.value r
12                         , rownum  -- to prevent view merging and bugs
13                      from abc
14                  )
15         )
16  /
A                            RANDOM_VALUE
a1                                     35
a2                                     15
a3                                     45
a4                                     36
a5                                      3
a6                                     41
6 rijen zijn geselecteerd.
SQL> /
A                            RANDOM_VALUE
a1                                     16
a2                                     14
a3                                     22
a4                                     40
a5                                     41
a6                                     42
6 rijen zijn geselecteerd.
SQL> /
A                            RANDOM_VALUE
a1                                     25
a2                                     24
a3                                      9
a4                                     28
a5                                     50
a6                                     39
6 rijen zijn geselecteerd.
SQL> /
A                            RANDOM_VALUE
a1                                     44
a2                                      6
a3                                     26
a4                                     39
a5                                     43
a6                                     17
6 rijen zijn geselecteerd.Because I've found that the other one did produce negative values sometimes:
SQL> var N2 number
SQL> exec :N2 := 25
PL/SQL-procedure is geslaagd.
SQL> select a,
  2     case when rn = 1 -- account for rounding errors
  3        then rdm_num1 + (175 - sum(rdm_num1) over ())
  4        else rdm_num1
  5     end rdm_num
  6  from (
  7     select a,
  8        round(rdm /sum(rdm) over (partition by null) * :N) rdm_num1,
  9        rownum rn
10     from (
11        select a,
12           trunc(dbms_random.value(1,:N2)) rdm -- randonm number
13        from abc)
14  )
15  /
A                                 RDM_NUM
a1                                    -64
a2                                     52
a3                                     52
a4                                     20
a5                                     63
a6                                     52
6 rijen zijn geselecteerd.
SQL> /
A                                 RDM_NUM
a1                                     24
a2                                     43
a3                                     34
a4                                     25
a5                                     13
a6                                     36
6 rijen zijn geselecteerd.
SQL> /
A                                 RDM_NUM
a1                                     62
a2                                     41
a3                                      4
a4                                     23
a5                                     26
a6                                     19
6 rijen zijn geselecteerd.
SQL> /
A                                 RDM_NUM
a1                                     -1
a2                                     17
a3                                     49
a4                                     54
a5                                     17
a6                                     39
6 rijen zijn geselecteerd.Regards,
Rob.

Similar Messages

  • How can I find out the first intial row in VA02-output transaction??

    How can I find out the first intial row in the screen which appears during sales order change (VA02) >>enter the sales order number >>after the first screen appears >>in the menu bar go to EXTRAS>>OUTPUT>>HEADER>>EDIT??
    I have to find out the first row in the OUTPUT column which is empty,then populate it with some data using BDC.
    usefull answers will be rewarded.
    regards,
    Shashank .

    Pass the sale order number in the object key of NAST table,
    if u get 3 entires, then u need to place ur entry in the fourth row in that table control thru BDC.
    I think u got the logic.
    Regards,
    Sujatha.

  • How can I get only the last 2 rows?

    How can I narrow down a query to get only the last two rows in a table ?
    Thanks,
    Mohan

    Thanks a lot Ram for your suggestion but already I have a query which returns a set of rows, of which I would like to further filter and get the last two rows
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Ramanuj Bangad ([email protected]):
    try out something like this if it helps.
    Example:
    select * from users
    where rownum <= (select count(*) from users)
    minus
    select * from users
    where rownum <= (select count(*) -2 from users )
    <HR></BLOCKQUOTE>
    null

  • How can I change a minus figure on a web page to show in red? Firefox3.6.8 winXP

    How can I change a minus figure on a web page to show in red?

    I would assume that the website sends negative number with a special CSS class code or uses JavaScript to to change the style afterward.
    Did it ever work in Firefox?
    Do you see a error in the Tools > Error Console regarding this problem?
    "Clear the Cache": Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    "Remove the Cookies" from sites that cause problems: Tools > Options > Privacy > Cookies: "Show Cookies"
    Start Firefox in [[Safe Mode]] to check if one of your add-ons is causing your problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    See [[Troubleshooting extensions and themes]] and [[Troubleshooting plugins]]

  • How can I get only the music I want onto my iPhone using iTunes Match

    how can I get only the music I want onto my iPhone using iTunes Match?  At present it is putting a random selection on; it has none of the same control as iTunes on my Mac.  And although I uploaded all my album artwork to iCloud, it is not transferring it back to my iPhone

    Iphone are meant to be sync to one itunes library only.
    You can copy those songs to a cd and then put a copy to your itunes media library.

  • How can I mix up the photos in a file?

    I am doing a slide show for my child's teacher.  I am using about 400 photos from 6 different events. How can I mix up the photos so the slide show is in a random order rather than chronological? 

    So, you're exporting photos from Lightroom, then rolling them into a slideshow using a different program, and that program does not support randomization - did I get that right?
    If so, then indeed: it's as Paige said: no way to export in random order without a plugin or script or something.
    Here's one possibility: put them in a collection, then shuffle the collection using the "Shuffle Photos In Collection" script available here:
    http://www.robcole.com/Rob/ProductsAndServices/MiscLrPlugins/#MiscScripts
    (they're free scripts I wrote - kinda like plugins, but simpler..).
    Then export all photos in the collection with a sequence number prefix so they're in proper order (alphabetically) on disk - then your slideshow program will have them in random order - makes sense? (try it before answering ).
    Rob

  • I have 300 albums of music on my ipod how can I random every song

    I have 300 albums of music on my ipod, how can I random the entire list?

    go to your songs not genre or albums DO NOT USE GENIUS then shuffle or if you want to listen to the songs on an album go to your albums and click on the one you want and shuffle (BTW what iPod gen is yours?)

  • How can we find out the info object in a query ....?

    How can we find out the info object in a query is belongs to that particular info cube? If the query is built on a Multiprovider having 4 different info cubes.

    Hi,
    If you have restrictions at the infoprovider level in the RKF then it is coming from that particular infocube.
    Or go to the selection option in the multiprovider...her you can get the mappings of which key figure is populted from which infocube.
    For characteristics you will have to look for identification tab in the characteristics option.
    Thanks

  • My macbook pro no longer connects to my TV with the mini dvi to hdmi adapter. A while ago the computer fell down and hit the cable. How can I tell if the computer is working ok. Doesn't show mirroring any more.

    My macbook pro no longer connects to my TV with the mini dvi to hdmi adapter. A while ago the computer fell down and hit the cable. How can I tell if the computer is working ok. Doesn't show mirroring any more. Was working while watching a movie but when I pulled it out from the computer and put it back in the system would no longer mirrorr the screen on the TV.

    The best option is an appointment at an Apple store genius bar.  The evaluation will be for FREE!
    Ciao.

  • How can I will declare the symbol u2018 (Single Quote) in the report

    Hi ,
    Could you please tell me how can I will declare the symbol u2018 (Single Quote) in the report.
    My requirement is that I have concate the data with single quote and after that I have to store the data in to an internal table and I have to download the data in the form text file in the presentation server.
    For example :
    Let the below data I want to download into the presentation serve in the format of text file by storing in internal table.
    Assume all are constants:
    1st line : abcu2019add
    2nd line :  defu2019gef
    Thanks in advance.

    Hi Jyothi,
    Thanks for the quick reply .
    I can agree with you are point but My requirement is like this I am explaining clearly.
    I have declared the internal table like this.
    DATA: BEGIN OF OTAB OCCURS 0,
             LINE (9024),
           END OF OTAB.
    So I have to append the each line item into the internal table.
    So I am explaining what the data I have to append
    Ist line contains
    'UNBUNOC:2020308u2019 where 020308 I will get the  date from reguh table
    2nd line contains:
    'DTM+20020510' where the 20020510 will be reference document number from the table reguh.
    So I want to declare a constant 'UNBUNOC:2
    2nd the date from reguh table
    And another constant u2018
    So that I can concate all the three and I can put into string and I will append into internal table and I can download the data into the presentation server.
    Please let me know if you need any more clarification regarding my requirement.
    Thanks in advance.

  • How can two pictures of the same size still appear to be a different size? (Photoshop CS6 problem)

    I called the big webdesign file "(X)" and the image I wanted to paste in it "(Y)",
    so that you won't be confused when you read it.
    What Happened
    For the design of a webpage, I made a photoshop-file(X) with a lot of squares(X). These squares(X) had the size 9,42×9,42 centimeters.
    I measured the squares(X) carefully and also made sure the size was in centimeters, so I am sure that the size was 9,42×9,42 cm.
    My plan was to fill the squares(X) with pictures(Y). I had a map on my computer with a lot of square-sized JPG-images(Y) in it. All I had
    to do was open the pictures(Y) in photoshop, change the size into 9,42×9,42 centimeters and copy+paste them into the squares(X)
    of the webdesign. I did this before and it worked perfectly.
    But then, I opened one of the images(Y) and when I wanted to resize it, the resize-tool said it was only 3,25×3,25 centimeters big.
    I thought it was strange, so I copied+pasted it into the design-file(X) with all the squares(X) that needed to be filled. Allthough photoshop
    had told me that the squares(X) in the file were 9,42×9,42 centimeters big and the image(Y) I resized was 3,25×3,25, the "small" image(Y)
    turned out to be bigger then the squares(X). And when I resized the "small" image(Y) into 9,42×9,42 centimeters, it was even bigger
    than it was before. How can this be, and how can I still make the images(X&Y) the same size?
    What I already tried
    - I made the file of the webdesign(X) with all the squares(X) into one layer, copied the file(X) and opened a new file(X) with the same
    measurements of the old file(X). I also changed the resolution of the file(X) into pixels/centimeters instead of pixels/inches.
    Then I pasted the image(X) of the old file into the new file. I didn't work; the image(X) that I pasted in the new file was bigger and
    when I pasted the other picture(Y) in this image, the same problem was still there.
    - I also opened the picture(Y) in photoshop, made a screenshot of it and then pasted the screenshot into paint. With paint I cropped
    it again and saved it as a JPEG-file. After that, I opened the image (Y) in photoshop again. It now had the size of 11,27×11,27 centimeters.
    Then I resized the image(Y) into 9,42×9,42 cm, but it still wasn't as big as the squares(X).
    Could you help me please?
    I really don't understand why this problem is happening and I can't think of a solution because I am still quite unexperienced with photoshop
    and I also couldn't find the answer on the internet. I assume the solution is very simple, but I haven't found it yet allthough I have to finish this
    project soon and I also have to use the image(Y). So any help would be very appreciated.
    Thanks,
    Simone

    You need to understand the basic of a digital image.
    Image is recorded as pixels and image is a rectangle so many pixels wide and so many pixel high.   Pixels do not have a fixed size. The images resolution setting defines the image current pixel size.
    So for example if you have and image that is 300 pixel wide and 200 pixels high and set its dip resolution to 100DPI the image size is 3" wide and 2" high.  Set its dip setting tp 300DPI the image size becomes 1" wide by 2/3" high.  Both the 3" x 2" and the 1"x2/3" images have the same 60000 RGB Colored Pixels.
    Web pages are sized to to be so many pixel wide and so many pixel high.  Often the height is not enforced scrolling is used when viewing. 
    However image for web pages often must not exceed so many pixel wide and high for displays have a fixed resolution and a fixed number of Pixels. You want images to fit on users displays.  Users use different displays some have high resolution displays and other have low resolution displays with far fewer pixels then high resolution displays.
    Web Image are normally image that have been interpolated down in number of pixels a resampled large image so that web image will fit on a low resolution  display with limited number of pixels. The image size the user see depend on the display they are currently using. A low resolution display's image will be much larger then the same image displayed on a nexus 10 with a 300dip display. Cheep low cost Desktop LCD have a 84DIP resolution, large high cost desktop displays are not be much higher in resolution somewhere near 108DPI.   Tablets, Laptop and Phones can have much higher resolution like the nexus 10 300DPI IPS display

  • How can I save to the same map every time when printing pdfs?

    How can I save to the same map every time when printing pdfs?
    Finder points to the document map even when I chose a different map recently.
    I often print series of pdfs from the print dialog box, I'd like to choose the map to save to and then have all subsequent pdf prints automatically directed to the same map until I decide otherwise.

    that link seems to be broken right now:
    403 Error - Forbidden  - No cred, dude.

  • I have itunes on a mac with 2 users but the library only shows on the main user how can both users have the same library?

    i have itunes on a mac with 2 users but the library only shows on the main user how can both users have the same library?

    Hi,
    Have a Look at these Links on Home Sharing:
    http://support.apple.com/kb/HT4620
    http://support.apple.com/kb/HT3819
    Could be what you need...
    Cheers,

  • I downloaded 11.1 but now can't access the store, can only view my library. I've reinstalled but still get a blank, white screen when I try to go to the itunes store. How can I get to the store for purchases?

    I downloaded 11.1 but now can't access the store, can only view my library. I've reinstalled but still get a blank, white screen when I try to go to the itunes store. How can I get to the store for purchases?

    In iTunes prefs>Store - is the Store check box unchecked?
    Did you repair pemissions with Disk Utility?
    MJ

  • How can I sync ONLY the top photo in the stack?

    I sync various Aperture photo albums to my IPad and iPhone. The problem is that when I have 'stacked' images (eg. one image showing in the album and two other copies 'hidden' underneath), all of the photos in the stack are copied to the ipad and iPhone. So instead of seeing just one version of an image, I see as many as are in the stack. How can I sync ONLY the top photo in the stack?
    Thanks for your help!

    I solved my own problem.
    The sync problem seemed to only occur on smart albums. There is a new check box option in Aperture 3 when making a smart album: "sync stack pick only." One click and the problem is solved!

Maybe you are looking for