Need help with conditional join....
Table 1 - Student
ID# Name
1 # A
2 # B
3 # C
4 # D
Table2 - Marks
ID Marks Display
1 # 10 # Y
1 # 20 # Y
1 # 14 # N
2 # 12 # N
2 # 13 # N
3 # 12 # Y
Result...Need query to do this?..
Want to join above two tables and display marks as X when there is no ID in table marks or there is ID but all marked with display as 'N'...if there is one or more
marked with Y then display with marks..
I am using oracle 11i.
ID NAme Marks
1 # A # 10
1 # A # 20
2 # B # X
3 # C # 12
4 # D # X
Or, using ANSI join syntax:
with Table1 as (
select 1 id,'A' name from dual union all
select 2,'B' from dual union all
select 3,'C' from dual union all
select 4,'D' from dual
Table2 as (
select 1 id,10 marks,'Y' display from dual union all
select 1,20,'Y' from dual union all
select 1,14,'N' from dual union all
select 2,12,'N' from dual union all
select 2,13,'N' from dual union all
select 3,12,'Y' from dual
-- end of on-the-fly data sample
select t1.id,
name,
nvl(to_char(marks),'X') marks
from Table1 t1
left join
Table2 t2
on (
t2.id = t1.id
and
display = 'Y'
order by id
ID NAME MARKS
1 A 10
1 A 20
2 B X
3 C 12
4 D X
SQL> SY.
Similar Messages
-
Need help with conditional query
guys this is just an extension of this post that Frank was helping me with. im reposting because my requirements have changes slightly and im having a hell of a time trying to modify the query.
here is the previous post.
need help with query that can look data back please help.
CREATE TABLE "FGL"
"FGL_GRNT_CODE" VARCHAR2(60),
"FGL_FUND_CODE" VARCHAR2(60),
"FGL_ACCT_CODE" VARCHAR2(60),
"FGL_ORGN_CODE" VARCHAR2(60),
"FGL_PROG_CODE" VARCHAR2(60),
"FGL_GRNT_YEAR" VARCHAR2(60),
"FGL_PERIOD" VARCHAR2(60),
"FGL_BUDGET" VARCHAR2(60)
)data
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');
I need to find the greatest grant year for the grant by a period parameter.
once i find the greatest year i need to check the value of period 14 for that grant for the previous year and add it to the budget amount for that grant. however if their is an entry in the greatest year for period 00 then i need to ignore the period 14 of previous year and do this calculation current period +(current period - greatest year 00)
hope that makes sense so in other words with the new data above. if i was querying period two of grant year 11. i would end up with $800
because the greatest year is 11 it contains a period 0 with amount of $400 so my total should be
period 2 amount $ 600
period 0 amount $ 400 - period 2 amount of $600 = 200
600+200 = $800
if i query period 1 of grant 360055 i would just end up with 800 of grnt year 10.
i have tried to modify that query you supplied to me with no luck. I have tried for several day but im embarrased to say i just can get it to do what im trying to do .
can you please help me out.
here is the query supplied by frank kulash who gracefully put this together for me.
WITH got_greatest_year AS
SELECT fgl.* -- or whatever columns are needed
, MAX ( CASE
WHEN fgl_period = :given_period
THEN fgl_grnt_year
END
) OVER () AS greatest_year
FROM fgl
SELECT SUM (fgl_budget) AS total_budget -- or SELECT *
FROM got_greatest_year
WHERE ( fgl_grnt_year = greatest_year
AND fgl_period = :given_period
OR ( fgl_grnt_year = greatest_year - 1
AND fgl_period = 14
;MiguelHi, Miguel,
Are you waying that, when the greatest year that has :given_period also has period='00' (or '0', or whatever you want to use), then you want to double the budget from the given_period (as well as subtract the budget from the '00', and not count the pevious year's '14')? If so, add another condition to the CASE statement which decides what you're SUMming:
WITH got_greatest_year AS
SELECT TO_NUMBER (fgl_grnt_year) AS grnt_year
, fgl_period
, TO_NUMBER (fgl_budget) AS budget
, MAX ( CASE
WHEN fgl_period = :given_period
THEN TO_NUMBER (fgl_grnt_year)
END
) OVER () AS greatest_year
FROM fgl
, got_cnt_00 AS
SELECT grnt_year
, fgl_period
, budget
, greatest_year
, COUNT ( CASE
WHEN grnt_year = greatest_year
AND fgl_period = '00'
THEN 1
END
) OVER () AS cnt_00
FROM got_greatest_year
SELECT SUM ( CASE
WHEN grnt_year = greatest_year -- New
AND fgl_period = :given_period -- New
AND cnt_00 > 0 THEN budget * 2 -- New
WHEN grnt_year = greatest_year
AND fgl_period = :given_period THEN budget
WHEN grnt_year = greatest_year
AND fgl_period = '00' THEN -budget
WHEN grnt_year = greatest_year - 1
AND fgl_period = '14'
AND cnt_00 = 0 THEN budget
END
) AS total_budget
FROM got_cnt_00
;You'll notice this is the same as the previous query I posted, except for 3 lines maked "New". -
Need assistance with conditional join in SQL
Hi All -
I need to ask for help with this query:
Create table user_tab_col_test (table_name varchar2(30), column_name varchar2(30), data_type varchar2(30));
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column1', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column2', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column3', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table1', 'column4', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column1', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column2', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column3', 'varchar2')
Insert into user_tab_col_test (table_name, column_name, data_type) values ('table2', 'column4', 'varchar2')
Commit;
Create table all_cons_columns_test (table_name varchar2(30), column_name varchar2(30), constraint_name varchar2(30))
Insert into all_cons_columns_test (table_name, column_name, constraint_name) values ('table1', 'column1', 'primary')
Insert into all_cons_columns_test (table_name, column_name, constraint_name) values ('table1', 'column1', 'secondary')
Commit;
This is my query and the current result:
Select u.table_name, u.column_name, c.constraint_name
From user_tab_col_test u
Left outer join all_cons_columns_test c
On ( u.table_name = c.table_name
AND U.COLUMN_NAME = C.COLUMN_NAME
AND C.CONSTRAINT_NAME IN ('primary'))
order by U.table_name, U.COLUMN_NAME;
TABLE_NAME COLUMN_NAME CONSTRAINT_NAME
table1 column1 primary
table1 column2
table1 column3
table2 column1
table2 column2
Three questions:
1) I only want to return results where table_name = 'table1'. I can't seem to get this to work.
2) Is my query proper and is this the best way to return my desired results? I.e. I want all columns from user_tab_col_test and I only want to display the constraint_name from all_cons_columns_test if the constraint_name = 'primary'.
3) Will the synatx be the same if I need to join a third table to all_cons_columns_test?
Any advice/suggestions are much appreciated -
john
Edited by: user703358 on Jan 11, 2013 8:57 PM
Edited by: user703358 on Jan 11, 2013 9:48 PMHi,
user703358 wrote:
... ALL_CONSTRAINTS_TEST joins to ALL_CONS_COLUMNS_TEST on TABLE_NAME and CONSTRAINT_NAME. If you adapt this to use the data dictionary views ALL_CONSTRAINTS and ALL_CONS_COLUMNS, then rememeber to join on the OWNER column, also.
Ultimately I want to use ALL_CONSTRAINTS_TEST.CONSTRAINT_TYPE = 'P' in my WHERE clause, instead of C.CONSTRAINT_NAME IN (primary), only because the constraint_type is a more definitive attribute than just the name of the constraint.
I tried something like the query below but I'm getting
ORA-00904: "U"."COLUMN_NAME": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 8 Column: 16Errors like that are caused by trying to mix old join syntax with ANSI join syntax in the same query.
SELECT u.table_name
, u.column_name
, C.CONSTRAINT_NAME
FROM USER_TAB_COL_TEST U,
ALL_CONSTRAINTS_TEST ACAbove is an example of an old-style join: there is a comma between the table names, and the join conditions are included in the WHERE clause below.
LEFT OUTER JOIN all_cons_columns_test c
ON u.table_name = c.table_name
AND U.COLUMN_NAME = C.COLUMN_NAMEThis is an example of an ANSI-style join: the keyword JOIN appears between the table names, and the join conditions are right here, after the keyword ON.
WHERE U.TABLE_NAME = 'table1'
AND C.TABLE_NAME = AC.TABLE_NAME
AND C.CONSTRAINT_NAME = AC.CONSTRAINT_NAME
and AC.CONSTRAINT_TYPE = 'P'
ORDER BY u.table_name
, u.column_name
;While it is possible to use both join styles in the same query, it's a really bad idea. I suggest always using ANSI syntax, especially for outer joins, but whatever style you choose, use it consistently for all joins in the same query.
In this case, you want an inner join between tablec c and ac, so the ANSI syntax would be something like
FROM u LEFT OUTER JOIN c ON ... JOIN ac ON ...In this case, it's very important that the inner join between c and ac is done before the outer join with u. How can you make sure that happens? Well, if you have an arithmetic expression such as
12 * 5 - 1and you want to make sure the subtraction of 1 from 5 takes place before the multiplication by 12, what do you do? You can add parentheses:
12 * ( 5 - 1 )In ANSI join syntax, you can use parentheses the same way:
FROM u LEFT OUTER JOIN ( c ON ... JOIN ac ON ... )or, in full:
SELECT u.table_name
, u.column_name
, c.constraint_name
, ac.constraint_type
FROM user_tab_col_test u
LEFT OUTER JOIN (
all_cons_columns_test c
JOIN all_constraints_test ac
ON ac.table_name = c.table_name
AND ac.constraint_name = c.constraint_name
ON u.table_name = c.table_name
AND u.column_name = c.column_name
AND ac.constraint_type = 'P'
WHERE u.table_name = 'table1'
ORDER BY u.table_name
, u.column_name
;Output:
TABLE_NAME COLUMN_NAME CONSTRAINT_NAME CONSTRAINT_TYPE
table1 column1 primary P
table1 column2
table1 column3
table1 column4 -
Need help with self join query
Hello,
I have table A with the following data
oid parent_oid
10 4
4 2
2 2
12 6
6 6
parent_oid is the parent of oid. I'd like a query that shows the final parent of the oid. The result should show the following
oid final parent
10 2
4 2
2 2
12 6
6 6
I'm using Oracle 10g. I'm familiar with self joins, but that alone will not do the job. Thanks!Hi,
arizona9952 wrote:
... I'm familiar with self joins, but that alone will not do the job.You're absolutely right!
A 2-way self join would work for rows have no parent, or rows that are directly connected to their final ancestor (such as oid=4), but not for anything farther away.
A 3-way self-join would work for one more level away from the final row, but no more. That would be enough for the small set of sample data that you posted, but it would not work if you added a new row with parent_id=10.
An N-way self-join would work for up to N+1 levels, but no more.
You need something that can go any number of levels, such as CONNECT BY:
SELECT CONNECT_BY_ROOT oid AS oid
, parent_oid AS final_parent
FROM a
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY oid = PRIOR parent_oid
AND oid != parent_oid
;Edited by: Frank Kulash on Feb 22, 2010 7:09 PM
Upon sober reflection, I think that a Top-Down query, like the one below, would be more efficient than a Bottom-Up query, like the one above:
SELECT oid
, CONNECT_BY_ROOT parent_oid AS final_parent
FROM a
START WITH parent_oid = oid
CONNECT BY parent_oid = PRIOR oid
AND oid != PRIOR oid
; -
Need help with query joining several tables into a single return line
what i have:
tableA:
puid, task
id0, task0
id1, task1
id2, task2
tableB:
puid, seq, state
id0, 0, foo
id0, 1, bar
id0, 2, me
id1, 0, foo
id2, 0, foo
id2, 1, bar
tableC:
puid, seq, date
id0, 0, 12/21
id0, 1, 12/22
id0, 2, 12/22
id1, 0, 12/23
id2, 0, 12/22
id2, 1, 12/23
what i'd like to return:
id0, task0, 12/21, 12/22, 12/22
id1, task1, 12/23, N/A, N/A
id2, task2, 12/22, 12/23, N/A
N/A doesn't mean return the string "N/A"... it just means there was no value, so we don't need anything in this column (null?)
i can get output like below through several joins, however i was hoping to condense each "id" into a single line...
id0, task0, 12/21
id0, task0, 12/22
id0, task0, 12/23
id1, task1, 12/23
is this possible fairly easily?
Edited by: user9979830 on Mar 29, 2011 10:53 AM
Edited by: user9979830 on Mar 29, 2011 10:58 AMHi,
Welcome to the forum!
user9979830 wrote:
what i have:...Thanks for posting that so clearly!
Whenever you have a question, it's even better if you post CREATE TABLE and INSERT statements for your sample data, like this:
CREATE TABLE tablea
( puid VARCHAR2 (5)
, task VARCHAR2 (5)
INSERT INTO tablea (puid, task) VALUES ('id0', 'task0');
INSERT INTO tablea (puid, task) VALUES ('id1', 'task1');
INSERT INTO tablea (puid, task) VALUES ('id2', 'task2');
CREATE TABLE tablec
( puid VARCHAR2 (5)
, seq NUMBER (3)
, dt DATE -- DATE is not a good column name
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 0, DATE '2010-12-21');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 1, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 2, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id1', 0, DATE '2010-12-23');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 0, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 1, DATE '2010-12-23');This way, people can re-create the problem and test their ideas.
It doesn't look like tableb plays any role in this problem, so I didn't post it.
Explain how you get the results from that data. For example, why do you want this row in the results:
PUID TASK DT1 DT2 DT3
id0 task0 12/21/2010 12/22/2010 12/22/2010rather than, say
PUID TASK DT1 DT2 DT3
id0 task0 12/22/2010 12/21/2010 12/22/2010? Does 12/21 have to go in the first column because it is the earliest date, or is it because 12/21 is related to the lowest seq value? Or do you even care about the order, just as long as all 3 dates are shown?
Always say what version of Oracle you're uisng. The query below will work in Oracle 9 (and up), but starting in Oracle 11, the SELECT ... PIVOT feature could help you.
i can get output like below through several joins, however i was hoping to condense each "id" into a single line... Condensing the output, so that there's only one line for each puid, sounds like a job for "GROUP BY puid":
WITH got_r_num AS
SELECT puid
, dt
, ROW_NUMBER () OVER ( PARTITION BY puid
ORDER BY seq -- and/or dt
) AS r_num
FROM tablec
-- WHERE ... -- If you need any filtering, put it here
SELECT a.puid
, a.task
, MIN (CASE WHEN r.r_num = 1 THEN r.dt END) AS dt1
, MIN (CASE WHEN r.r_num = 2 THEN r.dt END) AS dt2
, MIN (CASE WHEN r.r_num = 3 THEN r.dt END) AS dt3
, MIN (CASE WHEN r.r_num = 4 THEN r.dt END) AS dt4
FROM tablea a
JOIN got_r_num r ON a.puid = r.puid
GROUP BY a.puid
, a.task
ORDER BY a.puid
;I'm guessing that you want the dates arranged by seq; that is, for each puid, the date related to the lowest seq comes first, regardless of whther that date is the earliest date for that puid or not. If that's not what you need, then change the analytic ORDER BY clause.
This does not assume that the seq values are always consecutive integers (0, 1, 2, ...) for each puid. You can skip, or even duplicate values. However, if the values are always consecutive integers, starting from 0, then you could simplify this. You won't need a sub-query at all; just use seq instead of r_num in the main query.
Here's the output I got from the query above:
PUID TASK DT1 DT2 DT3 DT4
id0 task0 12/21/2010 12/22/2010 12/22/2010
id1 task1 12/23/2010
id2 task2 12/22/2010 12/23/2010As posted, the query will display the first 4 dts for each puid.
If there are fewer than 4 dts for a puid, the query will still work. It will leave some columns NULL at the end.
If there are more than 4 dts for a puid, the query will still work. It will display the first 4, and ignore the others.
There's nothing special about the number 4; you could make it 3, or 5, or 35, but whatever number you choose, you have to hard-code that many columns into the query, and always get that many columns of output.
For various ways to deal with a variable number of pivoted coolumns, see the following thread:
PL/SQL
This question actually doesn't have anything to do with SQL*Plus; it's strictly a SQL question, and SQL questions are best posted on the "SQL and PL/SQL" forum:
PL/SQL
If you're not sure whether a question is more of a SQL question or a SQL*Plus question, then post it on the SQL forum. Many more people pay attention to that forum than to this one. -
Need help with Conditional Audio
Hello again! So early on into a course, after a couple intro slides, I have a sorting slide to direct users to other parts of a course (self-led learning in a way). However, I have an audio narration that plays on entry to that slide (not slide audio, it plays via advanced actions). I'm trying to figure out a way so that once they have viewed that slide once and heard that audio the first time, any time they get sent back to this sorting slide, they don't hear the audio again. How might this be done?
What I have already tried: I've tried two things, first I set up a variable called Sort_Audio. On exit of the slide previous, Sort_Audio would be assigned a value of 10. On entry to the sorting slide, an advanced action would check if Sort_Audio is equal to 10. If it is, it plays audio (and clears another variable that is unrelated, I just needed to do both things, so I combined them). I have On Exit from the sorting slide an advanced action that resets Sort_Audio to 0 so that when a user gets sent back to that sorting slide, it would theoretically not star the audio. Problem with this in the first place is for some reason it won't even play the audio the first time.
The other thing I tried was quite similar, but instead of assigning values to slides I tried cpInfoLastSlideViewed and equalled it to the name of the previous slide, as well as doing the same thing for cpInfoPreviousSlide. Neither seemed to work.
Any help would be much appreciated! Thanks!The problem is resolved. Using TTS, the audio went onto the slide. I had not removed it from the slide audio so that the Advanced Action could work. Lieve, I got it to work by being extra careful to get each step correct and complete and assuring that the slide didn't have audio attached to it. Rod, I could not get the test project to work with audio attached to a smart shape. I believe it is because I am not controlling visibility of the smart shape--a logic test for another project and another day.
Here is what I finally got to work:
I created a project to get the procedure working before I put it into the big project. Here is a description of the test project.
Slide one has a smart shape (not used as a button) and a button which goes to the next slide. There is slide audio that plays when the slide is viewed.
Slide two has a smart shape that is used as a button to go back to slide one.
First Test with simple Advanced Action (no increment of variable):
Variable used is called "v_visit"
Conditional advanced action is called "checker"
If v_visit is equal to 0
Then Play Audio "text to audio X" (created with TTS. I also tested it with recorded voice in a second test, "RecordingX.wav")Assign v_visit with 1
Else Continue
Slide one, on enter, execute Advanced Action "checker"
on exit, No Action
Second Test with different Advanced Action (variable increments):
Variable called "v_counter"
Advanced Action (conditional) called "menuAudioChecker"
First condition ("setCounter")
If 1 is equal to 1
Then Expression v_counter = v_counter + 1
(Else not used)
Second condition ("checkCounter")
If v_counter is equal to 1
Then Play Audio "Text to AudioX" or "RecordingX.wav"
(Else not used)
Slide one, On Enter Execute Advanced Action "menuAudioChecker"
On Exit, No Action
On this "Second Test" I added a smart shape to display the contents of v_counter and it counts as expected. As Lieve has indicated in other posts, if one desired a different audio to play based on the value of v_counter this would allow that to happen. In my case in the production project I could play audio associated with section 6 when they return to the menu after completing section 6.
Thank you for patiently working with me to resolve this issue (mostly due to my own oversights).
Most importantly, thank you for the excellent Advanced Action model to use for the project.
Michael -
I have the following table structure,
Table - 1_
ID | Information
1 | abcadskasasa
2 | asdasdasdasd
3 | saeqdfdvsfcsc
Table - 2_
ID | PID
1 | 12
1 | 13
2 | 14
1 | 15
1 | 16
2 | 12
Table - 3_
ID | PARID
1 | 12
2 | 14
1 | 15
Now I want to select for each ID in table 1, the count of number of PID from table 2 and count of number of PARID from table 3.
Desired output:_
ID | COUNT_PID | COUNT_PARID
1 | 4 | 2
2 | 2 | 1
3 | 0 | 0
Could anyone please help me out with this. I am trying to make use of outer joins, but as I work mostly on the front end so, not able to come up with a proper solution for the above.
Thanks in advance,
TejasHi, Tejas,
You might have been doing the outer join correctly.
There's another problem here: joining table_1 to two other tables with which it has a one-to-many relationship.
If you were joining table_1 to just one other table, you could say:
SELECT t1.id
, COUNT (t2.pid) AS count_pid
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.id = t2.id
GROUP BY t1.id
ORDER BY t1.id;You could have done the exact same thing with table_3 instead of table_2.
But you can't do the same thing with both table_2 and table_3 at the same time: that would be like cross-joining table_2 and table_3. Instead of showing id=1 having count_pid=4 and count_parid=2, you would get cout_pid=8 and count_parid=8 (since 8 = 4 * 2).
You can do a separate GROUP BY on (at least) one of the tables.
This gets the right results. In the main query, there is only one one-to-many relationship.
WITH t3_summary AS
SELECT id
, COUNT (parid) AS count_parid
FROM table_3
GROUP BY id
SELECT t1.id
, COUNT (t2.pid) AS count_pid
, MAX (t3.count_parid) AS count_parid
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.id = t2.id
LEFT OUTER JOIN t3_summary t3 ON t1.id = t3.id
GROUP BY t1.id
ORDER BY t1.id; -
Need help with Update Join Query
Hello, I am trying to update PID of #child table with PID of #parent table if "lastname & firstname are matches in both table" but my update query is giving some error. Please help and correct the update query. I am also trying to remove any
blank space from starting and ending.
drop table #parent,#child
create table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan ',' Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Update #child
set PID = p.PID
from #child C Join
#parent p ON c.LTRIM(RTRIM(lastname) = p.LTRIM(RTRIM(lastname)
AND c.LTRIM(RTRIM(firstname) = p.LTRIM(RTRIM(firstname)
/* Requested Output */
PID lastname firstname
100 Josheph Sumali
600 Mursan Terrycreate table #parent (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #parent values ('100','Josheph','Sumali')
insert into #parent values ('400','Karen','Hunsa')
insert into #parent values ('600','Mursan ',' Terry')
create table #child (PID varchar(10), lastname varchar(50), firstname varchar(50))
insert into #child values ('2','Josheph ','Sumali ')
insert into #child values ('5','Karen','Kunsi')
insert into #child values ('6','Mursan ','Terry ')
Merge #child as t
Using #parent as p ON (LTRIM(RTRIM(t.lastname)) = LTRIM(RTRIM(p.lastname))
AND LTRIM(RTRIM(t.firstname)) = LTRIM(RTRIM(p.firstname)) )
When Matched Then
Update
set PID = p.PID;
update #child
Set lastname=LTRIM(RTRIM(lastname)), firstname= LTRIM(RTRIM(firstname));
update #parent
Set lastname=LTRIM(RTRIM(lastname)), firstname = LTRIM(RTRIM(firstname));
select * from #child
select * from #parent
drop table #parent,#child -
Hi,
I am looking for the following result:
PROVIDER_ID SPECIALTY BUCKET CODE RATING FEE
1 FP EM 100 9 20
1 FP SP 300 15 0
1 INFUS EM 100 3 20
1 INFUS EM 200 6 15The base tables are provided below in the with clause. What I am trying to do is, where "code" matches show the fee from t1 else show 0.
I tried a few variotions but just can't seem to get it right.
with t1 as
select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 200 as code, 15 as fee, 30 as default_multiplier from dual
, t2 as
select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
SELECT t1.provider_id
, t2.specialty
, t2.bucket
, t2.code
, t2.rating
, t1.fee
FROM t1, t2
ORDER BY 1, 2, 3Any help will be appreciated.
Thanks.Could I possibly add one more twist to this.
The current result:
PROVIDER_ID SPECI BU CODE RATING FEE
1 FP EM 100 9 20
1 FP SP 300 15 0
1 INFUS EM 100 3 20
1 INFUS EM 200 6 75
2 FP EM 100 9 40
2 FP SP 300 15 0
2 INFUS EM 100 3 40
2 INFUS EM 200 6 0
3 FP EM 100 9 0
3 FP SP 300 15 0
3 INFUS EM 100 3 0
3 INFUS EM 200 6 60The current code:
with t1 as
select 1 as provider_id, 100 as code, 20 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 200 as code, 75 as fee, 10 as default_multiplier from dual union all
select 1 as provider_id, 500 as code, 75 as fee, 10 as default_multiplier from dual union all
select 2 as provider_id, 100 as code, 40 as fee, 20 as default_multiplier from dual union all
select 3 as provider_id, 200 as code, 60 as fee, 30 as default_multiplier from dual
, t2 as
select 100 as code, 'INFUS' AS specialty, 'EM' AS bucket, 3 as rating from dual union all
select 200 as code, 'INFUS' AS specialty, 'EM' AS bucket, 6 as rating from dual union all
select 100 as code, 'FP' AS specialty, 'EM' AS bucket, 9 as rating from dual union all
select 300 as code, 'FP' AS specialty, 'SP' AS bucket, 15 as rating from dual
, t3 as
SELECT t1.provider_id
, t2.specialty
, t2.bucket
, t2.code
, t2.rating
, CASE t1.code
WHEN t2.code
THEN t1.fee
ELSE 0
END AS fee
, RANK () OVER (PARTITION BY t1.provider_id, t2.specialty, t2.bucket, t2.code ORDER BY CASE t1.code
WHEN t2.code
THEN t1.fee
ELSE 0
END DESC) AS the_rank
FROM t1
, t2
SELECT DISTINCT provider_id
, specialty
, bucket
, code
, rating
, fee
FROM t3
WHERE the_rank = 1
ORDER BY 1
, 2
, 3
, 4I added the code 500 to t1. Howevere, I don't want this code to show up in the result becuase it is does not exist in t2.
I also added code 200 to t1, to see if that shows properly.
I had to do the rank, becuase I need the proper count of rows.
In reality the row counts are:
t1: 20 Million
t2: 10 Thousand.
So, I would like to avoid doing DISTINCT and ANALYTIC functions on this large set, so any better way of achiving the same result, with a better statement.
Thanks. -
Need help with inner join and distinct rows
Hey Guys,
i have
1) BaseEnv Table
2) Link Table
3) BaseData Table
Link table has three columns Id,BaseEnvId,BaseDataId
the BaseEnvID is unique in the table where as BaseDataId can be repeated i.e multile rows of BaseEnv Table can point to same BaseData table row
Now i want to do BaseEnvTable inner join Link Table inner join BaseData Table and select 5 columsn ; Name,SyncName,Version,PPO,DOM from the BaseData table.. the problem is that after i do the inner join I get duplciate records..
i want to eliminate the duplicate records , can any one help me herePlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums. Now we have to guess and type, guess and type, etc. because of your bad manners.
CREATE TABLE Base_Env
(base_env_id CHAR(10) NOT NULL PRIMARY KEY,
Think about the name Base_Data; do you have lots of tables without data? Silly, unh?
CREATE TABLE Base_Data
(base_data_id CHAR(10) NOT NULL PRIMARY KEY,
Your Links table is wrong in concept and implementation. The term “link” refers to a pointer chain structure used in network databases and makes no sense in RDBMS. There is no generic, magic, universal “id” in RDBMS! People that do this are called “id-iots”
in SQL slang.
We can model a particular relationship in a table by referencing the keys in other tables. But we need to know if the relationship is 1:1, 1:m, or n:m. This is the membership of the relationship. Your narrative implies this:
CREATE TABLE Links
(base_env_id CHAR(10) NOT NULL UNIQUE
REFERENCES Base_Env (base_env_id),
base_data_id CHAR(10) NOT NULL
REFERENCES Base_Data (base_data_id));
>> The base_env_id is unique in the table where as base_data_id can be repeated I.e multiple rows of Base_Env Table can point [sic] to same Base_Data table row. <<
Again, RDBMS has no pointers! We have referenced an referencing tables. This is a fundamental concept.
That narrative you posted has no ON clauses! And the narrative is also wrong. There is no generic “name”, etc. What tables were used in your non-query? Replace the ?? in this skeleton:
SELECT ??.something_name, ??.sync_name, ??.something_version,
??.ppo, ??.dom
FROM Base_Env AS E, Links AS L, Base_Data AS D
WHERE ?????????;
>> I want to eliminate the duplicate records [sic], can any one help me here?<<
Where is the sample data? Where is the results? Please read a book on RDBMS so you can post correct SQL and try again.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Need help with outer join filter.
Need a little help filtering a resultset and I cant seem to find a proper way to do this.
/*table*/
create table invoice( farinvc_invh_code varchar2(100),
farinvc_item varchar2(100),
farinvc_po varchar2(100)
create table po(
supplier_number varchar2(60),
supplier_invoice_no varchar2(60),
po_number varchar2(60),
run_date varchar2(60),
PO_LINE_NUMBER varchar2(60) );
/*data*/
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '1', 'P0142245');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '3', 'P0142245');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '1', 'P0142246');
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '2', 'P0142246');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529132260', 'P0142245', '21-NOV-12', '1');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '3');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '2');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145678', 'P0142246', '22-NOV-12', '1');
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145679', 'P0142246', '22-NOV-12', '2');query im executing.
SELECT farinvc_invh_code,
supplier_number,
supplier_invoice_no,
farinvc_item,
farinvc_po ,
po_number,
run_date,
PO_LINE_NUMBER
FROM INVOICE, PO
WHERE PO_NUMBER = FARINVC_PO(+)
AND FARINVC_ITEM(+) = PO_LINE_NUMBER
result
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554165" "914100122" "145678" "1" "P0142246" "P0142246" "22-NOV-12" "1"
"I0554165" "914100122" "145679" "2" "P0142246" "P0142246" "22-NOV-12" "2"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2" this is a much larger table and I took an excerpt in order to keep things clear and understanding. I would like to filter this result set to only show the lines that have have the po numbers are the same and line are the same but there is an extra item. in other words like such.
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2"fair enough frank lets add some extra data to the tables.
for example.
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554167', '1', 'P0142447')
INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554167', '2', 'P0142447')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV1', 'P0142247', '25-NOV-12', '1')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV2', 'P0142247', '25-NOV-12', '2')
INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100123', 'INV3', 'P0142247', '25-NOV-12', '3')if we run the query above we get
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2"
"" "914100123" "INV2" "" "" "P0142247" "25-NOV-12" "2"
"" "914100123" "INV1" "" "" "P0142247" "25-NOV-12" "1"
"" "914100123" "INV3" "" "" "P0142247" "25-NOV-12" "3" where really what im trying to target is pos and lines that have a match in both tables and there is additional items from the po table that do not have have a match from the invoice table. Furthermore i would only like to see the items that are repeating invoice numbers, in other words my result here should still only be, because "SUPPLIER_INVOICE_NO" is repeating and it does find a match for the po in the invoice table but yet there is an item with the same invoice and po in the po table that does not have a match in the invoice table.
"FARINVC_INVH_CODE" "SUPPLIER_NUMBER" "SUPPLIER_INVOICE_NO" "FARINVC_ITEM" "FARINVC_PO" "PO_NUMBER" "RUN_DATE" "PO_LINE_NUMBER"
"I0554164" "914100121" "529132260" "1" "P0142245" "P0142245" "21-NOV-12" "1"
"I0554164" "914100121" "529137831" "3" "P0142245" "P0142245" "21-NOV-12" "3"
"" "914100121" "529137831" "" "" "P0142245" "21-NOV-12" "2" hope that makes sense, and thanks for working with me on this.
Edited by: mlov83 on Dec 12, 2012 5:53 AM -
Need help with conditional display
I found a older thread (704012) that explains how to conditionally display a link using style. Which is what I want to do. The part I'm having trouble with is that part of the instructions say to put
class="row_has_values_#HAS_VALUES#"
in the Link Attributes which I have done. I also have a column in the report called HAS_VALUES and I'm properly populating it in my select. I know because I left it displayed while I'm testing. I can run the report and I have Y's and N's showing up in that column but the conditional part does not work. When I look at the page source (below) I see that in the html the #HAS_VALUES# has not been replaced by the actual Y or N. I think that's where I'm having trouble? I think it might work if there was a Y or N in that but I don't understand why that didn't happen.
<tr class="odd"><td headers="LINK"><a href="f?p=125:3:12628966088981::NO::P3_PROGRAM_KEY:2" class="row_has_values_#HAS_VALUES#"><img src="/i/e2.gif" border="0"></a></td><td align="left"LawrenceJ wrote:
I've continued to snoop around and see lots of posts on conditional display in reports. Common desire I guess. I saw some stuff that I interpreted to suggest that what I'm trying to do may not work in interactive reports, only in classic reports. Does that sound familiar?
That would certainly apply to anything template-related. However there have been enhancements to IRs (like adding HTML Expressions) that might supersede information in older posts. If you provide a link to the sources you've found and the APEX version you're using someone will be able to confirm whether that information is relevant.
Many other posts on doing things conditionally in reports seemed to lean towards "selecting" the html based on data values and then just letting that get put in the report column. That looks promising but sure is a bit ugly. I've very little apex experience but lots of mod pl/sql and that sort of solution was pretty common when I did stuff with it. I saw a little suggesting using templates as a solution but I'll confess to not understanding it at all.
As I pointed out in the original thread, if the conditional display of the link is in any way security related then CSS or JS/jQuery methods are not acceptable. If the user is not supposed to be able to click the link or see any data contained in it, then you have to use a method that ensures that the link element never reaches the browser.
Hard-coding the link HTML in the report query is the common approach, but is as you say a bit ugly. Using a custom report template (my favourite APEX subject) enables clean separation of the report query, conditional logic and HTML structure. If you know HTML then you're advised to get familiar with using templates in APEX. You'll find this a major advance on the PL/SQL web toolkit. What is it that you're not understanding about using templates? -
Hi,
I have data like this
create table dd1
(col1 varchar2(20))
insert into dd1
values('P1')
insert into dd1
values('P2')
insert into dd1
values('P3')
insert into dd1
values('P4')
Col1
P1
P2
P3
P4
I need result like this:
Col1 col2
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
Is there any way to this in Sql?
Thanksdd_ram wrote:
Hi,
I have data like this
create table dd1
(col1 varchar2(20))
insert into dd1
values('P1')
insert into dd1
values('P2')
insert into dd1
values('P3')
insert into dd1
values('P4')
Col1
P1
P2
P3
P4
I need result like this:
Col1 col2
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
Is there any way to this in Sql?
ThanksHi,
I have something for you who is more simple like previous example (probably the simplest answer from this forums):
HR: XE > select * from dd1 a, dd1 b
2 where a.col1<b.col1;
COL1 COL1
P1 P2
P1 P3
P1 P4
P2 P3
P2 P4
P3 P4
6 rows selected.
HR: XE > Regards,
Ion
Edited by: user111444777 on Sep 26, 2009 7:12 PM
Edited by: user111444777 on Sep 26, 2009 7:18 PM -
Need help with query join...
Hi all, i have the below query, it has too many where clauses and want to seperate it into joins perhaps, how can i do this please suggest some syntax.
Thanks,
Alex
CODE:
“select surname,award.name as award_name,module.name as module_name,attemptno as attempt_no from student,award,studentaward,module,studentenrollment where studentenrollment.attemptno = 2 and studentenrollment.studentid = student.studentid and studentenrollment.moduleid = module.moduleid and studentaward.studentid = student.studentid and studentaward.awardid = award.awardid”select surname,award.name as award_name,module.name as module_name,attemptno as attempt_no
from student
join studentaward on studentaward.studentid = student.studentid
join award on studentaward.awardid = award.awardid
join studentenrollment on studentenrollment.studentid = student.studentid
join module on studentenrollment.moduleid = module.moduleid
where studentenrollment.attemptno = 2 -
I need help with the conditional build tag option RoboHelp 10
I need help with the conditional build tag option. I want to apply CBT to content in a topic. I looked at the Help topics and believed that I applied the feature correctly. Howver, it is not working as desired. In the 2nd sentence below I want the text highlighted in blue to only appear for the printed output and the text printed in purple to only appear for the .htm /online output. Please help.
There are common tasks used to manage the folders in the Navigator and the folders
in the BBS Folders Viewer Grid. For more information on these common tasks see Help
and Support in Success Enterprise. click the links below.Hi there
Using tagging is a two part process.
Part One
You create and apply the tags to the information you wish to control.
Part Two
You create a Build Expression that is used when you generate your output. The Build Expression typically reads something like: NOT Tag1 (or whatever your tag name is)
Then when you generate and use the Build Expression, the information tagged is not included in the build.
Cheers... Rick
Maybe you are looking for
-
Query user ACL results in exception, PortalException, Invalid pointer AxisFault
Hi, I am developing a remote portlet application in Java for plumtree portal v 5.0.3, using EDK library v 5.0. The requirement is to determine if the user accessing the application has "Admin" privileges for the portlet or not.. For this, I query Use
-
Help required in UCM BPEL integration
Myself Rahul Dutta from itech Technology Sales Consulting team India. We are working on a customer scenario where we need to call a BPEL process when we are checking in a document in the UCM. The document checkin and the BPEL process triggering is to
-
Can I make a file uneditable??
Hi there!! In fact my java app creates a config file on the client's machine and I want that after the file's creation the client or any user can't edit the file,,,is it possible some way?? Thanks...
-
Sap R/3 basis 4.7 edition pdf document
could u send me basis 4.7 enterprises pdf document for my mail [email protected]
-
Getting an english installation
I need a link to an English version download of iTunes. I am unable to install an English version of iTunes. I live in the United States buy my computer and Windows Vista OS are from Sweden. During the installition process (just purchased an iPod)the