Can I produce such a result with a single query?
I am having a table with records like
A....B
1....20
2....19
3....03
4....09
5....74
6....24
7....31
Column A is a primary key and Column B has radomly generated positive integer numbers. I need to generate one more column C with randomly generated positive integer values (including 0) so that -
1) The values genereated for the Column C must be less than or equal to corresponding value of Column B.
2). The randomly generated values for Column C should be in such a way so that the sum of values for Column C would be 30% of the sum of values of Column B.
In our example, the sum(B) = 180. So the sum(C) should be around 54.
One of the snap of column C can be like this...
A....B....C
1....20....14
2....19....00
3....03....02
4....09....03
5....74....03
6....24....01
7....31....31
......==-..==
.....180...54
How do I write a query to produce such a result?
You may try the following:
SQL> CREATE TABLE t AS SELECT ROWNUM a,ROUND(DBMS_RANDOM.VALUE(10,99)) b FROM user_objects WHERE ROWNUM <= 15
Table created.
SQL> BREAK on l
SQL> COMPUTE SUM OF b on l
SQL> COMPUTE SUM OF c on l
SQL> SELECT a,
b,
c,
l
FROM (SELECT a,
b,
c,
ROUND (30 / 100 * s) s,
SUM (c) OVER (PARTITION BY l) d,
l
FROM (SELECT a,
b,
l,
ROUND (DBMS_RANDOM.VALUE (1, b)) c,
SUM (b) OVER (PARTITION BY l) s
FROM t,
(SELECT LEVEL l
FROM DUAL
CONNECT BY LEVEL <= 50000)))
WHERE d = s
A B C L
1 12 3 184
2 40 31
3 79 7
4 46 8
5 18 7
6 14 3
7 48 12
8 32 8
9 67 16
10 25 20
11 23 17
12 22 2
13 24 8
14 74 5
15 48 25
572 172 sum
1 12 3 10298
2 40 25
3 79 32
4 46 6
5 18 6
6 14 4
7 48 4
8 32 26
9 67 31
10 25 2
11 23 3
12 22 12
13 24 7
14 74 3
15 48 8
572 172 sum
1 12 8 13450
2 40 5
3 79 12
4 46 13
5 18 11
6 14 11
7 48 8
8 32 22
9 67 3
10 25 4
11 23 2
12 22 14
13 24 12
14 74 40
15 48 7
572 172 sum
1 12 1 16656
2 40 31
3 79 3
4 46 20
5 18 9
6 14 5
7 48 18
8 32 24
9 67 16
10 25 5
11 23 7
12 22 2
13 24 6
14 74 23
15 48 2
572 172 sum
1 12 5 19842
2 40 11
3 79 11
4 46 12
5 18 2
6 14 2
7 48 3
8 32 16
9 67 10
10 25 11
11 23 17
12 22 18
13 24 6
14 74 26
15 48 22
572 172 sum
1 12 3 22337
2 40 18
3 79 9
4 46 26
5 18 11
6 14 9
7 48 25
8 32 14
9 67 6
10 25 3
11 23 21
12 22 12
13 24 10
14 74 4
15 48 1
572 172 sum
1 12 5 24501
2 40 11
3 79 48
4 46 3
5 18 6
6 14 8
7 48 2
8 32 3
9 67 21
10 25 7
11 23 16
12 22 2
13 24 16
14 74 12
15 48 12
572 172 sum
1 12 8 24555
2 40 29
3 79 7
4 46 13
5 18 5
6 14 4
7 48 20
8 32 3
9 67 17
10 25 13
11 23 18
12 22 2
13 24 5
14 74 27
15 48 1
572 172 sum
1 12 7 27312
2 40 11
3 79 3
4 46 37
5 18 3
6 14 7
7 48 16
8 32 8
9 67 27
10 25 2
11 23 14
12 22 4
13 24 20
14 74 9
15 48 4
572 172 sum
1 12 3 29197
2 40 12
3 79 24
4 46 17
5 18 6
6 14 9
7 48 17
8 32 17
9 67 4
10 25 23
11 23 15
12 22 4
13 24 5
14 74 2
15 48 14
572 172 sum
1 12 8 32635
2 40 5
3 79 21
4 46 1
5 18 9
6 14 6
7 48 20
8 32 6
9 67 43
10 25 10
11 23 15
12 22 5
13 24 4
14 74 15
15 48 4
572 172 sum
1 12 2 38404
2 40 26
3 79 6
4 46 28
5 18 4
6 14 2
7 48 4
8 32 13
9 67 19
10 25 3
11 23 3
12 22 11
13 24 13
14 74 32
15 48 6
572 172 sum
1 12 10 38830
2 40 5
3 79 8
4 46 3
5 18 2
6 14 12
7 48 42
8 32 5
9 67 27
10 25 4
11 23 9
12 22 11
13 24 11
14 74 10
15 48 13
572 172 sum
1 12 2 42291
2 40 5
3 79 34
4 46 24
5 18 15
6 14 7
7 48 7
8 32 10
9 67 1
10 25 5
11 23 20
12 22 8
13 24 3
14 74 7
15 48 24
572 172 sum
1 12 5 47614
2 40 6
3 79 15
4 46 32
5 18 6
6 14 12
7 48 8
8 32 7
9 67 6
10 25 22
11 23 8
12 22 3
13 24 17
14 74 13
15 48 12
572 172 sum
225 rows selected.
Similar Messages
-
Mercedez Benz COMAND System not recognizing iPhone 5 after iOS 8 update, can we expect the same result with iPhone 6 or 6 plus?
I'm planning to purchase the 6 plus, but I need to know from where.whom will the fix for this problem emerge.My new iPhone 6 paired perfectly with my Mercedes system. That said, on your iPhone 5, reset your network settings and then try to pair it again.
-
Today I purchased an ATT mifi liberate hotspot to use with my early 2011 MBP, along with the iPad Air (128GB, WiFionlyI also purchased today. I've been testing out all the settings on the hotspot, but can't seem to get results with the GPS switch. GPS just spins, searches, doesn'. I can access the hotspots settings in Safari on the laptop and still the problem persisits. I did install some new GPS drivers recommended by AT&T, that doesn't seemed to have work. Could it be that there is a port wrong somewhere? Can anybody make heads or tails of this? Any assistance is gratefully appreciated...
Today I purchased an ATT mifi liberate hotspot to use with my early 2011 MBP, along with the iPad Air (128GB, WiFionlyI also purchased today. I've been testing out all the settings on the hotspot, but can't seem to get results with the GPS switch. GPS just spins, searches, doesn'. I can access the hotspots settings in Safari on the laptop and still the problem persisits. I did install some new GPS drivers recommended by AT&T, that doesn't seemed to have work. Could it be that there is a port wrong somewhere? Can anybody make heads or tails of this? Any assistance is gratefully appreciated...
-
How can I pass/add these results in my Main query's column
Hello Experts
I’m working on a subquery. My subquery works fine and give the results what I need. But how can I pass/add these results in my Main query’s column ‘MGRNAME’. Here is my query
select pc.c_id "Project ID",
cn.c_id "Client ID", cn.c_last "Last Name", cn.c_first "First Name",
pj.mgr_id "Manager ID", pj.project_name "Project", MGRNAME
from project_consultant pc
join consultant cn
on (pc.c_id = cn.c_id)
join project pj
on (pc.p_id = pj.p_id)
where lower(cn.c_last) = 'myers'
and MGRNAME in
( select concat (substr(c_last,1,20),
substr(c_first,1,20))
as MGRNAME from consultant
where c_id in (102, 103)
When I run the above query I got error.
Thanks a lot on advanceI’ve join three tables to get the information about I need. I got everything working expert one part. The subquery
( select concat (substr(c_last,1,20),
substr(c_first,1,20))
as MGRNAME from consultant
where c_id in (102, 103)
Gives me the names of employees I need. The output comes 2 names. I’m trying to add these 2 names in my main query and show the results but I got the following error
Error at Command Line:11 Column:4
Error report:
SQL Error: ORA-00904: "MGRNAME": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
I can understand there is something wrong with MGRNAME field but not sure what it is. Can somebody point it out?
Once again this is main query with single sub query
select pc.c_id "Project ID",
cn.c_id "Client ID", cn.c_last "Last Name", cn.c_first "First Name",
pj.mgr_id "Manager ID", pj.project_name "Project",
MGRNAME
from project_consultant pc
join consultant cn
on (pc.c_id = cn.c_id)
join project pj
on (pc.p_id = pj.p_id)
where lower(cn.c_last) = 'myers'
and MGRNAME in
( select concat (substr(c_last,1,20),
substr(c_first,1,20))
as MGRNAME from consultant
where c_id in (102, 103)
Hope it helps -
How can I update the table with a single query for...
I have a table with columns C1 and C2.
C1 C2
A1 null
A1 null
A1 null
A1 null
A2 null
A2 null
A2 null
A3 null
A4 null
A4 null
I want to update my table with a single query so that I would have data like
C1 C2
A1 1
A1 2
A1 3
A1 4
A2 1
A2 2
A2 3
A3 1
A4 1
A4 2
The updated column C2 has the values like serial no grouped on the column C1.SQL> create table mytable
2 ( c1 varchar2(2)
3 , c2 number(2)
4 )
5 /
Tabel is aangemaakt.
SQL> insert into mytable (c1)
2 select 'A1' from dual union all
3 select 'A1' from dual union all
4 select 'A1' from dual union all
5 select 'A1' from dual union all
6 select 'A2' from dual union all
7 select 'A2' from dual union all
8 select 'A2' from dual union all
9 select 'A3' from dual union all
10 select 'A4' from dual union all
11 select 'A4' from dual
12 /
10 rijen zijn aangemaakt.
SQL> select * from mytable
2 /
C1 C2
A1
A1
A1
A1
A2
A2
A2
A3
A4
A4
10 rijen zijn geselecteerd.
SQL> merge into mytable t1
2 using (select c1
3 , row_number() over (partition by c1 order by null) rn
4 , rowid rid
5 from mytable
6 ) t2
7 on (t1.rowid = t2.rid)
8 when matched then
9 update set c2 = rn
10 when not matched then
11 insert values (null,null)
12 /
10 rijen zijn samengevoegd.
SQL> select * from mytable
2 /
C1 C2
A1 1
A1 2
A1 3
A1 4
A2 1
A2 2
A2 3
A3 1
A4 1
A4 2
10 rijen zijn geselecteerd.Regards,
Rob. -
Insert into two tables with a single query (same ID)
Hello,
I want to insert two tables at the same time ( with a single query) provided that both records get inserted with the same id. How do I do this?
Table Movies
id
name
Table Category
movie_id
cat_typea) Insert into first table, retrieve the id (may be by using my_sequence.currval and then insert into another table.
issue: Makes three query to the db, I am also guessing that when multiple people try to insert there will be an issue, I might be wrong.
I don't have any other idea.
Greatly appreciated!Why don't use multitable insert ? It's available from 9i.
A sequence.nextval will return the same value within the whole instruction
so all records can be inserted with the same id.
Look at this example:
DROP TABLE A;
DROP TABLE B;
drop sequence a_seq;
CREATE TABLE A(
ID NUMBER,
FIRSTNAME VARCHAR2(50)
CREATE TABLE B AS
SELECT id, firstname lastname FROM a;
CREATE SEQUENCE a_seq
START WITH 1;
INSERT ALL
INTO A(ID, FIRSTNAME) VALUES(A_SEQ.NEXTVAL, FNAME)
INTO B(ID, LASTNAME) VALUES(A_SEQ.NEXTVAL, LNAME)
SELECT 'fname ' || LEVEL FNAME, 'lname ' || LEVEL LNAME
FROM DUAL
CONNECT BY LEVEL < 10
COMMIT;
SELECT * FROM A;
SELECT * FROM b;
DROP TABLE A succeeded.
DROP TABLE B succeeded.
drop sequence a_seq succeeded.
CREATE TABLE succeeded.
CREATE TABLE succeeded.
CREATE SEQUENCE succeeded.
18 rows inserted
commited
ID FIRSTNAME
3 fname 1
4 fname 2
5 fname 3
6 fname 4
7 fname 5
8 fname 6
9 fname 7
10 fname 8
11 fname 9
9 rows selected
ID LASTNAME
3 lname 1
4 lname 2
5 lname 3
6 lname 4
7 lname 5
8 lname 6
9 lname 7
10 lname 8
11 lname 9
9 rows selected -
Can I produce an app preview with my Mac Mini and my iPad 2?
I want to produce an app preview with my Mac Mini (~2009), which is running Yosemite, and my iPad 2, which is running iOS 8. Is this possible, even though the iPad 2 doesn't have a lightning connector? If it is, what are the steps to do this? I am not tech-illiterate, but I'm new to Mac.
Thanks for your time!Any bluetooth speaker will do, there are thousands. Range is 20 to 35 ft
-
Can Free add-ons be downloaded with a single Adobe ID and applied to a classroom of computers?
Can Adobe's free extensions and add-ons be downloaded with a single Adobe ID and applied to a classroom of computers? Specifically a situation where a computer technician has a request from a teacher for a particular free extension (such as Kuler) which the technician then downloads with his or her Adobe ID and installs on all computers in a classroom.
Hi,
This question is difficult to answer because it depends how the classroom of computers were set up from a licensing perspective but in talking to some fellow Adobe colleagues we do think this is technically feasible. I do not have a classroom of computers to test this with to hand and even if I did it could be different to your environment so I would love for you to try it out and give it a go. Here's how I think this could work:
Sign in to the Creative Cloud for desktops app with the Adobe ID you want to use. It should automatically sync and install your free extensions, if not you can force a reinstall here:
http://creative.adobe.com/addons/my_addons
Then repeat the process for each machine. If this does not work you can just download the extension and install it using Adobe Extension Manager CC on each machine, it's a little more work but that method will definitely work.
Please let me know how you get on.
Thanks,
Jonathan -
Can I exchange a multi app with a single app?
Is it possible to update/replace a multi-folio app with a single-app version ?
the key to sharing apps is that they all have to be on the same Apple ID. So if you have the same Apple ID on all the devices, they can share apps.
You may want to research into messages, etc because that can be one of the pitfalls of sharing the ID's. -
Can I do this with a single query?
I have table A like
Table A
node_id node_no
N1......01
N2......02
N3......03
N4......04
N5......05
Using Table A, I need to populate the Table B having relationship like
Table B
link_id pred_node_id succ_node_id
L1..... N1...........N2
L2..... N2...........N3
L3..... N3...........N4
L4..... N4...........N5
There is a sequencial link between the nodes. We need to arrange the nodes ordered by their node_no so that the sequencial like for nodeds would be 01,02,03,04,05,.... Between each pair of ordered nodes, a link is to be created i.e.
a Link L1 links node N1 (pred_node) and N2 (succ_node),...
Do I have any ways so that I can populate the table B with a single insert...select statement?SQL> create table tablea
2 as
3 select 'N1' node_id, '01' node_no from dual
4 union all
5 select 'N2','02' from dual
6 union all
7 select 'N3','03' from dual
8 union all
9 select 'N4','04' from dual
10 union all
11 select 'N5','05' from dual
12 /
Table created.
SQL> create table tableb(link_id varchar2(10), pred_node_id varchar2(10),succ_node_id varchar2(10))
2 /
Table created.
SQL> insert into tableb
2 select * from
3 (
4 select 'L'||to_number(node_no) link_id,node_id,lead(node_id) over(partition by 1 order by to_number(node_no)) succ_node_id
5 from tablea
6 )
7 where succ_node_id is not null
8 /
4 rows created.
SQL> select * from tableb;
LINK_ID PRED_NODE_ SUCC_NODE_
L1 N1 N2
L2 N2 N3
L3 N3 N4
L4 N4 N5
SQL> -
How to insert multiple records with a single query?
Hi,
I've to save a huge number of installments with their other information such as due on blah blah. Now, I want to add the all of these information at once with a single insert query.
How can I do that?Hi
What is your source data?
If the source is external to the SQL Server (like a log file, Excel, CSV, JSON, XML, external application...) you can and should insert it all using Bulk Insert operation.
Pls clarify what is your source data, and if you need more help using Bulk Insert.
https://www.simple-talk.com/sql/learn-sql-server/bulk-inserts-via-tsql-in-sql-server/
http://msdn.microsoft.com/en-us/library/ms188365.aspx
[Personal Site] [Blog] [Facebook] -
How can I produce a title card with multiple lines of text?
When I drag "title, multiple lines" to my timeline, the second line appears in a smaller font. When I type all my text on a single line, the title appears in a teeny-tiny font. I want to be able to produce four or five lines of text, all in a readable font. Help!
2 solutions:
Costs money and takes work:
Look for a title plug-in on geethree.com. There are free ones out there too.
OR.
Works right now:
Type your title into the 'scrolling block' title.
Drop it in and let it render.
Play it back and pause it when the text is in the middle (or where ever you want it). Choose 'create still clip' from the edit menu. And blamo! You've got multiple lines of the same sized text.
This might take some experimenting to find what font works best. Some get a little jaggy in the rendering process and look bit-mapped a bit when changed into a still.
Give it a shot! -
How Can I produce a Pro_Res files with 5.1 (6 channels of audio)
I need to screen a feature film in a digital Theater and I want to make sure the Dolby Digital Mix is heard the way the film was mixed and not the way FCP does a 2 channel mix down.
I have the timeline setup with the 6 channels but when I go to export the file to deliver one QT to the exhibitor, it won't let me export the 5.1 mix.
I've read several post, Exported the file with Compressor but when I check the channels my audio was not exported the right way.
Any ideas?there is this tutorial online @ Creative Cow from Shane Ross:
http://library.creativecow.net/articles/ross_shane/multi-audio/video-tutorial
Are they screening from the QT or from tape? -
Can Numbers produce a scatter graph with differently sized dots?
I want to graph a set of points in a scatter graph where each dot will have a different size. How can I do that? How do I have to set up the table? is it a scatter graph or is it a different type of graph that I have to use? which one?
Hello
Welcome to the club.
As far as I know, the responce is No.
We may ungroup the chart's objects and edit them but it would be a tedious task.
You may go to "Provide Numbers Feedback" in the "Numbers" menu, describe what you wish.
Then, cross your fingers, and wait for iWork'09
Yvan KOENIG (from FRANCE mercredi 16 janvier 2008 15:11:51) -
How can I display multiple column results of a sql query?
I'm wanting to display the results of a query selecting from multiple columns, but I can't seem to find an item type that will allow this. I've tried LOV, multiselect, display as text and others. Any ideas on how to do this. Here is my basic query:
Select ul.meaning,
rqa.submission_date,
rqa.location_num
from
cfa_rqa_doc_submit_dates rqa,
cfa_user_lookups ul
where
rqa.location_num in (Select location_num from cfa_current_locations_mv
where operator_person_id = :P1_PERSON_ID and substr(location_num,1,1) = '8')
and
ul.lookup_type_id='1269'
and
to_char(rqa.submission_date, 'month') = to_char(sysdate, 'month')
and
rqa.submission_date >= (SYSDATE-365)
and ul.lookup_ID= rqa.document_type_id
;Hi,
Do you just wish to display the result of the query? If so, you can simply parse it out in HTML.
You can then choose if you want to use tables or css.
Multiple column layout in tables are based on <tr> and <td>.
<tr> is a table row, where <td> is a column.
All tags should be closed.
Example:
<table>
<tr>
<td>col1</td><td>col2</td>
</tr>
<tr>
<td colspan="2">spans over 2 cols</td>
</tr>
</table>If you want to do the same in css:
(Seems this forum removes the styles!):
float:left; width:50; << first div and 2 div (col 1 and col 2 style)
float:left;clear:both; width:100%; << clear both, double width (spans over 2)
<div>
<div style="float:left; width:50;">col1</div><div style="float:left;width:50%;">col2</div>
<div style="float:left;clear:both; width=100%;">Spans over 2 cols</div>
</div>The div might look more complicated, but if you refrain from styling in the tags them selves, you can define different styles in the .css file.
You need the clear:both to start a new line with the divs that float.
I usually run a dedicated "new line" div, without content.. (exception beeing the html-entities version of ' ' (& nbsp ; << without the spaces)).
eg.:
<div style="cls"> </div>Parsing out the values from the PL SQL you do like so:
begin
declare
cursor c_search is
Select ul.meaning,
rqa.submission_date,
rqa.location_num
from
cfa_rqa_doc_submit_dates rqa,
cfa_user_lookups ul
where
rqa.location_num in (Select location_num from cfa_current_locations_mv
where operator_person_id = :P1_PERSON_ID and substr(location_num,1,1) = '8')
and
ul.lookup_type_id='1269'
and
to_char(rqa.submission_date, 'month') = to_char(sysdate, 'month')
and
rqa.submission_date >= (SYSDATE-365)
and ul.lookup_ID= rqa.document_type_id
begin
htp.p('<div style="width:80%;">');
for v_search in c_search loop
htp.p('<div style="float:left;clear:none;">'||v_search.location_num||'</div>');
htp.p('<div style="float:left;clear:none;">'||v_search.submission_date||'</div>');
end loop;
htp.p('</div>');
end;
end;Ps. hope I understood you correctly, also this code is untested.. but I hope it can still help.
Edited by: Olav Alexander Mjelde on May 12, 2010 9:36 AM
Maybe you are looking for
-
AfContext.launchDialog not working with upgrade to 11.1.1.2.0
I recently upgraded my project to latest version and now some code stopped working correctly...here is the code which is suppose to show a new dialog via backing bean (there is code setting a bunch of parameters dynamically and why I'm using backing
-
I have looked all over this forum and in other places and cannot find the answer to this question. Is it possible to set an instance of an object or a variable to something to the equivalent of the "Application scope" in ColdFusion. I need to be able
-
NSSharingService on mountain lion without sharing window
On mountain lion, I try the new sharing possiblities with the NSSharingService class of AppKit.framework Everything goes fine with this three lines of code NSArray* array = @[ @"myText", [NSImage imageNamed:@"myImageFile"] ]; NSSharingService* sharin
-
Webcam (iSight) probs with 10.1 update...
Hi all, Me too having webcam probs ever since the last update. Is there any way to go back to the previous version of Flash Player? Or if anyone has a solution for this static image: Info: MacBook Pro Mid-2010 running with all updates 64-bit kernel r
-
BAPI_SFLIGHT_GETLIST "expired"
Hello, I have a problem with my minisap installation. I would like to use the example function module BAPI_SFLIGHT_GETLIST. Problem is it never returns any result. The reason is simple: all flight dates are older then the system date but the query ch