help the join of two tables
HelloI need your help in the script below:
I have two tables.
My "table1" table contains the data below:
Code:
Name, Value
------------
12 A, 1
12 B, 1
12 C, 1
Table2 contains the following data:
Code:
value, result
------------
1.12
1.24
1.56
1 423
1.32
1, 3
I need to join based on a field value.
My result is:
Code:
NAME, VALUE, RESULT
-------------------------
12 A, 1, 12
12 B, 1: 24
12 C, 1, 56
12d, 1, 423
12TH, 1, 32
12F, 1, 3
Based on the number of records in the second table, it must add A to Z at the end of the name field. The number of records exceeds no more than 26. How can we achieve this?
_________________
Thank you
Pocard
OK, now you give other useful information - that there always will be combinations of amounts in table2 to match values in table1. (It is difficult to help when you say the specs one both :-))
But it is not easy, because the code should really try to consider all combinations and then 'choose the right' - it's easy for us humans, but not easy to code in the programming logic.
I made an attempt:
SQL> set linesize 120
SQL> with table1 as (
2 select 'A1' name, 123 id, 150 value from dual union all
3 select 'A2' name, 123 id, 200 value from dual union all
4 select 'A3' name, 123 id, 300 value from dual
5 ), table2 as (
6 select 123 id, 100 value from dual union all
7 select 123 id, 100 value from dual union all
8 select 123 id, 50 value from dual union all
9 select 123 id, 100 value from dual union all
10 select 123 id, 100 value from dual union all
11 select 123 id, 100 value from dual union all
12 select 123 id, 100 value from dual
13 )
14 --
15 -- End of test data
16 --
17 select
18 t1.id, t1.name, t1.value, t2.value,
19 t1.rn, t1.minval, t1.maxval,
20 t2.rn, t2.sumval
21 from (
22 select
23 tab1.*,
24 nvl(sum(tab1.value) over (
25 partition by tab1.id
26 order by tab1.rn
27 rows between unbounded preceding and 1 preceding
28 ),0) minval,
29 sum(tab1.value) over (
30 partition by tab1.id
31 order by tab1.rn
32 rows between unbounded preceding and current row
33 ) maxval
34 from (
35 select
36 table1.*,
37 row_number() over (
38 partition by table1.id
39 order by table1.value desc
40 ) rn
41 from table1
42 ) tab1
43 ) t1
44 join (
45 select
46 tab2.*,
47 sum(tab2.value) over (
48 partition by tab2.id
49 order by tab2.rn
50 ) sumval
51 from (
52 select
53 table2.*,
54 row_number() over (
55 partition by table2.id
56 order by table2.value desc
57 ) rn
58 from table2
59 ) tab2
60 ) t2
61 on (t2.id = t1.id)
62 where t2.sumval > t1.minval
63 and t2.sumval <= t1.maxval
64 order by
65 t1.id,
66 t1.rn,
67 t2.rn
68 ;
ID NA VALUE VALUE RN MINVAL MAXVAL RN SUMVAL
---------- -- ---------- ---------- ---------- ---------- ---------- ---------- ----------
123 A3 300 100 1 0 300 1 100
123 A3 300 100 1 0 300 2 200
123 A3 300 100 1 0 300 3 300
123 A2 200 100 2 300 500 4 400
123 A2 200 100 2 300 500 5 500
123 A1 150 100 3 500 650 6 600
123 A1 150 50 3 500 650 7 650
7 rows selected.
It doesn't seem to work for your sample data, but it is much too simple a rule at work in general. My "rule" is simply of sorts data according to the value descending and adding up to what 'enough' of values have been added.
Consider this example of data instead of this:
SQL> with table1 as (
2 select 'A1' name, 1 id, 100 value from dual union all
3 select 'A2' name, 1 id, 200 value from dual union all
4 select 'A3' name, 1 id, 300 value from dual union all
5 select 'B1' name, 2 id, 100 value from dual union all
6 select 'B2' name, 2 id, 200 value from dual
7 ), table2 as (
8 select 1 id, 25 value from dual union all
9 select 1 id, 75 value from dual union all
10 select 1 id, 50 value from dual union all
11 select 1 id, 50 value from dual union all
12 select 1 id, 175 value from dual union all
13 select 1 id, 225 value from dual union all
14 select 2 id, 25 value from dual union all
15 select 2 id, 50 value from dual union all
16 select 2 id, 75 value from dual union all
17 select 2 id, 100 value from dual union all
18 select 2 id, 50 value from dual
19 )
20 --
21 -- End of test data
22 --
23 select
24 t1.id, t1.name, t1.value, t2.value,
25 t1.rn, t1.minval, t1.maxval,
26 t2.rn, t2.sumval
27 from (
28 select
29 tab1.*,
30 nvl(sum(tab1.value) over (
31 partition by tab1.id
32 order by tab1.rn
33 rows between unbounded preceding and 1 preceding
34 ),0) minval,
35 sum(tab1.value) over (
36 partition by tab1.id
37 order by tab1.rn
38 rows between unbounded preceding and current row
39 ) maxval
40 from (
41 select
42 table1.*,
43 row_number() over (
44 partition by table1.id
45 order by table1.value desc
46 ) rn
47 from table1
48 ) tab1
49 ) t1
50 join (
51 select
52 tab2.*,
53 sum(tab2.value) over (
54 partition by tab2.id
55 order by tab2.rn
56 ) sumval
57 from (
58 select
59 table2.*,
60 row_number() over (
61 partition by table2.id
62 order by table2.value desc
63 ) rn
64 from table2
65 ) tab2
66 ) t2
67 on (t2.id = t1.id)
68 where t2.sumval > t1.minval
69 and t2.sumval <= t1.maxval
70 order by
71 t1.id,
72 t1.rn,
73 t2.rn
74 ;
ID NA VALUE VALUE RN MINVAL MAXVAL RN SUMVAL
---------- -- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 A3 300 225 1 0 300 1 225
1 A2 200 175 2 300 500 2 400
1 A2 200 75 2 300 500 3 475
1 A1 100 50 3 500 600 4 525
1 A1 100 50 3 500 600 5 575
1 A1 100 25 3 500 600 6 600
2 B2 200 100 1 0 200 1 100
2 B2 200 75 1 0 200 2 175
2 B1 100 50 2 200 300 3 225
2 B1 100 50 2 200 300 4 275
2 B1 100 25 2 200 300 5 300
11 rows selected.
In this data set simple ranking by value won't work - it should have been A3: (225,75), A2: (175,25) and A1: (50.50).
I can't really think of a reasonably easy way to do this in SQL only. Maybe using the clause TYPE would be possible, but not negligible. It is possible, it would be easier to solve this problem in PL/SQL in iterating through a few tables and intelligently to try different combinations, rather than creating all combinations in a huge piece of brute force SQL.
I'm sorry, Pandeesh, but I can't think a solution easily.
I might be able to do something, if I fiddled with the problem for a few days, but that would be beyond the scope of this forum. It would be a consultation of employment rather than a little help from the forum :-)
Tags: Database
Similar Questions
-
How it warns Oracle to use an index for the join of two tables...
How to prevent the Oracle to use an index for the join of two tables to get a view online that is used in an update statement?
O.K. I think I should explain what I mean:
When you join two tables that have many entries sometimes there're better is not to use an index on the column that is used as a criterion to join.
I have two tables: table A and table B.
Table A has 4,000,000 entries and table B has 700,000 entries.
I have a join of two tables with a numeric column as join criteria.
There is an index on this column in A table.
So I instead of
I want to usewhere (A.col = B.col)
in order to avoid Oracle using the index.where (A.col+0 = B.col)
When I use the join in a select query, it works.
But when I use the join as inline in an update statement I get the error ORA-01779.
When I remove the '+ 0' the update statement works. (The column is unique in table B).
Any ideas why this happens?
Thank you very much in advance for any help.
Hartmut cordiallyYou plan to use a NO_INDEX hint as shown here: http://www.psoug.org/reference/hints.html
-
How do good group when the join of two tables?
Hi all
The slot using join query, I'd like to get an output will depend on the number of 'Classes' (for example: if I want the result for 15 classes, then, he must return to 16 lines from 0 to 16).
I n query below, I use the group as
."GROUP BY report_parameters.report_parameter_value "
Error that says "this is not group by expression.
If I have commented here, here he returns 320 rows instead of 16 ranks.
Could someone help me?
Thank you.SELECT 'SUM(' || 'CASE ' || 'WHEN edr_class_by_gvw_report_data.bin_id >= ' || report_range_parameters.report_parameter_min_value || ' AND edr_class_by_gvw_report_data.bin_id < ' || report_range_parameters.report_parameter_max_value || ' THEN edr_class_by_gvw_report_data.bin_value ' || 'ELSE 0 ' || 'END ' || ') "Class ' || report_parameters.report_parameter_value || '" ' FROM report_parameters JOIN report_range_parameters ON report_parameters.report_parameter_id = report_range_parameters.report_parameter_id WHERE report_range_parameters.report_parameter_id = 2316 AND report_range_parameters.report_parameter_group = 'GVW_GROUP' AND report_range_parameters.report_parameter_name = 'GVW_NAME' AND report_parameters.report_parameter_group = 'CLASS' AND report_parameters.report_parameter_name = 'CLASS' GROUP BY report_parameters.report_parameter_value ORDER BY report_range_parameters.report_parameter_min_value ASC;
Published by: user10641405 on June 11, 2009 12:23
Published by: user10641405 on June 11, 2009 12:30Hello
Let me explain what I'm trying to do.
I want to write a query that produces this output:SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 0 AND edr_class_by_gvw_report_data.gvw < 5 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 0" SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 5 AND edr_class_by_gvw_report_data.gvw < 10 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 1" SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 10 AND edr_class_by_gvw_report_data.gvw < 15 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 2" SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 15 AND edr_class_by_gvw_report_data.gvw < 20 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 3" SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 20 AND edr_class_by_gvw_report_data.gvw < 25 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 4"
using data from tables report_parameters and report_range_parameters, which are like your tables.
Right now, I don't have report_parameters and report_range_parameters tables.
I need to create and put the data in them that is like your data.
If I have the same data you have, and if I can produce the results that you want from the data, so I'll post my query, and you can use it.I do not see how to INSERT statements like this:
INSERT INTO "class_by_gvw_report_data" SELECT site_id, site_lane_id, site_direction_id, site_direction_name, bin_start_date_time, bin_end_date_time, bin_id, bin_value FROM "class_by_gvw_bin_data"
can help me. Is the table "class_by_gvw_report_data" invloved in this problem? Is this the same as report_parameters or report_range_parameters? If Yes, I need to create it, and put the data into it, so to use the above statement I need all data of "class_by_gvw_bin_data".
I need to have something that I can run, so that my paintings will have sample data which would result in the desired output.
I need the CREATE TABLE statements, but I can probably guess what they should be if I have your data. If this isn't the case, I will seek further clarification later.
I can't guess what your data, and why is it causing problems. If I just do a few data, LII is virtually no chance he's going against hepatitis has a relationship between the tables that are causing you problems of the same nature.Please post INSERT statements that I can run and produce the same type of data that is causing problems.
-
Kindly help me with the request to find the data in two tables
Hello Guru
Kindly help me to recover the data from two tables-
BASEBALL
LEGAL_ENT_ID (PK)
GAME_ID (FK)
LEGAL_ENT_NM
INACTIVE_DT
DATE OF INS_TS
INS_LOGIN
DATE OF UPD_TS
UPD_LOGIN
FOOTBALL
GAME_ID (PK)
BRKR_NM,
BRKR_ISR_ID
BROKER_SYMBOL
INACTIVE_DT
BRKR_SWIFT_FLG
BRKR_INTERNAL_FLG
BRKR_CATEGORY
UPD_TS
MINORITY_FLG
BROKER_TYP
STATUS
INS_TS
INS_LOGIN
UPD_LOGIN
APP_USER
ACTIVE_FLG
and if I want fecth data from these two tables according to the following condition then it is fine with the suite of applications.
1 select distinct values only table of BASEBALL by using the following query.
SELECT DISTINCT B.GAME_ID as 'CLEARING GAME ID', B.BRKR_NM "NAME of THE GAME of COMPENSATION" OF BASEBALL A, FOOTBALL B WHERE A.BROKER_RELATION_CD IN ('FUTBRKR1', 'FUTBRKR2') AND A.GAME_ID = B.GAME_ID
2 Select all the table BRKR_NM OF FOOTBALL as well by using the query - next
SELECT GAME_ID "RUNNING GAME ID", 'NAME OF THE GAME OF EXECUTION' BRKR_NM SOCCER
Now, my query is that--
I want a query that gives me a combination of above mentioned queries... and if I tried to use Union or Union All, then she is not giving me the result as expected.
I like the result to look like who has a few conditions such as -
1 - the records in the table Football are high vs Baseball table because there is no condition to filter the records of the Football.
2 - football is a superset of records and Baseball is a subset.
3 - COMPENSATION NOM_JEU and RUNNING NOM_JEU may return the same values as well.
I want the result to be in the following form-
EXECUTION ID GAME | NAME OF THE GAME TO RUN. COMPENSATION ID GAME | DELETE THE NAME OF THE GAME.
2123 test1 2345 test5
2456 test10 2456 test10
Thanks in advance. Kindly help me.
Published by: user555994 on January 4, 2011 23:48In the output you want.
All the values of baseball;
Values of football that are matched;
But on what condition you want to match? -
problem with join of two tables
Hi, consider the following data
what I want to do, is to join data2 with data by isi, ri or southern id table and produce this output.WITH data AS ( SELECT 123 cid, 'aaa' isi, 'kkk' sud, 'ttt' ri FROM dual UNION ALL SELECT 222 cid, 'bbb' isi, 'gggg' sud, 'hhh' ri FROM dual ) , data2 AS ( SELECT 'aaa' isi, 'yyy' sud, 'ooo' ri FROM dual UNION ALL SELECT 'qqq' isi, 'ggg' sud, 'ooo' ri FROM dual UNION ALL SELECT 'uuu' isi, 'ppp' sud, 'ttt' ri FROM dual UNION ALL SELECT 'ppp' isi, 'mmm' sud, 'nnn' ri FROM dual )
It's how I got the above output:cid txt === ==== 123 aaa 222 ggg 123 ttt
first try to join isi in database2 with isi in "features" and discover if line matches. If this is the case, display the cid of the data table and the use of the value to match.
e.g. in the first line of table data2, we can see that isi = aaa matches line an isi data table. If the output displays 123 and value aaa.
If the rank table two of the Data2 isi = qqq. When join them with the data table, we find not all isi with qqq. in this case, it must join with column of South. We can see that South = ggg data2 games
the second line in the table of output so will bee 222 and value ggg.
now, take a look at the third row in Database2. ISI = uuu is not found in the data table when join them. then try to join to the South. South = ttt is also not found in any row of the data table. so join with ri. We
can see that ri = ttt data2 matches with ri = ttt in the table of data online 1. Therefore, we display the value.
can someone help me write a sql query that joins these two tables by isi. If no line found then join South, if no found rows then join in ri? Thank youHello
elmasduro wrote:
Hi Frank, data2.sud = 'Gay' (4 characters) assume to be South = "ggg". by mistake, I type extra g.I have another question about the request. What happens if I want to bring rows of data.2 even if they do not exist in the data table for the entire column was join?
for example, I want to show this:cid txt === ==== 123 aaa 222 ggg 123 ttt ppp --row from data2
the columns of the last row of data.2 does not match any column in the data table. in this case, I want to make but cid will be null. I tried outer join on your request, but it looks like I can not clause or with outer join
SELECT d1.cid , CASE WHEN d1.isi = d2.isi THEN d2.isi WHEN d1.sud = d2.sud THEN d2.sud ELSE d2.ri END AS txt FROM data d1 JOIN data2 d2 ON d1.is(+)i = d2.isi OR d1.sud(+) = d2.sud OR d1.ri(+) = d2.ri ;
How can I re - write the query for this case? Thanks again for your help
Here is the outer join syntax:
SELECT d1.cid , CASE WHEN d1.isi = d2.isi THEN d2.isi WHEN d1.sud = d2.sud THEN d2.sud WHEN d1.ri = d2.ri THEN d2.ri ELSE d2.isi END AS txt FROM data2 d2 LEFT OUTER JOIN data d1 ON d1.isi = d2.isi OR d1.sud = d2.sud OR d1.ri = d2.ri ;
The + sign for outer joins only works with the old join syntax, non-ANSI. Do not try to mix the two different ways to do joins.
Not to mention that the outer join, don't forget to change the CASE expression, to include the new possibility of any columns 3 join matching.
-
Join of two tables in two different schemas
Hi all
I have an obligation to join two tables on two different schemas. How to join these two tables in the object view.
Thanks in advance.
Concerning
Kaushik GuillauminYou can do just that using schema name in the +' view object.table name ' + according to the query object and also grant select the another schema to this schema user
ex
you need schem is test and another Act you need to a view object based on the test to join a table on shcema Actyou write semply object sql view code:
Act.table name
and you can also give him select statement on Bill schem table to test
concerning
-
display the data in two tables.
Hi considers the following data
I want to join these two tables and result the followingWITH table1 AS ( SELECT 111 userid, To_Date('7/31/2010','mm/dd/yyyy') dt FROM dual UNION ALL SELECT 111 userid, To_Date('8/1/2010','mm/dd/yyyy') dt FROM dual UNION all SELECT 111 userid, To_Date('8/2/2010','mm/dd/yyyy') dt FROM dual UNION ALL SELECT 111 userid, To_Date('8/3/2010','mm/dd/yyyy') dt FROM dual UNION ALL SELECT 111 userid, To_Date('8/4/2010','mm/dd/yyyy') dt FROM dual UNION ALL SELECT 111 userid, To_Date('8/5/2010','mm/dd/yyyy') dt FROM dual UNION ALL SELECT 222 userid, To_Date('2/28/2010','mm/dd/yyyy') dt FROM dual UNION all SELECT 222 userid, To_Date('3/5/2010','mm/dd/yyyy') dt FROM dual UNION all ), mydates AS ( SELECT To_Date('7/31/2010','mm/dd/yyyy') dt1, To_Date('8/4/2010','mm/dd/yyyy') dt2 FROM dual UNION ALL SELECT To_Date('2/28/2010','mm/dd/yyyy') dt1, To_Date('3/5/2010','mm/dd/yyyy') dt2 FROM dual )
Basically, I join two tables, take dt from table1 and look into mydates. If the data match then display dt and DM2.DT DT2 7/31/2010 8/04/2010 8/01/2010 8/01/2010 8/02/2010 8/02/2010 8/03/2010 8/03/2010 8/04/2010 8/04/2010 8/05/2010 8/05/2010 2/28/2010 3/5/2010 3/5/2010 3/5/2010
If the date in table1 does not match date in t1d in mydates, then display the data but dt and DM2 there same date.
for example,.
7/31 in table1 is 7/31 in mydates so I show 7/31 dt and DM2 as 8/4(from mydates).
now 8/01 does not match all lines in my dates so dt should be 8/01 and T2D should be 8/01. same logic applies to the other lines
can someone write a query that gives the above result? Thank youHello
If you want all lines of table1 (attached to mydates when this is possible, but even when there is no match) then use an outer join:
SELECT t.dt , NVL (m.dt2, t.dt) AS dt2 FROM table1 t LEFT OUTER JOIN mydates m ON t.dt = m.dt1 ORDER BY t.dt ;
-
Please, help me to join of two tables?
Hello..
I'm using oracle 11g.
I have two tables with below description
1.esb_v_study_personnel
study_code_alias varchar2,
Site_ID number,
role_at_site_level_desc varchar2
2 esb_v_study_site
study_code_alias varchar2
Site_ID
I'm counting the role_at_site_level_desc where it is'Monitor' or 'primary monitor'
So I wrote the following query
Select study_code_alias, site_id,.
no_of_study_monitors (role_at_site_lvl_desc)
esb_v_study_site_personnel
study_code_alias IN & arg_trial_code
role_at_site_lvl_desc in ()"Monitor" "Main screen"()
Group of study_code_alias, study_site_id
order by study_site_id
using the above, I'm getting lines of 1617.
but I want to map the resultant site_id
esb_v_study_site with lines of 1647. If I want to get all the site_id in esb_v_study_site table mapping to above query.
Can you guide me on this point.
I tried with a left outer join, but still iam getting lines of 1617.
Select study_code_alias,
no_of_study_monitors (role_at_site_lvl_desc)
esb_v_study_site_personnel
study_code_alias IN & arg_trial_code
role_at_site_lvl_desc in ()"Monitor" "Main screen"()
Group of study_code_alias, study_site_id
order of study_site_id) a
LEFT OUTER JOIN
ON a.trial_no = d.trial_no et a.study_site_id = d.study_site_id
où d.study_code_alias in & arg_trial_code
Group by d.study_code_alias, d.study_site_id, a.no_of_study_monitors
order by d.study_site_id
Please help me on this?
I'm not quite sure, because your query seems to us of small missing pieces. He was probably down during some copy & paste on this forum.
I guess the reason is that the left join is performed after the group where the count on the column does not count NULL values that might be created by the outer join.
Maybe you can start with the following query and give us the results and tell us if it is always wrong and what the outcome would be expected.
example (not tested the syntax)
select d.study_code_alias ,d.site_id , count(a.role_at_site_lvl_desc) no_of_study_monitors , count(distinct a.role_at_site_lvl_desc) no2 , count(*) no3 from esb_v_study_site d left join esb_v_study_site_personnel a on a.trial_no=d.trial_no and a.study_site_id=d.study_site_id and a.role_at_site_lvl_desc in ('Field Monitor','Primary Monitor') where d.study_code_alias IN (&arg_trial_code) group by d.study_code_alias,d.study_site_id order by d.study_site_id;
BTW: If you read the FAQ in the upper right corner, here, he said how well sql zip code search.
-
help in registration of the records from two tables
HI: I have two tables joined the first field. The field is the primary key in the first table. Need help listing records from both tables with each a line/record results.
I want to print each record in the table of EVENTS such as a line and the corresponding records in EVENT_UPDATES as a line like this recordcreate table EVENTS ( event_key varchar2(64) primary key, event_description varchar2(64), create_time int ); create table EVENT_UPDATES ( event_key varchar2(64) NOT NULL , update_description varchar2(64), update_time int ); insert into EVENTS values('Event1', 'This is event1', 1); insert into EVENT_UPDATES values('Event1', 'Ticket created', 3); insert into EVENT_UPDATES values('Event1', 'Event cleared', 10); insert into EVENTS values('Event2', 'This is event2', 4); insert into EVENT_UPDATES values('Event2', 'Ticket created', 6); insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);
TIAEvent1 1 This is event1 3 Ticket created 10 Event cleared Event2 4 This is event2 6 Ticket created 8 Event cleared
Raviselect case weight when 1 then event_key end key, time_val, description from ( select event_key, create_time time_val, event_description description, 1 weight from events union all select event_key, update_time, update_description, 2 weight from event_updates ) order by event_key, weight / KEY TIME_VAL DESCRIPTION ---------- ---------- ------------------------- Event1 1 This is event1 3 Ticket created 10 Event cleared Event2 4 This is event2 6 Ticket created 8 Event cleared 6 rows selected. SQL>
SY.
-
Delete rows in a table when the columns from two tables match
Hello
I have following two tables.
===========================================
create the table empbooth as
(
Select 1 empid, 1 double cabin Union all the
Select option 2, Union 1 double all the
Select 3, Union 1 double all the
Select option 4, Union 2 double all the
Select option 5, 2 double
);
create the table attsht as
(
Select 1 empid, 240 reg, 0 unpaid all double union
Select option 2, reg 200, 0 unpaid of all the double union
Select 3, 240 reg, 0 unpaid all double them union
Select 4 480 reg, 0 unpaid all double union
Select 5 240 reg, unpaid double 0
);
=================================================
I want to remove rows from attsht where corresponding booth (which is stored in the empbooth table) is 1.
The condition is 'where attsht.empid = empbooth.empid and empbooth.booth = 1 '.
I use oracle 10g.
Help, please
delete from attsht where a.empid in (select b.empid from empbooth b where b.booth = 1)
or
remove from attsht a
where exists (select null
of empbooth b
where b.booth = 1
and b.empid = a.empid)
-
Take the difference of two tables
Hello
I have two tables where in I need to take the difference between the two columns of different tables and show as decimal below:
Table A
Name, salary
x 10.5
y - 10.32
z 5
Table B
Name, salary
x 10.5
y - 10.32
z 3
a 12
Output:
Name_A, Name_B, salary, remarks
x, x, 0, no diff
y, y, 0, no diff
z, z, 2 Diff is here
, one, no data, no data from the previous
Hello
Try ythis:
with table_a as
(
SELECT 'X' NAME, SALARY, 10.5 DUAL UNION ALL
SELECT THE NAME OF 'Y',-10.32 SALARY OF DOUBLE UNION ALL
SELECT THE NAME OF 'Z', 5 DOUBLE SALARY
)
table_B as
(
SELECT 'X' NAME, SALARY, 10.5 DUAL UNION ALL
SELECT THE NAME OF 'Y',-10.32 SALARY OF DOUBLE UNION ALL
SELECT THE NAME OF 'Z', 3 DOUBLE UNION ALL SALARIES
SELECT 'A' NAME, SALARY 12 DUAL FROM
)
SELECT
COALESCE(A.NAME,B.NAME) NAME
, NVL (TO_CHAR (A.SALARY - B.SALARY), 'NO DATA') SALARY
, CASE WHEN A.SALARY - B.SALARY = 0 THEN 'NO DIFFERENCE '.
WHEN B.SID IS NULL, "NO DATA OF THE PREVIOUS.
WHERE B.NAME IS NULL, "NO DATA FROM NEXT.
ANOTHER 'DIFFERENCE IS THERE.
END NOTEOf
TABLE_A HAS
TABLE_B FULL OUTER JOIN B ON (B.SID = B.NAME)
;NAME SALARY NOTE
---- ---------------------------------------- ---------------------
X 0 NO DIFFERENCE
Y 0 NO DIFFERENCE
Z 2 THE DIFFERENCE IS THERE
A NUMBER OF DATA NO DATA FROM THE PREVIOUSKind regards
Peter
-
Hello
I am trying to build a query WITH, but I can't seem to make it work:
WITH P1 AS (SELECT OT_VALUE
OF CSD_OPEN_VERY_HIGH_INCIDENTS VHIGH
WHERE OT_VALUE > = 0
). P2 AS (SELECT OT_VALUE
OF HIGH CSD_OPEN_HIGH_INCIDENTS
WHERE OT_VALUE > = 0
), SELECT VHIGH. OT_VALUE, TOP. OT_VALUE
P1 P2 JOIN INTERNALLY. RATING = P2. RANKING
The two tables have the same column names.
I want to only return a set of results if the two columns (OT_VALUE) contain in reality 1.
Select this check box. This is a job for all four images. This will return the line to the following combinations. 0.0 and 0.1 and 1.0 and 1.1. Importance is 1.2. Let me know if you face any problem
SELECT CASE WHEN ((VH_OT_VAL = 0 ET H_OT_VAL = 0) OR (VH_OT_VAL = 1 AND H_OT_VAL = 0) OR (VH_OT_VAL = 0 AND H_OT_VAL = 1) or (VH_OT_VAL = 1 AND H_OT_VAL = 1)) 1
ANOTHER ACE OF NULL TERMINATOR OT_VAL
DE)
SELECT H.OT_VALUE H_OT_VAL,
VH. OT_VALUE VH_OT_VAL
OF CSD_OPEN_VERY_HIGH_INCIDENTS VH.
CSD_OPEN_HIGH_INCIDENTS H
WHERE NVL (VH. CLASSIFICATION, 0) = NVL(H.CLASSIFICATION,0)
AND VH. IMPORTANCE IN (1,2)
AND H.IMPORTANCE IN (1,2)
AND NVL (VH. OT_GROUP, 0) = NVL(H.OT_GROUP,0)
AND VH. IMPORTANCE = H.IMPORTANCE
AND VH. TIME_STAMP = H.TIME_STAMP
);
-
The most elegant way to get the difference between two tables - not least!
Hello
Simplified example of what I'm trying to achieve - I have two tables ORIGINAL and REVISED.
My aim is to compare the two, such as; -
When there is data in the two tables I get the difference between the Budget column, and if there is no difference, so I don't want no lines.
When data exists in the ORIGINAL, but not in review, I want to the inverse of the current value of the Budget column.
Where the data exist in REVISED I want the REVISED value.
I can see how I can do this, see below, but is there a more elegant solution?
Data for the ORIGINAL table
Data for the REVISED tableselect '801040' entity, '2186' expense_type, 234000 budget from dual union all select '801040' entity, '3001' expense_type, 1000 budget from dual union all select '801040' entity, 'P132' expense_type, 34000 budget from dual union all select '801040' entity, 'P135' expense_type, 43000 budget from dual
Desired outputselect '801040' entity, '2186' expense_type, 235000 budget from dual union all select '801040' entity, 'P132' expense_type, 34000 budget from dual union all select '801040' entity, 'P139' expense_type, 56000 budget from dual
ENTITY EXPENSE_TYPE DIFFERENCE
------ ------------ ----------
801040 2186 1000
801040 3001-1000
801040 P135-43000
801040 P139 56000
5 selected lines.
Code current to achieve this, is there a better way?
Thanks a lot for your comments,.select original.entity , original.expense_type , (nvl(revised.budget,0) - original.budget) as difference from original , revised where original.entity = revised.entity(+) and original.expense_type = revised.expense_type(+) and (nvl(revised.budget,0) - original.budget) != 0 union all select revised.entity , revised.expense_type , revised.budget as difference from revised where not exists (select 'x' from original where original.entity = revised.entity and original.expense_type = revised.expense_type) and revised.budget != 0
Robert.
Published by: Robert Angel on January 17, 2012 03:31 to change is not equal to! = - Thanks for the heads upSQL> with original 2 as 3 ( 4 select '801040' entity, '2186' expense_type, 234000 budget 5 from dual 6 union all 7 select '801040' entity, '3001' expense_type, 1000 budget 8 from dual 9 union all 10 select '801040' entity, 'P132' expense_type, 34000 budget 11 from dual 12 union all 13 select '801040' entity, 'P135' expense_type, 43000 budget 14 from dual 15 ) 16 , revised 17 as 18 ( 19 select '801040' entity, '2186' expense_type, 235000 budget 20 from dual 21 union all 22 select '801040' entity, 'P132' expense_type, 34000 budget 23 from dual 24 union all 25 select '801040' entity, 'P139' expense_type, 56000 budget 26 from dual 27 ) 28 select * 29 from ( 30 select nvl(o.entity, r.entity) entity, 31 nvl(o.expense_type, r.expense_type) expense_type, 32 nvl(r.budget,0) - nvl(o.budget,0) budget 33 from original o 34 full join revised r 35 on o.entity = r.entity 36 and o.expense_type = r.expense_type 37 ) 38 where budget <> 0 39 / ENTITY EXPE BUDGET ------ ---- ---------- 801040 2186 1000 801040 P135 -43000 801040 3001 -1000 801040 P139 56000 SQL>
-
Hi all
I have two tables - TableA and TableB
I need all these records in TableA that have no matching records in TableB based on Key1, Key2, and Key3 and process them further.
Can someone help pls?
Hello 954475
Try this:
SELECT A.*
FROM TableA A
WHERE DOES NOT EXIST (SELECT 1
FROM TableB B
WHERE A.Key1 = B.Key1
AND A.Key2 = B.Key2
AND A.Key3 = B.Key3
)
;
I hope that helps!
-
How to find the data in two tables are the same?
Hi people,
Suppose we have table emp01, have 10 records and create another emp02 as table
create the table emp02 in select * from emp01;
now both the table has the same data.
How to find a 'data' in the two tables are the same?Hello
SELECT * FROM emp01 MINUS SELECT * FROM emp02 UNION SELECT * FROM emp02 MINUS SELECT * FROM emp01
You can also compare resultset hash to select * in emp01 and select * from emp02 by using dbms_sqlhash.
Best regards
Nikolai
Maybe you are looking for
-
Numbers - how to import external data to a cell in a single file into a new cell in another file? I want to do the same thing in numbers (3.6.2 (2577) I used to do in Excel (any version):) to import a calculation of a cell in a file in a new cell in
-
bike alert icon is not showing in my e bike
Motorola alert application does not appear in my device in the game store, however, it is shown that updated Please help locate
-
Services for SQL Server 2008 Express are not listed. No error message when installing.
Hello community, I installed SQL Server Express 2008 (without SP but with SQL Management Studio) on a Windows XP 2002 SP3 32-bit. Used to install operating system user is a member of the administrator groupThe SQLManagementStudio_x86_ENU.exe installa
-
Aspire connected bluetooth V3-731, signal drop internet wifi
What can I do to improve this
-
Need help connecting 2 routers
Hello, I was scratching around the search for a solution to my problem for a few weeks now. I have 2 routers (connected according to the picture below) - I have it set up so that I can collect access logs (one of the routers is bit I found that can c