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 AM

Hi,
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.

Similar Messages

  • Need help with query that uses 'SELECT INTO'

    Hi guys,
    I am trying to duplicate the values of a table to another by using the script below:
    ACCEPT TBSNAME           CHAR PROMPT 'Tablespace name>'
    PROMPT Connect As System
    Connect system
    CREATE TABLE FREESPACE
         TABLESPACE_NAME          VARCHAR(20)          NOT NULL,
              CONSTRAINT FREESPACE_PKEY PRIMARY KEY(TABLESPACE_NAME),
         TOTAL_FREE_STORAGE     NUMBER(10)          NOT NULL
    ) TABLESPACE USERS;
    SELECT     TABLESPACE_NAME,
              SUM(BLOCKS) TOTBLOCKS
    INTO      FREESPACE
    FROM      SYS.DBA_FREE_SPACE
    WHERE      TABLESPACE_NAME = UPPER('&TBSNAME')
    GROUP BY TABLESPACE_NAME;
    However, when I execute this script, I was prompted with the 'missing keyword' error which happens at the third line of the select statment. Any idea what's missing here?
    Thanks in advance.

    If you mean "fill up the table FREESPACE" by using "select...into..." then it is totally wrong. You cannot do it in plsql also.
    Use this:
    insert into freespace(tablespace_name,total_free_storage)
    SELECT TABLESPACE_NAME,SUM(BLOCKS) TOTBLOCKS
    FROM SYS.DBA_FREE_SPACE
    WHERE TABLESPACE_NAME = UPPER('&TBSNAME')
    GROUP BY TABLESPACE_NAME;
    Message was edited by:
    Yas

  • 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 to edit sections of several songs into a single track on a CD for a dressage test. Any ideas what software I need to get to achieve this?

    I need to edit sections of several songs into a single track on a CD for a dressage test. Any ideas what software I need to get to achieve this?

    Audacity 1.2.5 is an audio editor in which you can assemble and edit your recordings: it's free. I don't think it would burn CDs - you would have to reimport the finished recording into iTunes and burn from there.
    Amadeus Lite is $24.99 in the Mac App Store and is in my opinion a better product: it can burn your audio CD directly.
    If this is a one-off job you might as well use Audacity: if you are planning on doing this sort of thing at all often you would probably be better off either with Amadeus Lite or Amadeus Pro ($59.99) though this latter may well be over-specified for you as it includes multi-track editing.

  • 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        Terry

    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 ')
    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

  • 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

    Hi,
    Good day everyone! I need help writing a query. I have this table with the following data in them...
    ACCT_CODE  FSYR      YTD_AMT
    A123            11          100  
    A456            11          200
    A123            10          50
    A456            10          100I want the output to look like this:
    ACCT_CODE     CURRENT_YEAR(11)       PRIOR_YEAR(10)
    A123               100                              50
    A456               200                              100The user will input the fiscal year and based on that input, I want to get the prior year value as well.
    Thank you for all your help!!
    Edited by: user5737516 on Jun 29, 2011 6:48 AM
    Edited by: user5737516 on Jun 29, 2011 6:50 AM

    user5737516 wrote:
    Hi,
    Good day everyone! I need help writing a query. I have this table with the following data in them...
    ACCT_CODE FSYR YTD_AMT
    A123 11 100
    A456 11 200
    A123 10 50
    A456 10 100
    I want the output to look like this:
    ACCT_CODE CURRENT_YEAR PRIOR_YEAR
    A123 100 50
    A456 200 100
    The user will input the fiscal year and based on that input, I want to get the prior year value as well.
    Thank you for all your help!!what is prior year?

  • 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 here

    Please 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 a JOIN query

    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 query that can look data back please help.

    hi guys i have a table like such
    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)
      )and i have a data like such
    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 ('240055','240055','7600','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','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','7600','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','7470','4730','02','10','2','200');I bascially need to get the total of the budget column. however its not as simple as it sound(well atleast not for me.) the totals carry over to the new period. youll noticed the you have a period column. basically what im saying is that
    fgl_grant_year 10 period 1 = for account 7600 its $100 and $100 for period 2 you see 100 dollars again this is not to be added this is the carried over balance. which remains $100.
    so im trying to write a query that basically does the following.
    im given a period for the sake of this example lets say period 1 i get nothing else. I have to find the greates grant year grab the amount for period 14(which is the total from the previous year) and add it to the amount of the current period. in this case period 1 grnt_year 11
    so the expected outcome should be $700
    240055     240055     7240     4730     02     10     14     200
    240055     240055     7600     4730     02     10     14     100
    240055     240055     7600     4730     02     11     1     400keep in mind that im not given a year just a period.
    any help that you guys can offer would be immensely appreciated. I have been trying to get this to work for over 3 days now.
    finally broke down and put together this post
    Edited by: mlov83 on Sep 14, 2011 8:48 PM

    Frank
    wondering if you can help me modify this sql statement that you provided me with .
    table values have been modified a bit.
    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 take one more thing into consideration. if the greatest year has a value on period 00 i need to ignore the period 14 and the current period total would be
    the 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.
    Miguel

  • Query off of Oracle using WinSql - Need help with query

    I am trying to query off of Oracle using program WinSql.
    I have a table(tticpr200110) that has the following sample data:
    ITEM     CODE     T$AMNT
    23500076 ACL     .0049
    23500076 APM     0
    23500076 APO     .0093
    23500076 EXP     .0001
    23500076 RES     .0072
    and what I want it to look like is:
    ITEM     ACL     APM     APO     EXP     RES
    23500076     0.0049     0     0.0093     0.0001     0.0072
    (actually I need the last 2 columns added together to be MATL-but can deal with that down the road).
    Seems simple enough, but I don't know to put into the columns.
    Any help would be GREATLY appreciated as soon as possible would be even better.

    My table - tticpr200110 when it runs I get the following sample data for part number 23500076:
    The first coloumn ITEM is the part number.
    The second column CODE is 1 of 5 different cost codes
    The third column is the cost for that code for that part.
    ITEM CODE AMNT
    23500076 ACL 0.0049
    23500076 APM 0.0000
    23500076 APO 0.0093
    23500076 EXP 0.0001
    23500076 RES 0.0072
    I want to make a query that makes the data look like this:
    ITEM ACL APM APO EXP RES
    23500076 0.0049 0.0000 0.0093 0.0001 0.0072
    (similar to a pivot table in excel or acess)
    I hope this helps better.
    Thanks!

  • 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 Query to determine Credit Memos and Invoices

    Hi All
    Thanks for all the help here.
    I need a query to determine any credits or invoices issued within a given period.
    OINV and ORIN with UNION ALL?
    Please advise any help.
    Thank you!

    Hi Daniel,
    Please check below Query.
    SELECT T0.[DocNum] as 'Invice No', T0.[DocDate] as 'Invoice Date', T0.[CardName] as 'Invoiced Customer', T3.[DocNum] as 'Credit Memo No', T3.[DocDate] as 'Credit Mamo Date', T3.[CardName] as 'Credit Memo Customer' FROM OINV T0  LEFT JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] LEFT JOIN RIN1 T2 ON T2.[BaseEntry] = T1.[DocEntry] AND T2.[BaseLine] =  T1.[LineNum] LEFT JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry] WHERE T0.[DocDate]  >=[%3]  AND   T0.[DocDate] <=[%4]
    Hope this helps
    Regards::::
    Atul Chakraborty

  • Need help with outer joins

    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,
    Tejas

    Hi, 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 query for converting columns to rows

    Hello,
    I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
    I have a table which is having multiple columns as follows:
    Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
    Col1 Col2
    X      0
    Y     0
    Z      2
    A     0
    B     0
    C     1
    D     3
    E     0
    F     0
    G     0
    H     0
    I      0Please anybody help me in writing the query for this..

    Is this what you are expecting:
    SQL> WITH T AS
      2  (
      3  SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
      4  )
      5  SELECT  'X' col1, X col2 FROM T
      6  UNION ALL
      7  SELECT  'Y' col1, Y col2 FROM T
      8  UNION ALL
      9  SELECT  'Z' col1, Z col2 FROM T
    10  UNION ALL
    11  SELECT  'A' col1, A col2 FROM T
    12  UNION ALL
    13  SELECT  'B' col1, B col2 FROM T
    14  UNION ALL
    15  SELECT  'C' col1, C col2 FROM T
    16  UNION ALL
    17  SELECT  'D' col1, D col2 FROM T
    18  UNION ALL
    19  SELECT  'E' col1, E col2 FROM T
    20  UNION ALL
    21  SELECT  'F' col1, F col2 FROM T
    22  UNION ALL
    23  SELECT  'G' col1, G col2 FROM T
    24  UNION ALL
    25  SELECT  'H' col1, H col2 FROM T
    26  UNION ALL
    27  SELECT  'I' col1, I col2 FROM T
    28  /
    C       COL2                                                                   
    X          0                                                                   
    Y          0                                                                   
    Z          2                                                                   
    A          0                                                                   
    B          0                                                                   
    C          1                                                                   
    D          3                                                                   
    E          0                                                                   
    F          0                                                                   
    G          0                                                                   
    H          0                                                                   
    C       COL2                                                                   
    I          0                                                                   
    12 rows selected.

Maybe you are looking for

  • Can you create a "constructor" for movie clips?

    What I'm doing: var this_array[counter] = new monster; //monster is a movie clip init_monster(); //set's things like .name, .x, .y, etc. What I want to do: var this_array[counter] = new monster(); When I add a "monster"  I then call a function to "in

  • Adobe Air Installation problems

    Hi, I am trying to install Adobe AIR, and I'm running into a problem. The installer quits with the error "An error occurred while installing Adobe AIR. Installation may not be allowed by your administrator. Please contact your administrator". I did a

  • Appropriate use of "return" in a method

    Migrating to Java quite some time back now I still haven't found the appropriate use of the 'return'-statement in a method. In procedual languages there has always been an unwritten rule saying that a function should not have more than one 'return'-s

  • Usage of DBMS_OUTPUT.PUT_LINE degrade performance?

    Does multiple usage of DBMS_OUTPUT.PUT_LINE in a procdure degrade performance OR crash the procedure?

  • How to implement a back functionally like the back of the browser

    Hi, I have a commandImageLink("go back") in a template jspx. How to implement the back functionally like the back of the browser with a managed bean method??. For know I m not using task flows.