Whats wrong with the sql

Hi ALL
( SELECT DISTINCT 'Negotiation' calc_type
, TRUNC(SYSDATE - COALESCE(RECPROP,AMENDREQ,SELECTION_DATE)) days
, contract_number
, ROW_NUMBER ( ) OVER ( PARTITION BY contract_number ORDER BY contract_number )
                         rn
FROM DW_CONTRACT_VW
WHERE (contract_type != 'On Call'
AND amendment_status_code IN ( 3,8,9,10,11,12,13,14,15,16,17,18)
     AND FIRMSIGN IS NULL )
or
(contract_type ='On Call'
and amendment_status_code in (1,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
and firmsign is null))
WHERE rn = 1
why am i getting the following error?
I got the error on the last WHERE statement
ORA-00933 SQL command not properly ended
Cause: The SQL statement ends with an inappropriate clause. For example, an ORDER BY clause may have been included in a CREATE VIEW or INSERT statement. ORDER BY cannot be used to create an ordered view or to insert in a certain order.
Action: Correct the syntax by removing the inappropriate clauses. It may be possible to duplicate the removed clause with another SQL statement. For example, to order the rows of a view, do so when querying the view and not when creating it. This error can also occur in SQL*Forms applications if a continuation line is indented. Check for indented lines and delete these spaces.
any advice is grtly appreciated
thank you

HI,
If you have blank lines in the middle of the statement, SQL*Plus will raise an error unless you have SET SQLBLANKLINES ON.

Similar Messages

  • Whats wrong with the sql query-- giving no rows selected

    i have a table audit_data with 4 columns , eff_m & end_m are declared as timestamp and the time stamp data is different for all the records
    Please advice as soon as possible as this is a production problem
    ORG_GRP_I grn_n ED_EFF_m (timestamp) ED_END_M OFV
    1 999 01-Jan-06 1 03-Aug-06 0 0
    1 999 28-Jul-06 0 04-Aug-06 1 0
    1 999 04-Aug-06 1 04-Aug-06 1 0
    1 999 04-Aug-06 0 6.08
    This query is giving no rows returned,even though there is data in database
    SELECT NVL(OFV,0)
    FROM audit_data ag
    WHERE ag.ORG_GRP_I = 1
    AND ag.GRN_N = 999
    AND (ag.ED_EFF_M,NVL(ag.ED_END_M,SYSDATE)) IN (SELECT MAX(ED_EFF_M),MAX(NVL(ED_END_M,SYSDATE))
    FROM audit_data ag2
    WHERE ag2.ORG_GRP_I= ag.org_grp_i
    AND ag2.grn_n=ag.grn_n
    AND To_Date(To_Char(ag2.ED_EFF_M,'MM/DD/YYYY'),'MM/DD/YYYY') <= TO_DATE(TO_CHAR('08/04/06','MM/DD/YYYY'),'MM/DD/YYYY')
    AND To_Date(To_Char(NVL(ag2.ED_END_M,SYSDATE),'MM/DD/YYYY'),'MM/DD/YYYY') >= TO_DATE(TO_CHAR('08/04/06','MM/DD/YYYY'),'MM/DD/YYYY'));
    This query is giving value 6.08, when made type conversions.
    what wrong with the above query.please advice
    SELECT NVL(OFV,0)
    FROM audit_data ag
    WHERE ag.ORG_GRP_I = 1
    AND ag.GRN_n = 999
    AND (to_date(to_char(ag.ED_EFF_M,'mm/dd/yyyy'),'mm/dd/yyyy'),NVL(to_date(to_char(ag.ED_END_M,'mm/dd/yyyy'),'mm/dd/yyyy'),SYSDATE)) IN
    (SELECT MAX(to_date(to_char(ED_EFF_M,'mm/dd/yyyy'),'mm/dd/yyyy')),MAX(NVL(ED_END_M,SYSDATE))
    FROM audit_data ag2
    WHERE ag2.ORG_GRP_I= ag.org_grp_i
    AND ag2.grn_n=ag.grn_n
    AND To_Date(To_Char(ag2.ED_EFF_M,'MM/DD/YYYY'),'MM/DD/YYYY') <= TO_date('08/04/2006','MM/DD/YYYY')
    AND To_Date(To_Char(NVL(ag2.ED_END_M,SYSDATE),'MM/DD/YYYY'),'MM/DD/YYYY') >= TO_DATE('08/04/2006','MM/DD/YYYY'));

    In the second query you convert a string to a date correctly using TO_DATE(string,date format)
    In the first query you try to make a char of something that already is a char, e.g. TO_CHAR('08/04/06','MM/DD/YYYY').
    The TO_CHAR functions expects either a number or a date as its first parameter. That's why it's giving you a ORA-01722.

  • Whats wrong with the sql query insert .... plz tell me

    {
    String sql = "INSERT INTO candidate VALUES (";
    sql += candidate.getEnrollmentNumber() + ",";
    sql += candidate.getDateOfEnrollment() + ",";
    sql += candidate.getName() + ",";
    sql += candidate.getContactNumber() + ",";
    sql += candidate.getGender() + ",";
    sql += candidate.getMailingAddress() + ",";
    sql += candidate.getEmailId() +",";
    sql += candidate.getClassOfVehicle() + ",";
    sql += candidate.getTypeOfVehicle() + ",";
    sql += candidate.getDateOfBirth() + ",";
    sql += candidate.getBatchTime() +")";
    System.out.println("sql: " +sql);
    //executeQuery(sql);
    executeUpdate(sql);
    }catch(SQLException e){
    e.printStackTrace();
    Giving me error as " com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;

    If you are inserting a string into your sql, you need to put it in single quotes (but not numbers).
    Example:
    sql += " ' "+candidate.getFirstName() " ', "
    Additionally, if firstName contains a single quote, it needs to be escaped by converting it to two single quotes.
    Note you don't have to do any of this if you use preparedStatements.
    Note: remove both spaces in " ' " so you get "'" in the above example, otherwise you are inserting spaces into your value.
    I use " ' " so you can more easily see what I did..

  • What wrong with this sql ?

    what wrong with my sql ?? When i try to run a explain plain against it. I get it that perfectly fine...if the syntax was wrong i wont get the explain plan, then why am i getting invalid number error ??? is it the data ?? i am on 10.2.0.3
    Edited by: S2K on Aug 18, 2009 10:21 AM

    S2K wrote:
    i am sorry i am not able to get what you mean...do you mean i should have it something like this ??
    CASE WHEN tv.var_data_typ_nm = 'Continuous' THEN ROUND(cr.rslt_qty, tmc.rpt_prcsn)
    ELSE cr.rslt_qty END,
    Well, I did not realize column with QTY in the name is stored as string. Then your issue is values stored in cr.rslt_qty - at least one is not numeric. Keep in mind balnk string will fail:
    SQL> select round(' ') from dual
      2  /
    select round(' ') from dual
    ERROR at line 1:
    ORA-01722: invalid number
    SQL> So if you can have blank cr.rslt_qty use LTRIM/RTRIM.
    SY.

  • Whats wrong with the servers i cant download update 5.1 firmware

    whats wrong with the servers i cant download update 5.1 firmware ive been trying for 2 days it says i dont have permission to access the requested source

    What says you don't have permission to access the requested source? That is not an error that should come up at all either from iTunes or when attempting an over the air upgrade. At least not an error that has anything to do with the servers.
    It could be a permissions problem on your computer or an issue being caused by your firewall or antivirus software.

  • Whats wrong with this sql statement ??

    Hello all, I am trying to run the below query out of persheet(tanel poder) performance excel chart...but i get below error...db is on 9.2
    what is wrong with this sql statement ?
    http://blog.tanelpoder.com/2008/12/28/performance-visualization-made-easy-perfsheet-20-beta/
    select * from (
    with fsq as (
      select /*+ materialize */
       i.dbid
        , i.instance_name
        , i.instance_number
    --    , trunc(s.snap_time, 'DD')     DAY
    --    , to_number(to_char(s.snap_time, 'HH24'))  HOUR
    --   -- , to_char(s.snap_time, 'MI')    MINUTE
    --    , 0           MINUTE
        , trunc(
          lag(s.snap_time, 1)
          over(
           partition by
          v.dbid
           , i.instance_name
           , v.instance_number
           , v.event
         order by
          s.snap_time
          , 'HH24'
         )           SNAP_TIME
        , v.event_type        EVENT_TYPE
        , v.event          EVENT_NAME
        , nvl(
        decode(
         greatest(
          time_waited_micro,
          nvl(
           lag(time_waited_micro,1,0)
           over(
            partition by
             v.dbid
              , i.instance_name
              , v.instance_number
              , v.event
            order by v.snap_id
           , time_waited_micro
         time_waited_micro,
         time_waited_micro - lag(time_waited_micro,1,0)
         over (
          partition by
           v.dbid
            , i.instance_name
            , v.instance_number
            , v.event
          order by v.snap_id
         time_waited_micro
           , time_waited_micro
         ) / 1000000         SECONDS_SPENT
        , total_waits         WAIT_COUNT
      from
       (select distinct dbid, instance_name, instance_number from stats$database_instance) i
        , stats$snapshot s
        , ( select
         snap_id, dbid, instance_number, 'WAIT' event_type, event, time_waited_micro, total_waits
        from
         stats$system_event
        where
         event not in (select event from stats$idle_event)
        union all
        select
         snap_id, dbid, instance_number,
         case
          when name in ('CPU used by this session', 'parse time cpu', 'recursive cpu usage') then 'CPU'
          when name like 'OS % time' then 'OS'
          else 'STAT'
         end,
         name , value, 1
        from
         stats$sysstat
    --    where      name in ('CPU used by this session', 'parse time cpu', 'recursive cpu usage')
    --    or  name like('OS % time')
    --    or 1 = 2 -- this will be a bind variable controlling whether all stats need to be returned
       ) v
      where
       i.dbid = s.dbid
      and i.dbid = v.dbid
      and s.dbid = v.dbid
      and s.snap_id = v.snap_id
      and s.snap_time between '%FROM_DATE%' and '%TO_DATE%'
      and i.instance_name = '%INSTANCE%'
    select * from (
      select
       instance_name
        , instance_number
        , snap_time
        , trunc(snap_time, 'DD')  DAY
        , to_char(snap_time, 'HH24') HOUR
        , to_char(snap_time, 'MI') MINUTE      
        , event_type  
        , event_name  
        , seconds_spent
        , wait_count  
        , ratio_to_report(seconds_spent) over (
    --      partition by (to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09'))
          partition by (snap_time)
          ) ratio
      from fsq
      where
       snap_time is not null -- lag(s.snap_time, 1) function above will leave time NULL for first snapshot
      -- to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09')
      -- > ( select min(to_char(day, 'YYYYMMDD')||to_char(hour,'09')||to_char(minute, '09')) from fsq)
    where ratio > 0
    order by
        instance_name
      , instance_number
      , day
      , hour
      , minute
      , event_type
      , seconds_spent desc
      , wait_count desc
    Error at line 6
    ORA-00604: error occurred at recursive SQL level 1
    ORA-00972: identifier is too long

    Hi Alex,
    Subquery factoring a.k.a. the with-clause should be possible on 9.2:
    http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_103a.htm#2075888
    (used it myself as well on 9.2)
    @OP
    I recall having problems myself using PL/SQL Developer and trying to get the with clause to work on 9.2 some years ago.
    A workaround might be to create a view based on the query.
    Also, your error message is "ORA-00972: identifier is too long"...
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14219/e900.htm#sthref419
    Can't test things currently, no 9.2 available at the moment, but perhaps tomorrow I'll have a chance.

  • Whats wrong with the code?????

    Hi All
    Pls can anyone help me, what is wrong with the below code, when ever i tried giving 10 digit number to chk out whether it is prime or not it is giving an NumberFormatException to me, pls can anyone help me...
    class PrimeNo{
    public static void main(String args[])
    int a = Integer.parseInt(args[0]);
    int count=0;
    for(int i=2;i<a;i++){
    if(a%i==0)
    System.out.println(a+" is not a Prime Number");
    count = 1;
    break;
    if(count==0)
    System.out.println(a+" is a Prime Number");
    Thnx and rgrds
    Badri

    Just curious, since this has already been answered, but to simplify code, would it be possible to set it so that if the value is not a prime number, just do the value like so:
    if (//code determines it is prime)
    System.out.println ("prime number");
    else
    prime = !prime;
    if(!prime)
    System.out.println ("not a prime number");not really code there, more pseudocode, but you get my drift. Would this make sense to do?

  • Whats wrong with the Network?

    What is wrong with the network? for about two days now i havent had 3g or 4g on my phone (droid razr maxx) just out of the blue it goes off my phone and hasnt come back. Does anyone know what happend?

        Yorktown301, that's very strange, but I'm here to figure this out! Are you still having problems connecting to 3G or 4G? Let's first verify that no settings on the phone were changed. Go to Settings, More, Mobile networks, Network Mode. Ensure LTE/CDMA is selected. Go back to Settings, Data usage and make sure Mobile Data is on. If still no change, remove both your battery and SIM card for about 30 seconds. If you're still having problems, let me know what zip code you're located in.
    MarquiaF_VZW
    Follow us on Twitter @VZWSupport

  • What wrong with the buttery ?

    hallow
    my question about the buttery i don't know what wrong with it . the buttery get finish after one and half hour using .
    please help me
    my Email
    [email protected]
    my mobile
    +966554302679

    Hi kamranh,
    I'd try starting Firefox in [[Safe Mode]]. If you don't have the issue while all of your add-ons, extensions, and themes are disabled, you can try adding them back in one by one until you find the culprit. You should look at the [https://support.mozilla.org/en-US/kb/Troubleshooting-extensions-themes Extensions and Themes troubleshooting guide ] and the [[Troubleshooting plugins]] article as well.
    From the looks of it though, your issue may be because you've been hit with some [https://support.mozilla.org/en-US/kb/Is%20my%20Firefox%20problem%20a%20result%20of%20malware?s=malware&r=0&e=sph&as=s#w_how-do-i-get-rid-of-malware Malware].
    If isn't malware or a plugin issue, you should take a look at the Knowledge Base article [[Search bar]]. Your default search might have been changed by a particular site. Sites like MSN, Yahoo, etc have been know to ask to become your homepage and change your search engine.
    If the address bar search has been changed as well, you might want to take a look at [[Location bar search]].
    If that doesn't work you should look at the article [[Preferences are not saved]].
    Hopefully this helps!

  • Whats wrong with the compilation?

    Dear all,
    I have a jsp file and when I check the codes, I dun see anything wrong with it.
    It gave me an error of below:
    C:\jakarta-tomcat-3.3.1a\work\DEFAULT\ROOT\AdvanceSearchResult_1.java:7577: Method popBody() not found in class javax.servlet.jsp.PageContext.
    out = pageContext.popBody();
    ^
    28 errors, 1 warning
         at org.apache.tomcat.facade.JasperLiaison.javac(JspInterceptor.java:898)
         at org.apache.tomcat.facade.JasperLiaison.processJspFile(JspInterceptor.java:733)
         at org.apache.tomcat.facade.JspInterceptor.requestMap(JspInterceptor.java:506)
         at org.apache.tomcat.core.ContextManager.processRequest(ContextManager.java:968)
         at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:875)
         at org.apache.tomcat.core.ContextManager.service(ContextManager.java:833)
         at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Http10Interceptor.java:176)
         at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:494)
         at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:516)
         at java.lang.Thread.run(Thread.java:534)
    I don't really get what this error means and how to debug.
    Please kindly advise me.
    Thanks in advance,
    Keng Yiam

    By servlet.jar I mean the jar file that contains the javax.* classes - particularly in your case javax.servlet.jsp.PageContext.
    It should be sitting in one of your [TOMCAT_HOME]/lib directories - lib/common I think. According to the jakarta website says that this version of Tomcat SHOULD support this call. What I was thinking is maybe you had an older version floating around your system somewhere, which could confuse Tomcat. Make sure that there aren't any sitting around in web-inf/lib or [JAVA_HOME]lib/ext etc etc
    The pushbody/popbody call will be generated by the translation of the JSP into a servlet (ie it is automatically generated code)Is it open to you to upgrade? I would recommend at least version 3.3.2, and version 4 if possble.
    Good luck,
    evnafets

  • Whats wrong with the light sensor

    Hello HP,
    I am really excited using my HP Pavilion X2. Ya i just got it from online store since 2 weeks ago. I  am happy with this one. Really. But arround 4 days ago i found a something wrong with my HP regarding the light sensor which is located in the near of camera. When i turned off the light still on, its mean it does not completely shut down yet. So the only way is press the on/off button in 5 seconds, them it will turn off completely.  I wonder what is going on with my Pav X2. Would you help me to identify and. then solve this problem? Just in case, i've already asked to the local HP service representative and they suggest me to recovery but it takes arround 7 days lefted in the service center. I decide to bringing back and try to learn how I can recover this pc tablet. Really it makes me worried. Your answer and explanation is really help me.
    Thank you so much
    Regards
    Naelil

    You arn't making any sense.. the light sensor does not light up, nore would it have anything to do with turning the laptop off.  When activated, the light sensor lets windows try and set the brightness of the screen automaticly. I allways disagree with it so im allways turning it off. 

  • Whats wrong with the last update?

    Since the last update, im not able to read a video on the net, could be youtube, daylimotion or  streaming, no ONE! So, I ve unistalled  flash player, re install it twice! and it still didnt work? whats wrong again? (and no it s not a problem from firefox,it's all updated, nor from an add-on( same) nor my own computer, its from flash player (when i ve unistalled it, i can read  some videos...)
    and no i havent real player...
    Any clue?
    (Btw; why oblige the user of flash to add the "not so good" Mac afée's scan"? )

    What OS?

  • Whats wrong with the Nokia update servers?

    Ive been trying to update my 5800 with the latest software version using the updtaer built in on the phone. Im using my home wireless connection to do so, it happens that it either stops downloading on the middle or says server doesnt respond. any idea how to do this? Cause this thing is starting to pi** me off. Courrent version is  11.0.008
    Ive tried more than 1 wireless connection and its the same problem. Either says there are no updates, server not responding or stops in the middle of the download
    thanks
    EDIT: also i cant open any page using wireless. Can it be imcompatibility with the router?
    Message Edited by tubago on 06-Jun-2009 03:33 PM
    Message Edited by tubago on 06-Jun-2009 03:34 PM
    Courrently using: 5800, N80, 6680, 6131and SE K850i
    Owns, besides the ones using: 3310, 7250, NGage, 3660, 6600, 6510, 8310, 8910i, 6822, 8850

    Problem solved. It was a problem on the router i found the solution here
    http://whyiseverytitletaken.blogspot.com/2008/11/nokia-n85-wifi-no-gateway-reply.html
    Courrently using: 5800, N80, 6680, 6131and SE K850i
    Owns, besides the ones using: 3310, 7250, NGage, 3660, 6600, 6510, 8310, 8910i, 6822, 8850

  • Whats wrong with the query

    Hi
    I have a view which is obtained by using multiple unions, sub queries which made the query complex. The view works good and gives correct data when i select all the records from the view. But the problem is when I use where clause and filter then it is giving me an error message which is shown below. In other words "select * from view" works good but "select * from view where dateran >to_date('3/3/2011','mm/dd/yyyy') ".
    Now I dont understand is why the query is not working when I use the where clause. Can any one tell what could be the reason?
    ERROR MESSAGE
    +"ORA-01427: single-row subquery returns more than one row+
    +01427. 00000 - "single-row subquery returns more than one row"+
    Thanks,

    CREATE OR replace FORCE VIEW "SECO"."D_REWORK_REPORT_V"
    ("MATERIALID", "CUSTOMER", "ORDER#", "PRODUCT", "STEELGRADEID", "GAUGE", "WIDTH"
    , "DEFECTCODE", "DEFECTCOMMENT", "RESPONSIBLEUNIT", "SEVERITY", "COMMENTS",
    "ENTRYCOIL", "ENTRYWEIGHT", "DATERAN", "SHIFTDATERAN")
    AS
      SELECT
      /*+ USE_HASH */ mf.me_id                                        materialid,
                      tp.customername,
                      v.au_nr                                         order#,
                      taup.productid                                  product,
                      taup.sc_steelgradeid                            steelgrade,
                      taup.coilorderthickness                         thick,
                      taup.coilorderwidth                             width,
                      mf.fehlertyp                                    defectcode,
                      ft.displaytext                                  defectcomment,
                      mf.fehlerverursacher
                      unitresponsible,
                      mf.auspraegung                                  priority,
                      mf.bemerkung                                    comments,
                      (SELECT matbezeichnung
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               ereignisiderfassung))
                                                                      entrytcoil,
                      (SELECT aftergew
                       FROM   d_matevents_v c
                       WHERE  c.me_id = v.me_id
                              AND c.ereignisid = ereignisiderfassung) entryweight,
                      (SELECT erstelldatum
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               ereignisiderfassung))
                                                                      dateran,
                      (SELECT CASE
                                WHEN To_number(To_char(erstelldatum, 'HH24')) < 7
                              THEN
                                erstelldatum - 1
                                ELSE erstelldatum
                              END
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               ereignisiderfassung))
                                                                      shiftdateran
      FROM   matfehler mf,
             matfehler_prj mfp,
             tap_prj tp,
             taup_prj taup,
             vlfehlerverursacher_v fv,
             vlfehlertyp_v ft,
             vlfehlerauspraegung_v fa,
             d_matevents_v v
      WHERE  mf.me_id = v.me_id
             AND v.au_nr = tp.au_nr(+)
             AND tp.au_nr = taup.au_nr(+)
             AND mf.fehlerverursacher = fv.fehlerverursacher(+)
             AND mf.fehlerverursacher <> 'TAC'
             AND mf.fehlertyp = ft.fehlertyp(+)
             AND mf.auspraegung = fa.auspraegung(+)
             AND mf.me_id = mfp.me_id
             AND mf.lfd_nr = mfp.lfd_nr
             AND v.fa_nr LIKE '%-%'
             AND v.bemerkung = 'kmpL3TMArb.processTTM_REPORT_TO_L3'
             AND v.dtstorno IS NULL
             AND Trunc (mf.erstelldatum) BETWEEN
                 To_date ('01/01/'
                          || ( To_char (SYSDATE,
                               'YYYY') - 1
                     'MM/DD/YYYY') AND Trunc (SYSDATE)
             AND v.ereignisid = (SELECT MIN (ereignisid)
                                 FROM   matereignis a
                                 WHERE  a.fa_nr LIKE '%-%'
                                        AND Nvl (a.bemerkung, 'A') =
                                            'kmpL3TMArb.processTTM_REPORT_TO_L3'
                                        AND a.dtstorno IS NULL
                                        AND a.me_id = v.me_id
                                        AND 2 = (SELECT COUNT (DISTINCT
                                                        matbezeichnung
                                                 FROM   matereignis b
                                                 WHERE  b.fa_nr LIKE '%-%'
                                                        AND b.me_id = a.me_id
                                                        AND b.dtstorno IS NULL)
                                 GROUP  BY a.me_id)
             AND ereignisiderfassung = (SELECT MAX (a.ereignisid)
                                        FROM   matereignis a
                                        WHERE  a.me_id = v.me_id
                                               AND a.ereignisid < v.ereignisid
                                               AND a.dtstorno IS NULL
                                               AND ( a.ausloeser = 'DIALOG'
                                                      OR a.ausloeser =
                                                         'MaterialQM_MPanel' )
                                               AND ( a.bemerkung != 'Unblocking'
                                                      OR Substr (a.bemerkung, 1, 9)
                                                         =
                                                         'kmpSIVPES'
      UNION
      SELECT
      /*+ USE_HASH */ mf.me_id
                      materialid,
                      tp.customername,
                      v.au_nr
                      order#,
                      taup.productid
                      product,
                      taup.sc_steelgradeid
                      steelgrade,
                      taup.coilorderthickness
                      thick,
                      taup.coilorderwidth
                      width,
                      mf.fehlertyp
                      defectcode,
                      ft.displaytext
                      defectcomment,
                      mf.fehlerverursacher
                      unitresponsible,
                      mf.auspraegung
                      priority,
                      mf.bemerkung
                      comments,
                      matbezeichnung
                      entrytcoil,
                      aftergew
                      entryweight,
                      (SELECT erstelldatum
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               v.ereignisid))
                      dateran,
                      (SELECT CASE
                                WHEN To_number(To_char(erstelldatum, 'HH24')) < 7
                              THEN
                                erstelldatum - 1
                                ELSE erstelldatum
                              END
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               v.ereignisid))
                      shiftdateran
      FROM   matfehler mf,
             matfehler_prj mfp,
             tap_prj tp,
             taup_prj taup,
             vlfehlerverursacher_v fv,
             vlfehlertyp_v ft,
             vlfehlerauspraegung_v fa,
             d_matevents_v v
      WHERE  mf.me_id = v.me_id
             AND v.au_nr = tp.au_nr(+)
             AND tp.au_nr = taup.au_nr(+)
             AND mf.fehlerverursacher = fv.fehlerverursacher(+)
             AND mf.fehlertyp = ft.fehlertyp(+)
             AND mf.auspraegung = fa.auspraegung(+)
             AND mf.me_id = mfp.me_id
             AND mf.lfd_nr = mfp.lfd_nr
             AND v.fa_nr LIKE '%-%'
             AND Trunc (mf.erstelldatum) BETWEEN
                 To_date ('01/01/'
                          || ( To_char (SYSDATE,
                               'YYYY') - 1
                     'MM/DD/YYYY') AND Trunc (SYSDATE)
             -- If a coil gets a rework more than 2 times, get the first record
             AND v.ereignisid = (SELECT MIN (ereignisid)
                                 FROM   matereignis a
                                 WHERE  a.fa_nr LIKE '%-%'
                                        AND a.dtstorno IS NULL
                                        AND a.me_id = v.me_id
                                        AND 2 < (SELECT COUNT (DISTINCT
                                                        matbezeichnung
                                                 FROM   matereignis b
                                                 WHERE  b.fa_nr LIKE '%-%'
                                                        AND b.me_id = a.me_id
                                                        AND b.dtstorno IS NULL)
                                 GROUP  BY a.me_id)
             AND ereignisiderfassung = (SELECT MAX (a.ereignisid)
                                        FROM   d_matevents_v a
                                        WHERE  a.me_id = v.me_id
                                               AND a.ereignisid <
                                                   (SELECT b.ereignisid
                                                    FROM   matereignis b
                                                    WHERE  b.fa_nr LIKE '%-%'
                                                           AND
                                                   b.metyp = 'Anbinden'
                                                           AND b.matbezeichnung =
                                                               v.matbezeichnung
                                                           AND b.me_id = v.me_id))
      UNION
      SELECT
      /*+ USE_HASH */ mf.me_id
                      materialid,
                      tp.customername,
                      v.au_nr
                      order#,
                      taup.productid
                      product,
                      taup.sc_steelgradeid
                      steelgrade,
                      taup.coilorderthickness
                      thick,
                      taup.coilorderwidth
                      width,
                      mf.fehlertyp
                      defectcode,
                      ft.displaytext
                      defectcomment,
                      mf.fehlerverursacher
                      unitresponsible,
                      mf.auspraegung
                      priority,
                      mf.bemerkung
                      comments,
                      matbezeichnung
                      entrytcoil,
                      aftergew
                      entryweight,
                      (SELECT erstelldatum
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               v.ereignisid)) AS
                      dateran,
                      (SELECT CASE
                                WHEN To_number(To_char(erstelldatum, 'HH24')) < 7
                              THEN
                                erstelldatum - 1
                                ELSE erstelldatum
                              END
                       FROM   matereignis c
                       WHERE  c.me_id = v.me_id
                              AND bemerkung = 'process scrap'
                              AND subtyp = 'Produktion'
                              AND dtstorno IS NULL
                              AND matbezeichnung = (SELECT a.matbezeichnung
                                                    FROM   d_matevents_v a
                                                    WHERE  me_id = v.me_id
                                                           AND dtstorno IS NULL
                                                           AND a.ereignisid =
                                                               v.ereignisid)) AS
                      shiftdateran
      FROM   matfehler mf,
             matfehler_prj mfp,
             tap_prj tp,
             taup_prj taup,
             vlfehlerverursacher_v fv,
             vlfehlertyp_v ft,
             vlfehlerauspraegung_v fa,
             d_matevents_v v
      WHERE  mf.me_id = v.me_id
             AND v.au_nr = tp.au_nr(+)
             AND tp.au_nr = taup.au_nr(+)
             AND mf.fehlerverursacher = fv.fehlerverursacher(+)
             AND mf.fehlertyp = ft.fehlertyp(+)
             AND mf.auspraegung = fa.auspraegung(+)
             AND mf.me_id = mfp.me_id
             AND mf.lfd_nr = mfp.lfd_nr
             AND v.fa_nr LIKE '%-%'
             AND Trunc (mf.erstelldatum) BETWEEN
                 To_date ('01/01/'
                          || ( To_char (SYSDATE,
                               'YYYY') - 1
                     'MM/DD/YYYY') AND Trunc (SYSDATE)
             -- If a coil gets a rework more than 2 times, get the second record
             AND v.ereignisid = (SELECT ereignisid
                                 FROM   matereignis a
                                 WHERE  a.fa_nr LIKE '%-%'
                                        AND a.me_id = v.me_id
                                        AND a.dtstorno IS NULL
                                        AND ROWNUM = 1
                                        AND a.ereignisid > (SELECT MIN (ereignisid)
                                                            FROM   matereignis b
                                                            WHERE
                                            b.fa_nr LIKE '%-%'
                                            AND b.dtstorno IS
                                                NULL
                                            AND
                                                           b.me_id = a.me_id
                                                                   AND 2 <
                                                           (SELECT COUNT (DISTINCT
                                                                   matbezeichnung)
                                                            FROM   matereignis c
                                                            WHERE
                                            c.fa_nr LIKE '%-%'
                                            AND
                                                           c.me_id = b.me_id
                                                                   AND c.dtstorno IS
                                                                       NULL)
                                                            GROUP  BY a.me_id))
             AND ereignisiderfassung = (SELECT MAX (a.ereignisid)
                                        FROM   d_matevents_v a
                                        WHERE  a.me_id = v.me_id
                                               AND a.ereignisid <
                                                   (SELECT MIN (b.ereignisid)
                                                    FROM   matereignis b
                                                    WHERE  b.fa_nr LIKE '%-%'
                                                           AND
                                                   b.metyp = 'Anbinden'
                                                           AND b.matbezeichnung =
                                                               v.matbezeichnung
                                                           AND b.me_id = v.me_id)); 

  • Whats wrong with the UNION ALL

    Hi All
    I have the following table and data
    WITH sample_data AS
         (SELECT '1' AS bin_data_id, '539' AS traffic_sample_id,
                 '1100' AS end_intv_time, '0' AS bin_1_data, '34' AS bin_2_data,
                 '19' AS bin_3_data
            FROM DUAL
          UNION ALL
          SELECT '2', '539', '1200', '0', '65', '18'
            FROM DUAL
          UNION ALL
          SELECT '3', '539', '1300', '0', '51', '17'
            FROM DUAL
          UNION ALL
          SELECT '4', '539', '1400', '0', '65', '27'
            FROM DUAL
          UNION ALL
          SELECT '5', '539', '1500', '0', '99', '48'
            FROM DUAL
          UNION ALL
          SELECT '6', '539', '1600', '0', '426', '138'
            FROM DUAL
          UNION ALL
          SELECT '7', '539', '1700', '0', '151', '62'
            FROM DUAL
          UNION ALL
          SELECT '8', '539', '1800', '0', '80', '32'
            FROM DUAL
          UNION ALL
          SELECT '9', '539', '1900', '0', '31', '11'
            FROM DUAL
          UNION ALL
          SELECT '10', '539', '2000', '0', '37', '11'
            FROM DUAL
          UNION ALL
          SELECT '11', '539', '2100', '0', '24', '9'
            FROM DUAL
          UNION ALL
          SELECT '12', '539', '2200', '0', '16', '5'
            FROM DUAL
          UNION ALL
          SELECT '13', '539', '2300', '0', '27', '12'
            FROM DUAL
          UNION ALL
          SELECT '14', '539', '2400', '0', '55', '20'
            FROM DUAL
          UNION ALL
          SELECT '15', '539', '0100', '0', '18', '9'
            FROM DUAL
          UNION ALL
          SELECT '16', '539', '0200', '0', '134', '52'
            FROM DUAL
          UNION ALL
          SELECT '17', '539', '0300', '0', '230', '69'
            FROM DUAL
          UNION ALL
          SELECT '18', '539', '0400', '0', '15', '7'
            FROM DUAL
          UNION ALL
          SELECT '19', '539', '0500', '0', '6', '5'
            FROM DUAL
          UNION ALL
          SELECT '20', '539', '0600', '0', '47', '23'
            FROM DUAL
          UNION ALL
          SELECT '21', '539', '0700', '0', '100', '41'
            FROM DUAL
          UNION ALL
          SELECT '22', '539', '0800', '0', '196', '43'
            FROM DUAL
          UNION ALL
          SELECT '23', '539', '0900', '0', '81', '20'
            FROM DUAL
          UNION ALL
          SELECT '24', '539', '1000', '0', '58', '28'
            FROM DUAL
          UNION ALL
          SELECT '25', '539', '1100', '0', '58', '24'
            FROM DUAL
          UNION ALL
          SELECT '26', '539', '1200', '0', '60', '22'
            FROM DUAL
          UNION ALL
          SELECT '27', '539', '1300', '0', '42', '18'
            FROM DUAL
          UNION ALL
          SELECT '28', '539', '1400', '0', '53', '15'
            FROM DUAL
          UNION ALL
          SELECT '29', '539', '1500', '0', '107', '43'
            FROM DUAL
          UNION ALL
          SELECT '30', '539', '1600', '0', '441', '146'
            FROM DUAL
          UNION ALL
          SELECT '31', '539', '1700', '0', '128', '34'
            FROM DUAL
          UNION ALL
          SELECT '32', '539', '1800', '0', '67', '27'
            FROM DUAL
          UNION ALL
          SELECT '33', '539', '1900', '0', '45', '22'
            FROM DUAL
          UNION ALL
          SELECT '34', '539', '2000', '0', '24', '13'
            FROM DUAL
          UNION ALL
          SELECT '35', '539', '2100', '0', '16', '11'
            FROM DUAL
          UNION ALL
          SELECT '36', '539', '2200', '0', '28', '5'
            FROM DUAL
          UNION ALL
          SELECT '37', '539', '2300', '0', '23', '8'
            FROM DUAL
          UNION ALL
          SELECT '38', '539', '2400', '0', '73', '11'
            FROM DUAL
          UNION ALL
          SELECT '39', '539', '0100', '0', '16', '3'
            FROM DUAL
          UNION ALL
          SELECT '40', '539', '0200', '0', '243', '82'
            FROM DUAL
          UNION ALL
          SELECT '41', '539', '0300', '0', '121', '55'
            FROM DUAL
          UNION ALL
          SELECT '42', '539', '0400', '0', '17', '4'
            FROM DUAL
          UNION ALL
          SELECT '43', '539', '0500', '0', '5', '5'
            FROM DUAL
          UNION ALL
          SELECT '44', '539', '0600', '0', '41', '15'
            FROM DUAL
          UNION ALL
          SELECT '45', '539', '0700', '0', '101', '34'
            FROM DUAL
          UNION ALL
          SELECT '46', '539', '0800', '0', '184', '43'
            FROM DUAL
          UNION ALL
          SELECT '47', '539', '0900', '0', '69', '15'
            FROM DUAL
          UNION ALL
          SELECT '48', '539', '1000', '0', '51', '18'
            FROM DUAL
          UNION ALL
          SELECT '49', '539', '1100', '0', '61', '25'
            FROM DUAL)
    SELECT *
      FROM sample_data;Now i have 2 queries , the one below query is to just get the last bin_data_id per each traffic_sample_id
    SELECT ROW_NUMBER () OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id)
                                                                            AS rn_1,
           COUNT (bin_data_id) OVER (PARTITION BY traffic_sample_id) AS cnt_1,TRAFFIC_SAMPLE_ID,bin_data_id,(END_INTV_TIME_1+100) AS END_INTV_TIME,BIN_1_DATA,BIN_2_DATA,BIN_3_DATA
      FROM
    SELECT ROW_NUMBER() OVER(ORDER BY TRAFFIC_SAMPLE_ID) AS RN,traffic_sample_id,
           end_intv_time,
           last_value(end_intv_time) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS END_INTV_TIME_1,
           LAST_VALUE (bin_data_id) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_data_id,
           LAST_VALUE (bin_1_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_1_data,
           LAST_VALUE (bin_2_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_2_data,
           LAST_VALUE (bin_3_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_3_data
      FROM bin_data
      WHERE RN=BIN_DATA_ID
      and traffic_sample_id=539And now i'm doing the UNION ALL the above query with a different query as below
    select rn_1,cnt_1,traffic_sample_id, bin_data_id, to_number(end_intv_time),bin_1_data,bin_2_data,bin_3_data
    from
    select ROW_NUMBER () OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id)
                                                                            AS rn_1,
           COUNT (bin_data_id) OVER (PARTITION BY traffic_sample_id) AS cnt_1,
           traffic_sample_id, bin_data_id, end_intv_time, direction,bin_1_data,bin_2_data,bin_3_data
           from
    SELECT ROW_NUMBER () OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id)
                                                                            AS rn,
           COUNT (bin_data_id) OVER (PARTITION BY traffic_sample_id) AS cnt,
           traffic_sample_id, bin_data_id, end_intv_time, direction,bin_1_data,bin_2_data,bin_3_data
      FROM bin_data
      where rn!=1
      where traffic_sample_id=539
      union all
      SELECT ROW_NUMBER () OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id)
                                                                            AS rn_1,
           COUNT (bin_data_id) OVER (PARTITION BY traffic_sample_id) AS cnt_1,TRAFFIC_SAMPLE_ID,bin_data_id,(END_INTV_TIME_1+100) AS END_INTV_TIME,BIN_1_DATA,BIN_2_DATA,BIN_3_DATA
      FROM
    SELECT ROW_NUMBER() OVER(ORDER BY TRAFFIC_SAMPLE_ID) AS RN,traffic_sample_id,
           end_intv_time,
           last_value(end_intv_time) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS END_INTV_TIME_1,
           LAST_VALUE (bin_data_id) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_data_id,
           LAST_VALUE (bin_1_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_1_data,
           LAST_VALUE (bin_2_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_2_data,
           LAST_VALUE (bin_3_data) OVER (PARTITION BY traffic_sample_id ORDER BY bin_data_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
                                                                            AS bin_3_data
      FROM bin_data
      WHERE RN=BIN_DATA_ID
      and traffic_sample_id=539Now all works fine and appends the single record to the set of records and i get the following data set.
    RN_1     CNT_1     TRAFFIC_SAMPLE_ID     BIN_DATA_ID     TO_NUMBER(END_INTV_TIME)     BIN_1_DATA     BIN_2_DATA     BIN_3_DATA
    1     48     539     2     1200     0     65     18
    2     48     539     3     1300     0     51     17
    3     48     539     4     1400     0     65     27
    4     48     539     5     1500     0     99     48
    5     48     539     6     1600     0     426     138
    6     48     539     7     1700     0     151     62
    7     48     539     8     1800     0     80     32
    8     48     539     9     1900     0     31     11
    9     48     539     10     2000     0     37     11
    10     48     539     11     2100     0     24     9
    11     48     539     12     2200     0     16     5
    12     48     539     13     2300     0     27     12
    13     48     539     14     2400     0     55     20
    14     48     539     15     100     0     18     9
    15     48     539     16     200     0     134     52
    16     48     539     17     300     0     230     69
    17     48     539     18     400     0     15     7
    18     48     539     19     500     0     6     5
    19     48     539     20     600     0     47     23
    20     48     539     21     700     0     100     41
    21     48     539     22     800     0     196     43
    22     48     539     23     900     0     81     20
    23     48     539     24     1000     0     58     28
    24     48     539     25     1100     0     58     24
    25     48     539     26     1200     0     60     22
    26     48     539     27     1300     0     42     18
    27     48     539     28     1400     0     53     15
    28     48     539     29     1500     0     107     43
    29     48     539     30     1600     0     441     146
    30     48     539     31     1700     0     128     34
    31     48     539     32     1800     0     67     27
    32     48     539     33     1900     0     45     22
    33     48     539     34     2000     0     24     13
    34     48     539     35     2100     0     16     11
    35     48     539     36     2200     0     28     5
    36     48     539     37     2300     0     23     8
    37     48     539     38     2400     0     73     11
    38     48     539     39     100     0     16     3
    39     48     539     40     200     0     243     82
    40     48     539     41     300     0     121     55
    41     48     539     42     400     0     17     4
    42     48     539     43     500     0     5     5
    43     48     539     44     600     0     41     15
    44     48     539     45     700     0     101     34
    45     48     539     46     800     0     184     43
    46     48     539     47     900     0     69     15
    47     48     539     48     1000     0     51     18
    48     48     539     49     1100     0     61     25
    1     1     539     49     1200     0     61     25But please help me , all the above queries are working fine when i check by traffic_sample_id, for example if i remove the WHERE CLAUSE, WHERE TRAFFIC_SAMPLE_ID=539 from both the queries in the UNION ALL to get all the TRAFFIC_SAMPLE_ID's the single records are not getting appended to the resultant data set. Please need help.
    Thanks

    Please consider the following sample data
    WITH sample_data AS
         (SELECT '1' AS bin_data_id, '539' AS traffic_sample_id,
                 '1100' AS end_intv_time, '0' AS bin_1_data, '34' AS bin_2_data,
                 '19' AS bin_3_data
            FROM DUAL
          UNION ALL
          SELECT '2', '539', '1200', '0', '65', '18'
            FROM DUAL
          UNION ALL
          SELECT '3', '539', '1300', '0', '51', '17'
            FROM DUAL
          UNION ALL
          SELECT '4', '539', '1400', '0', '65', '27'
            FROM DUAL
          UNION ALL
          SELECT '5', '539', '1500', '0', '99', '48'
            FROM DUAL
          UNION ALL
          SELECT '6', '539', '1600', '0', '426', '138'
            FROM DUAL
          UNION ALL
          SELECT '7', '539', '1700', '0', '151', '62'
            FROM DUAL
          UNION ALL
          SELECT '8', '539', '1800', '0', '80', '32'
            FROM DUAL
          UNION ALL
          SELECT '9', '539', '1900', '0', '31', '11'
            FROM DUAL
          UNION ALL
          SELECT '10', '539', '2000', '0', '37', '11'
            FROM DUAL
          UNION ALL
          SELECT '11', '539', '2100', '0', '24', '9'
            FROM DUAL
          UNION ALL
          SELECT '12', '539', '2200', '0', '16', '5'
            FROM DUAL
          UNION ALL
          SELECT '13', '539', '2300', '0', '27', '12'
            FROM DUAL
          UNION ALL
          SELECT '14', '539', '2400', '0', '55', '20'
            FROM DUAL
          UNION ALL
          SELECT '15', '539', '0100', '0', '18', '9'
            FROM DUAL
          UNION ALL
          SELECT '16', '539', '0200', '0', '134', '52'
            FROM DUAL
          UNION ALL
          SELECT '17', '539', '0300', '0', '230', '69'
            FROM DUAL
          UNION ALL
          SELECT '18', '539', '0400', '0', '15', '7'
            FROM DUAL
          UNION ALL
          SELECT '19', '539', '0500', '0', '6', '5'
            FROM DUAL
          UNION ALL
          SELECT '20', '539', '0600', '0', '47', '23'
            FROM DUAL
          UNION ALL
          SELECT '21', '539', '0700', '0', '100', '41'
            FROM DUAL
          UNION ALL
          SELECT '22', '539', '0800', '0', '196', '43'
            FROM DUAL
          UNION ALL
          SELECT '23', '539', '0900', '0', '81', '20'
            FROM DUAL
          UNION ALL
          SELECT '24', '539', '1000', '0', '58', '28'
            FROM DUAL
          UNION ALL
          SELECT '25', '539', '1100', '0', '58', '24'
            FROM DUAL
          UNION ALL
          SELECT '26', '539', '1200', '0', '60', '22'
            FROM DUAL
          UNION ALL
          SELECT '27', '539', '1300', '0', '42', '18'
            FROM DUAL
          UNION ALL
          SELECT '28', '539', '1400', '0', '53', '15'
            FROM DUAL
          UNION ALL
          SELECT '29', '539', '1500', '0', '107', '43'
            FROM DUAL
          UNION ALL
          SELECT '30', '539', '1600', '0', '441', '146'
            FROM DUAL
          UNION ALL
          SELECT '31', '539', '1700', '0', '128', '34'
            FROM DUAL
          UNION ALL
          SELECT '32', '539', '1800', '0', '67', '27'
            FROM DUAL
          UNION ALL
          SELECT '33', '539', '1900', '0', '45', '22'
            FROM DUAL
          UNION ALL
          SELECT '34', '539', '2000', '0', '24', '13'
            FROM DUAL
          UNION ALL
          SELECT '35', '539', '2100', '0', '16', '11'
            FROM DUAL
          UNION ALL
          SELECT '36', '539', '2200', '0', '28', '5'
            FROM DUAL
          UNION ALL
          SELECT '37', '539', '2300', '0', '23', '8'
            FROM DUAL
          UNION ALL
          SELECT '38', '539', '2400', '0', '73', '11'
            FROM DUAL
          UNION ALL
          SELECT '39', '539', '0100', '0', '16', '3'
            FROM DUAL
          UNION ALL
          SELECT '40', '539', '0200', '0', '243', '82'
            FROM DUAL
          UNION ALL
          SELECT '41', '539', '0300', '0', '121', '55'
            FROM DUAL
          UNION ALL
          SELECT '42', '539', '0400', '0', '17', '4'
            FROM DUAL
          UNION ALL
          SELECT '43', '539', '0500', '0', '5', '5'
            FROM DUAL
          UNION ALL
          SELECT '44', '539', '0600', '0', '41', '15'
            FROM DUAL
          UNION ALL
          SELECT '45', '539', '0700', '0', '101', '34'
            FROM DUAL
          UNION ALL
          SELECT '46', '539', '0800', '0', '184', '43'
            FROM DUAL
          UNION ALL
          SELECT '47', '539', '0900', '0', '69', '15'
            FROM DUAL
          UNION ALL
          SELECT '48', '539', '1000', '0', '51', '18'
            FROM DUAL
          UNION ALL
          SELECT '49', '539', '1100', '0', '61', '25'
            FROM DUAL
          UNION ALL
          SELECT '11033', '613','1300','3','421','124'
            FROM DUAL
          UNION ALL
          SELECT '11034', '613', '1400', '4', '419', '128'
            FROM DUAL
          UNION ALL
          SELECT '11035', '613', '1500', '5', '522', '169'
            FROM DUAL
          UNION ALL
          SELECT '11036', '613', '1600', '5', '596', '185'
            FROM DUAL
          UNION ALL
          SELECT '11037', '613', '1700', '9', '667', '150'
            FROM DUAL
          UNION ALL
          SELECT '11038', '613', '1800', '4', '522', '105'
            FROM DUAL
          UNION ALL
          SELECT '11039', '613', '1900', '2', '428', '80'
            FROM DUAL
          UNION ALL
          SELECT '11040', '613', '2000', '5', '304', '73'
            FROM DUAL
          UNION ALL
          SELECT '11041', '613', '2100', '1', '284', '61'
            FROM DUAL
          UNION ALL
          SELECT '11042', '613', '2200', '3', '245', '45'
            FROM DUAL
          UNION ALL
          SELECT '11043', '613', '2300', '6', '193', '39'
            FROM DUAL
          UNION ALL
          SELECT '11044', '613', '2400', '0', '109', '23'
            FROM DUAL
          UNION ALL
          SELECT '11045', '613', '0100', '0', '81', '25'
            FROM DUAL
          UNION ALL
          SELECT '11046', '613', '0200', '0', '56', '24'
            FROM DUAL
          UNION ALL
          SELECT '11047', '613', '0300', '0', '62', '11'
            FROM DUAL
          UNION ALL
          SELECT '11048', '613', '0400', '0', '59', '27'
            FROM DUAL
          UNION ALL
          SELECT '11049', '613', '0500', '1', '180', '87'
            FROM DUAL
          UNION ALL
          SELECT '11050', '613', '0600', '4', '410', '126'
            FROM DUAL
          UNION ALL
          SELECT '11051', '613', '0700', '6', '454', '138'
            FROM DUAL
          UNION ALL
          SELECT '11052', '613', '0800', '4', '393', '145'
            FROM DUAL
          UNION ALL
          SELECT '11053', '613', '0800', '4', '357', '131'
            FROM DUAL
          UNION ALL
          SELECT '11054', '613', '0900', '5', '347', '126'
            FROM DUAL
          UNION ALL
          SELECT '11055', '613', '1000', '3', '368', '155'
            FROM DUAL
          UNION ALL
          SELECT '11056', '613', '1100', '2', '384', '153'
            FROM DUAL
          UNION ALL
          SELECT '11057', '613', '1200', '7', '423', '166'
            FROM DUAL
          UNION ALL
          SELECT '11058', '613', '1300', '9', '489', '213'
            FROM DUAL
          UNION ALL
          SELECT '11059', '613', '1400', '5', '576', '190'
            FROM DUAL
          UNION ALL
          SELECT '11060', '613', '1500', '11', '712', '189'
            FROM DUAL
          UNION ALL
          SELECT '11061', '613', '1600', '9', '634', '211'
            FROM DUAL
          UNION ALL
          SELECT '11062', '613', '1700', '3', '501', '114'
            FROM DUAL
          UNION ALL
          SELECT '11063', '613', '1800', '2', '411', '117'
            FROM DUAL
          UNION ALL
          SELECT '11064', '613', '1900', '5', '291', '99'
            FROM DUAL
          UNION ALL
          SELECT '11065', '613', '2000', '1', '276', '71'
            FROM DUAL
          UNION ALL
          SELECT '11066', '613', '2100', '2', '214', '56'
            FROM DUAL
          UNION ALL
          SELECT '11067', '613', '2200', '3', '166', '46'
            FROM DUAL
          UNION ALL
          SELECT '11068', '613', '2300', '0', '99', '26'
            FROM DUAL
          UNION ALL
          SELECT '11069', '613', '2400', '0', '65', '19'
            FROM DUAL
          UNION ALL
          SELECT '11070', '613', '0100', '1', '57', '14'
            FROM DUAL
          UNION ALL
          SELECT '11071', '613', '0200', '0', '55', '15'
            FROM DUAL
          UNION ALL
          SELECT '11072', '613', '0300', '0', '49', '22'
            FROM DUAL
          UNION ALL
          SELECT '11073', '613', '0400', '3', '151', '113'
            FROM DUAL
          UNION ALL
          SELECT '11074', '613', '0500', '2', '324', '145'
            FROM DUAL
          UNION ALL
          SELECT '11075', '613', '0600', '5', '342', '161'
            FROM DUAL
          UNION ALL
          SELECT '11076', '613', '0700', '3', '294', '186'
            FROM DUAL
          UNION ALL
          SELECT '11077', '613', '0800', '6', '303', '171'
            FROM DUAL
          UNION ALL
          SELECT '11078', '613', '0900', '4', '278', '158'
            FROM DUAL
          UNION ALL
          SELECT '11079', '613', '1000', '2', '309', '156'
            FROM DUAL
          UNION ALL
          SELECT '11080', '613', '1100', '0', '51', '18'
            FROM DUAL
          UNION ALL
          SELECT '11081', '613', '1200', '0', '61', '25'
            FROM DUAL)
    SELECT *
      FROM sample_data;Here when i say the LAST(BIN_DATA_ID) i meant the last BIN_DATA_ID PER EACH TRAFFIC SAMPLE ID. What i was trying to do is in the bottom query i'm getting the LAST BIN_DATA_ID for each TRAFFIC SAMPLE ID and appending it to the top query.
    Thanks

Maybe you are looking for