Help in a query please

POs PRs
po1 r1
po1 r1
po1 r1
po1 r1
po2 r1
po2 r3
po2 r4
po2 r4
From the above two columns (POs, PRs) table I want the result where for same 'poX' print only if it has different 'rX'
POs PRs
po2 r1
po2 r3
po2 r4
po2 r4
So it should not print those POx if it has same PRx.

Hi,
That sounds like a job for the analytic COUNT function.
Here's one way:
WITH     got_prs_cnt     AS
     SELECT  pos, prs
     ,     COUNT (DISTINCT prs)
              OVER (PARTITION BY pos)     AS prs_cnt  -- See note below
     FROM    table_x
SELECT     pos, prs
FROM     got_prs_cnt
WHERE     prs_cnt     > 1
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data. Include examples of special cases that you need to handle (e.g., 3 rows with the same pos, and 2 disinct prs values).
Point out where the query above is producing the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Edited by: Frank Kulash on May 2, 2013 6:40 AM
Changed "PARTITIION" (with 2 consecutive I's) to "PARTITION", after OP

Similar Messages

  • SQL I need help with this query Please help

    List the names of all the products whose weight unit measure is “Gram”.  Order the list by product name.  Do not use JOINS, but use the IN clause with a sub-query.
    select Name
    from UnitMeasure
    where Name= 'Gram'
    order by Name
    I did this, but it seem that the requirement is different.

    As a guess:
    Select Name from Product
    where UnitMeasure in (Select Name from unitmeasure where name = 'Gram')
    Andy Tauber
    Data Architect
    The Vancouver Clinic
    Website | LinkedIn
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click
    "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

  • Can any one help with this query please

    I have a table something as below
    Things_t
    Things Characteristic Char Value
    Item 1 Colour Red
    Item 1 Packaging
    Item 2 Shape Square
    Item 2 Brand Spunk
    Now i want to reterive an item with none of its char values as Null. Using the query “ select distinct things from things_t where char value is Null ” will fetch the item 1 also together with item 2. i want to fetch a record from thing for which none of the char values are Null such as Item 2. Can you please help me with this query.

    Try this:
    WITH t AS
    (SELECT 1 item_id, 17436 chr_id, 14225034 chr_val_id FROM dual UNION
    SELECT 1 item_id, 39 chr_id, 14276173 chr_val_id FROM dual UNION
    SELECT 1 item_id, 17774 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 1 item_id, 265 chr_id, 20502978 chr_val_id FROM dual UNION
    SELECT 1 item_id, 16978 chr_id, 797233 chr_val_id FROM dual UNION
    SELECT 1 item_id, 13092 chr_id, 5666917 chr_val_id FROM dual UNION
    SELECT 1 item_id, 15228 chr_id, 1209758 chr_val_id FROM dual UNION
    SELECT 2 item_id, 112 chr_id,  12705342 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6945 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 70 chr_id, 12597376 chr_val_id FROM dual UNION
    SELECT 2 item_id, 16832 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 7886 chr_id, 9588619 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6986 chr_id, 2659351 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9531 chr_id, 8910943 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9798 chr_id, 8717531 chr_val_id FROM dual UNION
    SELECT 3 item_id, 17446 chr_id, 12266441 chr_val_id FROM dual UNION
    SELECT 3 item_id, 4830 chr_id, 13683090 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9518 chr_id, 834772 chr_val_id FROM dual UNION
    SELECT 3 item_id, 11031 chr_id, 20233753 chr_val_id FROM dual UNION
    SELECT 3 item_id, 12564 chr_id, 2282478 chr_val_id FROM dual)
    SELECT DISTINCT item_id
    FROM   t
    MINUS
    SELECT DISTINCT item_id
    FROM   t
    WHERE  chr_val_id IS NULLOr this:
    SELECT item_id
    FROM  (SELECT   item_id,
                    MIN(NVL(chr_val_id, -1)) min_chr_val_id
           FROM     t
           GROUP BY item_id)
    WHERE  min_chr_val_id != -1Edited by: lee200 on Oct 15, 2012 9:22 AM

  • Help with one query ( Please reply)

    Oracle version : 11.2.0.2.0
    I have a table with the following set of rows
    with Table_dm AS
    ( select 1 month_no ,'wk1' week, 120 DM_AMT,300 GRoss_Amt,4 Week_no, 12 Gross_qty,1000 dm_adj_total from dual
      UNion all
      select 1 month_no ,'wk2' week,120 DM_AMT,300 GRoss_Amt,4 Week_no, 12 Gross_qty,1000 dm_adj_total from dual
      union all
      select 1 month_no ,'wk3' week,120 DM_AMT,300 GRoss_Amt,4 Week_no, 12 Gross_qty,1000 dm_adj_total from dual
      union all
      select 1 month_no ,'wk4' week,120 DM_AMT,300 GRoss_Amt,4 Week_no, 12 Gross_qty,1000 dm_adj_total from dual
      union all
      select 2 month_no ,'wk5' week,200 DM_AMT,400 GRoss_Amt,5 Week_no, 20 Gross_qty,1000 dm_adj_total from dual
      UNion all
      select 2 month_no ,'wk6' week,200 DM_AMT,400 GRoss_Amt,5 Week_no, 20 Gross_qty,1000 dm_adj_total from dual
      union all
      select 2 month_no ,'wk7' week,200 DM_AMT,400 GRoss_Amt,5 Week_no, 20 Gross_qty,1000 dm_adj_total from dual
      UNion all
      select 2 month_no ,'wk8' week,200 DM_AMT,400 GRoss_Amt,5 Week_no, 20 Gross_qty,1000 dm_adj_total from dual
      UNion all
      select 2 month_no ,'wk9' week,200 DM_AMT,400 GRoss_Amt,5 Week_no, 20 Gross_qty,1000 dm_adj_total from dual
    So the data in Grid  will be like this
    month_no
    week
    DM_AMT
    GRoss_Amt
    Week_no
    Gross_qty
    dm_adj_total
    DM_ADJ_Final
    1
    wk1
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    =1
    wk2
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    1
    wk3
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    1
    wk4
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    2
    wk5
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk6
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk7
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk8
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk9
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    I need to calculate DM_ADJ_Final  where calculation for DM_ADJ_Final  will be like
                        = dm_adj_total * gross_amt/ (gross_amt (where month_no =1 ) + gross_amt (where month_no =2 )) ( Please refer  DM_ADJ_Final column above  for calculation with value )

    You apparently have (or intend) an association between rows where month_no = 1 and month_no = 2.
    But what is that relationship?  Is the month 2 row always 4 weeks later than the month 1 row?
    That relationship needs to be spelled out exactly.
    month_no
    week
    DM_AMT
    GRoss_Amt
    Week_no
    Gross_qty
    dm_adj_total
    DM_ADJ_Final
    1
    wk1
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    =1
    wk2
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    1
    wk3
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    1
    wk4
    120
    300
    4
    12
    1000
    1000 * 300 /(300+400) = 428.57
    2
    wk5
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk6
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk7
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk8
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    2
    wk9
    200
    400
    5
    20
    1000
    1000 * 400 /(300+400) = 571.42
    I need to calculate DM_ADJ_Final  where calculation for DM_ADJ_Final  will be like
                        = dm_adj_total * gross_amt/ (gross_amt (where month_no =1 ) + gross_amt (where month_no =2 )) ( Please refer  DM_ADJ_Final column above  for calculation with value )

  • Help with this query please

    Hi there, 
    These are the sample values
    declare @table table
    Name varchar(50),
    flag int
    insert into @table
    values('Matt', 0),
    ('George', 0),
    ('George', 1),
    ('Lucas', 0),
    ('Jerome', 0),
    ('Jerome', 1)
    I want to select out where George and Jerome where flag = 0 but leave the records from the same names where flag = 1. All others that only have flag = 0, should stay. So only the names that repeat and have both flag, flag = 0 zero should be selected out.
    Thanks for your help.

    So desired output is ????
    George 0
    Jerome 0
    declare @table table
    Name varchar(50),
    flag int
    insert into @table
    values('Matt', 0),
    ('George', 0),
    ('George', 1),
    ('Lucas', 0),
    ('Jerome', 0),
    ('Jerome', 1)
    select name,min(flag) flag,count(*) from @table
    group by name
    having count(*)>1
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Help in Select query please

    Hi, How I can split the column (Root_Desc) into five. Need to split column after the hyphen ('_').
    Select * from travel
    Root_Desc
    1100_AUS_04_RTOT_GT
    1102_USA_3_RTOT_TT
    Desired Output
    Rout_No Dest_Country Desc_Code Rout_Desc Rout_Line
    1100     AUS   04    RTOT    GT
    1102     USA    3     RTOT   TT
    Thanks.

    You can use split function, say,
    declare @travel table (Root_Desc varchar(max));
    insert into @travel (Root_Desc) values ('1100_AUS_04_RTOT_GT'),('1102_USA_3_RTOT_TT');
    ;with cte as (select t.Root_Desc, F.Value, F.Id from @travel t
    CROSS APPLY dbo.fnSplit(t.Root_Desc, '_') F)
    select Root_Desc, [1] as Rout_No, [2] as Dest_Country,
    [3] as Desc_Code,
    [4] as Rout_Desc,
    [5] as Rout_Line
    from cte PIVOT (min(value) for ID IN ([1],[2],[3],[4],[5])) pvt
    where fnSplit is table-valued function:
    USE [AllTests]
    GO
    /****** Object: UserDefinedFunction [dbo].[fnSplit] Script Date: 1/5/2015 11:47:32 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- Test query
    CREATE FUNCTION [dbo].[fnSplit]
    (@list VARCHAR(8000),
    @delim CHAR(1) = ','
    ) RETURNS TABLE AS
    RETURN
    WITH csvtbl(START, stop) AS (
    SELECT START = 1,
    stop = CHARINDEX(@delim COLLATE Slovenian_BIN2, @list + @delim)
    UNION ALL
    SELECT START = stop + 1,
    stop = CHARINDEX(@delim COLLATE Slovenian_BIN2,
    @list + @delim, stop + 1)
    FROM csvtbl
    WHERE stop > 0
    SELECT row_number() over (order by Start) as ID, LTRIM(RTRIM(SUBSTRING(@list, START,
    CASE WHEN stop > 0 THEN stop - START ELSE 0 END)))
    AS VALUE
    FROM csvtbl
    WHERE stop > 0
    GO
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • How to improve the performance of the attached query, Please help

    Hi,
    How to improve performance of the below query, Please help. also attached explain plan -
    SELECT Camp.Id,
    rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount,
    (SUM(rCam.Impressions) * 0.001 + SUM(rCam.Clickthrus)) AS GR,
    rCam.AccountKey as AccountKey
    FROM Campaign Camp, rCamSit rCam, CamBilling, Site xSite
    WHERE Camp.AccountKey = rCam.AccountKey
    AND Camp.AvCampaignKey = rCam.AvCampaignKey
    AND Camp.AccountKey = CamBilling.AccountKey
    AND Camp.CampaignKey = CamBilling.CampaignKey
    AND rCam.AccountKey = xSite.AccountKey
    AND rCam.AvSiteKey = xSite.AvSiteKey
    AND rCam.RmWhen BETWEEN to_date('01-01-2009', 'DD-MM-YYYY') and
    to_date('01-01-2011', 'DD-MM-YYYY')
    GROUP By rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount
    Explain Plan :-
    Description                    Object_owner          Object_name     Cost     Cardinality     Bytes     
    SELECT STATEMENT, GOAL = ALL_ROWS                              14     1     13
    SORT AGGREGATE                                                  1     13
    VIEW                         GEMINI_REPORTING               14     1     13
    HASH GROUP BY                                        14     1     103
    NESTED LOOPS                                        13     1     103
    HASH JOIN                                             12     1     85
    TABLE ACCESS BY INDEX ROWID     GEMINI_REPORTING     RCAMSIT          2     4     100
    NESTED LOOPS                                        9     5     325
    HASH JOIN                                        7     1     40
    SORT UNIQUE                                        2     1     18
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          SITE          2     1     18
    INDEX RANGE SCAN          GEMINI_PRIMARY          SITE_I0          1     1     
    TABLE ACCESS FULL          GEMINI_PRIMARY          SITE          3     27     594
    INDEX RANGE SCAN          GEMINI_REPORTING     RCAMSIT_I     1     1     5     
    TABLE ACCESS FULL     GEMINI_PRIMARY     CAMPAIGN                    3     127     2540
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          CAMBILLING     1     1     18
    INDEX UNIQUE SCAN     GEMINI_PRIMARY     CAMBILLING_U1                    0     1

    Hello,
    This has really nothing to do with the Oracle Forms product.
    Please, send the SQL or/and PL/SQL questions in the corresponding forums.
    Francois

  • Please, need help with a query

    Hi !
    Please need help with this query:
    Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
    Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
    select distinct a.id,
    create_date,
    a.loanid,
    a.rate,
    a.pays,
    a.gracetime,
    a.emailtosend,
    d.first_name,
    d.last_name,
    a.user_id
    from CLAL_LOANCALC_DET a,
    loan_Calculator b,
    bv_user_profile c,
    bv_mr_user_profile d
    where b.loanid = a.loanid
    and c.NET_USER_NO = a.resp_id
    and d.user_id = c.user_id
    and a.is_partner is null
    and a.create_date between
    TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
    TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    order by a.create_date

    Take a look on the syntax :
    max(...) keep (dense_rank last order by ...)
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#i1000901
    Nicolas.

  • Please need help with this query

    Hi !
    Please need help with this query:
    Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
    Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
    select distinct a.id,
    create_date,
    a.loanid,
    a.rate,
    a.pays,
    a.gracetime,
    a.emailtosend,
    d.first_name,
    d.last_name,
    a.user_id
    from CLAL_LOANCALC_DET a,
    loan_Calculator b,
    bv_user_profile c,
    bv_mr_user_profile d
    where b.loanid = a.loanid
    and c.NET_USER_NO = a.resp_id
    and d.user_id = c.user_id
    and a.is_partner is null
    and a.create_date between
    TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
    TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    order by a.create_date

    Perhaps something like this...
    select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
    from (
          select distinct a.id,
                          create_date,
                          a.loanid,
                          a.rate,
                          a.pays,
                          a.gracetime,
                          a.emailtosend,
                          d.first_name,
                          d.last_name,
                          a.user_id,
                          max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
          from CLAL_LOANCALC_DET a,
               loan_Calculator b,
               bv_user_profile c,
               bv_mr_user_profile d
          where b.loanid = a.loanid
          and   c.NET_USER_NO = a.resp_id
          and   d.user_id = c.user_id
          and   a.is_partner is null
          and   a.create_date between
                TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
                TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    where create_date = max_create_date
    order by create_date

  • Please Help for the Query

    Please Help for the Query
    Hi frds please help me for the below query.What I want to do is to pull out the data from below table :-
    date ticker indicator
    03/13/2008 3IINFOTECH -8
    03/18/2008 3IINFOTECH -4
    03/25/2008 3IINFOTECH -5
    03/27/2008 3IINFOTECH -3
    as such :-
    date ticker indicator
    03/13/2008 3IINFOTECH -8
    03/25/2008 3IINFOTECH -5
    03/27/2008 3IINFOTECH -3
    Here I want to find the Trend i.e either asc or desc order from the lowest indicator.
    In the above sample data -8, -4, -5, -3 out of which I want the asc order data -8, -5, -3 and exclude -4 data.Because the asc order -8, -5, -3 will not follow.
    So I want the data
    date ticker indicator
    03/13/2008 3IINFOTECH -8
    03/25/2008 3IINFOTECH -5
    03/27/2008 3IINFOTECH -3

    SQL> CREATE TABLE BORRAME(FECHA DATE, INDICA VARCHAR2(100));
    Tabla creada.
    SQL> INSERT INTO BORRAME VALUES(TO_DATE('03/13/2008','MM/DD/YYYY'), '3IINFOTECH -8');
    1 fila creada.
    SQL> INSERT INTO BORRAME VALUES(TO_DATE('03/18/2008','MM/DD/YYYY'), '3IINFOTECH -4');
    1 fila creada.
    SQL> INSERT INTO BORRAME VALUES(TO_DATE('03/25/2008','MM/DD/YYYY'), '3IINFOTECH -5');
    1 fila creada.
    SQL> INSERT INTO BORRAME VALUES(TO_DATE('03/27/2008','MM/DD/YYYY'), '3IINFOTECH -3');
    1 fila creada.
    SQL> COMMIT;
    Validación terminada.
    SQL>
    SQL> SELECT FECHA, INDICA
      2  FROM BORRAME
      3  WHERE SUBSTR(INDICA,INSTR(INDICA,'-',1)+1,LENGTH(INDICA)) <> '4'
      4  ORDER BY SUBSTR(INDICA,INSTR(INDICA,'-',1)+1,LENGTH(INDICA)) DESC;
    FECHA                                                                
    INDICA                                                               
    13/03/08                                                             
    3IINFOTECH -8                                                        
    25/03/08                                                             
    3IINFOTECH -5                                                        
    27/03/08                                                             
    3IINFOTECH -3                                                        
                    

  • HT1338 I have a macbook Pro i7 mid november 2010. I am wondering if i can exchange my notebook with the latest one. Can anyone help me out with this query please.

    I have a macbook Pro i7 mid november 2010. I am wondering if i can exchange my notebook with the latest one. Can anyone help me out with this query please.

    You can sell your existing computer using eBay, Craigslist or the venue of your choice. You could then use the proceeds to purchase a new computer.

  • Please help with tricky query

    I need help with SQL query (if it can be accomplished with query at all).
    I'm going to create a table with structure similar to:
    Article_Name varchar2(30), Author_Name varchar2(30), Position varchar2(2). Position field is basicly position of an article author in the author list, e.g. if there is one author, his/her position is 0, if 2, then 1st author is 0, second is 1, etc.
    Article_Name Author_Name Position
    Outer Space Smith 0
    Outer Space Blake 1
    How can I automate creation of Position, based on number of authors on the fly? Let's say I have original table without Position, but I want to create a new table that will have this information.
    Regards

    If you have an existing table whose structure doesn't tell you what position the author is in, what's the algorithm you'd use to determine who was the first author, the second author, etc? If you issue a select query on a table without providing an "order by" clause, Oracle makes no guarantees about the order in which it retrieves rows.
    As an aside, why would you store position number in a varchar2 field? If it's a number, it ought to be stored as a number.
    Justin

  • Please help me this query

    Plz help me this query:
    Create a query that displays the employees last name and indicates the amounts of their annual salaries with *. Each * signifies a thousand $'s.
    EG. If the salary of an employee with last name king is 2000 then it should appear as follows:
    King **
    2 (*) for 2 thousand.
    Thanks in advance

    Hi,
    I was able to solve this query on my own. For those who might face a problem in future the solutionto it is:
    SELECT rpad(last_name, length(last_name) + (salary/1000), '*') AS employee_and_their_salaries
    FROM employees;
    Thanks

  • Help in Joining query

    Hi, How I can get desired result. I am joining two tables but if date timeline does't exist in #two table then should show hyphen (-). Please help on this query. Thanks.
    create table #one (code_p char(4), code_h char(2), code_date datetime)
    insert into #one values ('DEHG','2','2010-01-01')
    insert into #one values ('DEHG','2','2011-01-01')
    insert into #one values ('DEHC','2','2009-01-01')
    insert into #one values ('DEHG','2','2012-01-01')
    create table #two (code_p char(4), code_h char(2), code_date datetime)
    insert into #two values ('DEHG','2','2010-01-01')
    insert into #two values ('DEHC','2','2009-01-01')
    select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
    p.code_p code_p_two, p.code_h code_h_two, p.code_date code_date_two from #one p join #two a on p.code_p = a.code_p
    --Result from the above query
    code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
    DEHG     2    2010-01-01     DEHG     2     2010-01-01
    DEHG     2    2011-01-01     DEHG     2     2010-01-01
    DEHC     2    2009-01-01     DEHG     2     2009-01-01
    DEHC     2    2012-01-01     DEHG     2     2009-01-01
    --Desired result
    code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
    DEHG     2    2010-01-01     DEHG     2     2010-01-01
    DEHG     2    2011-01-01     DEHG     2     -
    DEHC     2    2009-01-01     DEHG     2     2009-01-01
    DEHC     2    2012-01-01     DEHG     2     -

    Try this:
    select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
    p.code_p code_p_two, p.code_h code_h_two, ISNULL(CONVERT(varchar(50),a.code_date,121),'-') code_date_two
    from #one p
    left join #two a on p.code_p = a.code_p and p.code_date=a.code_date
    If this post answers your query, please click "Mark As Answer" or "Vote as Helpful".

  • Help with my query

    Hello all,
    Total newbie to this pl/sql stuff. So, deseperately need help in my query.
    BOOKING_ID     BOOKING_STATUS     BOOKING_DATE     BOOKING_TIME     BOOKING_DATE_TIME
    1234567     CANCELLED     20090301     37252     5/1/2010 10:20
    1234567     CANCELLED 20090301     44229     5/1/2010 12:17
    1234567     BOOKED     20090301     39462     5/1/2010 10:57
    1234567     CANCELLED     20090301     43549     5/1/2010 12:05
    9671111     BOOKED     20090301     68124     5/1/2010 12:57
    9671111     CANCELLED     20090301     45001     5/1/2010 12:05
    How do I write my query such that I would get the following results:
    BOOKING_ID     BOOKING_STATUS     BOOKING_DATE     BOOKING_TIME     BOOKING_DATE_TIME
    9671111     BOOKED     20090301     68124     2/4/2010 12:17
    Basically, I am looking at the latest BOOKING_TIME and making sure the BOOKING_STATUS=BOOKED, if not, don't even bother bring back the result. Hence, you see that BOOKING_ID=1234567 is not required since at the latest BOOKING_TIME=44229, the BOOKING_STATUS=CANCELLED.
    Any help is greatly appreciated.
    Thank you in advance for your help.
    Stanley Ho

    Hi, Stanley,
    Welcome to the forum!
    Whenever you have a question, please post your sample data in a form that people can actually use. CREATE TABLE and INSERT statements are perfect.
    For example:
    CREATE TABLE     booking
    (     booking_id          NUMBER (8)
    ,     booking_status          VARCHAR2 (10)
    ,     booking_date_time     DATE
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 10:20', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 12:17', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'BOOKED',        TO_DATE ('5/1/2010 10:57', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (9671111,        'BOOKED',        TO_DATE ('5/1/2010 12:57', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (9671111,        'CANCELLED',        TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));What you want is called a Top-N Query .
    Here's one way to do it:
    WITH     got_rnum  AS
         SELECT     booking.*
         ,     ROW_NUMBER () OVER ( PARTITION BY  booking_id
                                   ORDER BY          booking_date_time     DESC
                           ) AS rnum
         FROM    booking
    SELECT     booking_id
    ,     booking_status
    ,     TO_CHAR (booking_date_time, 'YYYYMMDD')               AS booking_date
    ,     TO_CHAR (booking_date_time, 'SSSSS')               AS booking_time
    ,     TO_CHAR (booking_date_time, 'MM/DD/YYYY HH24:MI')     AS booking_date_time
    FROM     got_rnum
    WHERE     rnum          = 1
    AND     booking_status     = 'BOOKED'
    ;Notice that you don't need PL/SQL to do this; plain old SQL is good enough.
    Of course, if you're using PL/SQL for other reasons, you can use a query like this within PL/SQL.
    Dates (including time of day) should always be stored in DATE columns.
    If you have a DATE column, like booking_date_time, then there's no need for redundant date and time columns.
    You can always display just the year-month-day, or just the time, in any format, as I did above.
    The output from the query above, with the data above, is:
    BOOKING_ID BOOKING_ST BOOKING_ BOOKI BOOKING_DATE_TIM
       9671111 BOOKED     20100501 46620 05/01/2010 12:57I realize the booking_date and booking_time columns aren't quite what you posted. If they are not derivable from booking_date_time, then you probably do need separate columns for them, and those columns can easily be added to the query above.
    Edited by: Frank Kulash on Feb 5, 2010 4:41 PM
    KEEP (DENSE_RANK ...) , like Max used below, is a great tool to have in your kit. The problem with it is that you have to repeat a lot of stuff for every column, so the more columns you have in your output, the more tedious it gets. ROW_NUMBER sclaes much better, and is adaptable to more situations. I suggest you master ROW_NUMBER first, and look into KEEP (DENSE_RANK ...) later.

Maybe you are looking for

  • Can't connect to mail server - DNS Server settings?

    Hello: Let me preface this question by saying that computer networking is not my strong suit. Last week I moved our web hosting from our ISP to Lunarpages; our domain is marshall-arts.net. I set up some POP email accounts on our new mail servers and

  • What is the meaning of dumping in oracle?

    What is the meaning of dumping in oracle? Also tell me is there any method to dump by user commands?

  • Logical backups of Intermedia data

    Hi, New Intermedia user looking for more information on backup and recovery issues with regard to Intermedia tables. I tried to do an export of some intermedia info and didn't seem to get the tables. Thanks.

  • I tunes songs playing as a background in e-mails. How to?  I dy,

    Hi everybody,   I subscribe to i Tunes and I have tons of music stored in my i Pod.  My e-mail server is Gmail(Primary) and Comcast(secondary).  Here's my question:  Is there a way to transfer a song off my i Tunes list and have it played as backgrou

  • Flash Player won't install properly in Mac

    I have repeatedly attempted to install the most current version of Adobe Flash Player in my 10.8.4 Mac without success.  Actually it installs, but does not give me opportunity to accept your terms (which I am willing to do).  When I try to open a fil