Common Table Expersion - inside existing query - How to add it in?

I need to add somethint like the below query to an existing query. How do I do that?
with emp_att_t
as
select empid,
       count(*) over(partition by empid order by 1) total_cnt,
       sum(decode(present_yn, 'N',1,0)) over(partition by empid order by 1) abs_cnt
  from emp_att
select eh.empid, eh.name
  from emp eh
  join emp_att_t ed
    on eh.empid = ed.empid
where ed.total_cnt > 100
   and ed.abs_cnt > 5thanks for looking.
Edited by: GMoney on Mar 7, 2013 2:00 PM

Please disregard the query it's self - it has nothing to do with my question really.
The below is from a question you answered for me in this thread: Re: MAX Value for multiple fields in same Query
WITH     got_rn          AS
     SELECT     ck.circuit_design_id
     ,     sr.document_number
     ,     ck.product_id
     ,     sr.activity_ind
     ,     sr.order_compl_dt
     ,     ROW_NUMBER () OVER ( PARTITION BY  ck.circuit_design_id
                               ORDER BY          sr.document_number     -- or sr.order_compl_dt
                                   DESC
                       ) AS rn
     FROM    ck
     JOIN     src     ON     ck.circuit_design_id     = src.circuit_design_id
     JOIN     sr     ON     src.document_number     = sr.document_number
--     WHERE     ...     -- If you need other filtering, put it here
SELECT       circuit_design_id
,       document_number
,       product_id
,       activity_ind
,       order_compl_dt
FROM       got_rn
WHERE       rn     = 1
ORDER BY  circuit_design_id     -- if wantedAll I am really asking is how to do an INNER JOIN on one or more other queries.
Example
select *
from (
Select abc, def
from t1, t2
where t1.abc = t2.abc) result1
inner join
WITH     got_rn          AS
     SELECT     ck.circuit_design_id
     ,     sr.document_number
     ,     ck.product_id
     ,     sr.activity_ind
     ,     sr.order_compl_dt
     ,     ROW_NUMBER () OVER ( PARTITION BY  ck.circuit_design_id
                               ORDER BY          sr.document_number     -- or sr.order_compl_dt
                                   DESC
                       ) AS rn
     FROM    ck
     JOIN     src     ON     ck.circuit_design_id     = src.circuit_design_id
     JOIN     sr     ON     src.document_number     = sr.document_number
--     WHERE     ...     -- If you need other filtering, put it here
SELECT       circuit_design_id
,       document_number
,       product_id
,       activity_ind
,       order_compl_dt
FROM       got_rn
WHERE       rn     = 1
result2
ON result1.abc = result2.activity_indEdited by: GMoney on Mar 7, 2013 2:23 PM

Similar Messages

  • Can I use table function inside Dynamic query ?

    Dear Gurus,
    I have following code
    DECLARE
    TYPE CRITERIA_LIST_TABLE AS TABLE OF VARCHAR2(20);
    OtherNoList CRITERIA_LIST_TABLE; /* CRITERIA_LIST_TABLE is index by table*/
    QUERY_STRING VARCHAR2(4000);
    BEGIN
    OtherNoList := CRITERIA_LIST_TABLE();
    SELECT DISTINCT REGEXP_SUBSTR('1,5,6,4', '[^\,]+',1, LEVEL ) BULK COLLECT INTO OtherNoList
    FROM DUAL
    CONNECT BY LEVEL <= REGEXP_COUNT('1,5,6,4', '\,') + 1 ;
    QUERY_STRING := 'INSERT INTO TAB1 (C1,C2) '||
    'SELECT C1,'||
    'C2 '||
    'FROM TAB1 ,'||
    'TABLE( '||
              'CAST (OtherNoList AS CRITERIA_LIST_TABLE) '||
                   ') OTHRNOS '||
    'WHERE TAB1.C1 = OTHRNOS.COLUMN_VALUE ';
    DBMS_OUTPUT.PUT_LINE('Query String is '||QUERY_STRING);
    EXECUTE IMMEDIATE QUERY_STRING;
    END;
    Can I use Table function inside dynamic query.
    Thanking in advance
    Sanjeev

    Try:
    DECLARE
    TYPE CRITERIA_LIST_TABLE AS TABLE OF VARCHAR2(20);
    OtherNoList CRITERIA_LIST_TABLE; /* CRITERIA_LIST_TABLE is index by table*/
    QUERY_STRING VARCHAR2(4000);
    BEGIN
    OtherNoList := CRITERIA_LIST_TABLE();
    SELECT DISTINCT REGEXP_SUBSTR('1,5,6,4', '[^\,]+',1, LEVEL ) BULK COLLECT INTO OtherNoList
    FROM DUAL
    CONNECT BY LEVEL <= REGEXP_COUNT('1,5,6,4', '\,') + 1 ;
    QUERY_STRING := 'INSERT INTO TAB1 (C1,C2) '||
    'SELECT C1,'||
    'C2 '||
    'FROM TAB1 ,'||
    'TABLE( '||
    'CAST (:OtherNoList AS CRITERIA_LIST_TABLE) '||
    ') OTHRNOS '||
    'WHERE TAB1.C1 = OTHRNOS.COLUMN_VALUE ';
    DBMS_OUTPUT.PUT_LINE('Query String is '||QUERY_STRING);
    EXECUTE IMMEDIATE QUERY_STRING using OtherNoList;
    END;p.s. not tested
    Amiel Davis

  • Query: How to add new fields in ABAP query?

    Hi All,
    Can any one tell me how to add new fields in the ABAP query output?
    We have to add the new fields using query design or we have
    to add those in the background program(automatically generated behind query) by selecting new fields in the code?

    What kind of report? Usually, you have to add the field in the table declaration and then also in the SQL query. After that, include the field in what kind of report you are using.

  • How do I see the tables in an existing business area and add tables to it

    Hi
    I am having a business area which contains a number of business folders.
    I am creating a new custom folder in it but i dont know if the table exists in the business area or not. How can I see the lisst of tables which are being used by the business area.

    Hi,
    Easiest way is to look at the folder properties using the administrator tool, you can see there the objects the folder is created on.
    Another way is to create a SQL to query your needs.
    You can find here in the forum several SQL regarding the connection between folders and BA.
    In the folders table you will be able to see the source of it.
    Of course that for custom folder you will need to see the SQL to get your requirment.
    Tamir

  • SAP Query:How to add the following tables

    Hi,
       I want to build a query by joining Tables BSIK, BKPF and BSEG .but when i add BSEG it does not allow to join because its a cluster table. I couldnt find any logical database having these three tables in it. Can you kindly advice how it is achievable?
    Thanks,
    Faisal

    You are trying to add one of the 2 biggest tables in the system.
    Try to do a select in bkpf first and then either use for all entries or cursor approach for BSEG/BSIS.

  • Adding Additional Tables to an Existing Query

    Dear All
    I have the following tables and relationships:
    I have the following query that returns records from the table SENAlert where the StudentID and the TeacherUsername are passed in as parameters.
    Also, records are only returned if there isn’t a corresponding record in the SENAlertHistory table (i.e. SENAlertHistorySENAlertID IS NULL).
    This query is returning the results that I would expect:
    SELECT       
    SENAlert.SENAlertID, SENAlertType.SENAlertTypeDescription
    FROM           
    SENAlertHistory RIGHT OUTER JOIN
    Teacher INNER JOIN
    Class ON Teacher.TeacherCode = Class.ClassTeacherCode INNER JOIN
    ClassMember INNER JOIN
    SENAlert INNER JOIN
    SENAlertType ON SENAlert.SENAlertType = SENAlertType.SENAlertTypeID INNER JOIN
    Student ON SENAlert.SENAlertStudentID = Student.StudentID ON ClassMember.ClassMemberStudentID = Student.StudentID ON
    Class.ClassClassCode = ClassMember.ClassMemberClassCode ON SENAlertHistory.SENAlertHistorySENAlertID = SENAlert.SENAlertID AND
    SENAlertHistory.SENAlertHistoryTeacherCode = Teacher.TeacherCode
    WHERE       
    (Student.StudentID = 011763) AND (Teacher.TeacherUsername = 'dsmith') AND (SENAlertHistory.SENAlertHistorySENAlertID IS NULL)
    However, I need to extend this query to include additional teachers who may also teach this student. I am trying to add in 3 additional copies of
    the Teacher table as Teacher_1, Teacher_3 and Teacher _3 in order to include them in the query also.
    However, when I add these tables in I no longer get any results returned.
    I have managed to do this on other queries but not this one.
    SELECT SENAlert.SENAlertID, SENAlertType.SENAlertTypeDescription
    FROM SENAlertHistory INNER JOIN
    Teacher ON SENAlertHistory.SENAlertHistoryTeacherCode = Teacher.TeacherCode INNER JOIN
    Teacher AS Teacher_1 ON SENAlertHistory.SENAlertHistoryTeacherCode = Teacher_1.TeacherCode INNER JOIN
    Teacher AS Teacher_2 ON SENAlertHistory.SENAlertHistoryTeacherCode = Teacher_2.TeacherCode INNER JOIN
    Teacher AS Teacher_3 ON SENAlertHistory.SENAlertHistoryTeacherCode = Teacher_3.TeacherCode RIGHT OUTER JOIN
    Class INNER JOIN
    ClassMember INNER JOIN
    SENAlert INNER JOIN
    SENAlertType ON SENAlert.SENAlertType = SENAlertType.SENAlertTypeID INNER JOIN
    Student ON SENAlert.SENAlertStudentID = Student.StudentID ON ClassMember.ClassMemberStudentID = Student.StudentID ON
    Class.ClassClassCode = ClassMember.ClassMemberClassCode ON Teacher_3.TeacherCode = Class.ClassTeacherCode AND
    Teacher_2.TeacherCode = Class.ClassTeacherCode AND Teacher_1.TeacherCode = Class.ClassTeacherCode AND
    Teacher.TeacherCode = Class.ClassTeacherCode AND SENAlertHistory.SENAlertHistorySENAlertID = SENAlert.SENAlertID
    WHERE (Student.StudentID = 011763) AND (SENAlertHistory.SENAlertHistorySENAlertID IS NULL)
    AND (Teacher.TeacherUsername = 'admin\dsmith' OR Teacher_1.TeacherUsername = 'admin\dsmith' OR Teacher_2.TeacherUsername = 'admin\dsmith' OR Teacher_3.TeacherUsername = 'admin\dsmith')
    No results are returned from this adapted query. I have noticed that by adding the additional tables, it keeps changing the type of joins that I
    have between certain tables. I have tried all sorts of combinations but haven't been able to make it work.
    I would be really grateful for any advice that you may be able to offer.
    Many thanks
    Daniel

    Dear All
    I followed the advice and created the query again from scratch, one table and relationship at a time checking that I am getting the expected result. I have put much of the query back together with the correct output:
    SELECT
    SENAlert.SENAlertID,
    SENAlertType.SENAlertTypeDescription
    FROM dbo.SENAlert
    INNER JOIN dbo.SENAlertType
    ON SENAlert.SENAlertType = SENAlertType.SENAlertTypeID
    INNER JOIN dbo.Student
    ON SENAlert.SENAlertStudentID = Student.StudentID
    INNER JOIN dbo.ClassMember
    ON ClassMember.ClassMemberStudentID = Student.StudentID
    INNER JOIN dbo.Class
    ON ClassMember.ClassMemberClassCode = Class.ClassClassCode
    INNER JOIN dbo.Teacher
    ON Class.ClassTeacherCode = Teacher.TeacherCode
    LEFT OUTER JOIN dbo.Teacher AdditionalTeacher1
    ON AdditionalTeacher1.TeacherCode = Class.ClassAdditionalTeacherCode1
    LEFT OUTER JOIN dbo.Teacher AdditionalTeacher2
    ON AdditionalTeacher2.TeacherCode = Class.ClassAdditionalTeacherCode2
    LEFT OUTER JOIN dbo.Teacher AdditionalTeacher3
    ON AdditionalTeacher3.TeacherCode = Class.ClassAdditionalTeacherCode3
    WHERE Student.StudentID = 011763
    AND (Teacher.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher1.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher2.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher3.TeacherUsername = 'admin\dsmith')
    The problem arises when I try to add the SENAlertHistory table back in. I only want to show results where a related record does not exist in SENAlertHistory. I have tried every combination of relationship but none have been successful.
    I think maybe I need to be using a subquery instead.
    Thanks for your help
    Daniel
    do you mean this?
    SELECT
    SENAlert.SENAlertID,
    SENAlertType.SENAlertTypeDescription
    FROM dbo.SENAlert
    INNER JOIN dbo.SENAlertType
    ON SENAlert.SENAlertType = SENAlertType.SENAlertTypeID
    INNER JOIN dbo.Student
    ON SENAlert.SENAlertStudentID = Student.StudentID
    INNER JOIN dbo.ClassMember
    ON ClassMember.ClassMemberStudentID = Student.StudentID
    INNER JOIN dbo.Class
    ON ClassMember.ClassMemberClassCode = Class.ClassClassCode
    INNER JOIN dbo.Teacher
    ON Class.ClassTeacherCode = Teacher.TeacherCode
    AND NOT EXISTS ( SELECT 1
    FROM SENAlertHistory
    WHERE SENALertHistoryTeacherCode = Teacher.TeacherCode
    AND SENAlertHistorySENAlertID = SENAlert.SENAlertID)LEFT OUTER JOIN dbo.Teacher AdditionalTeacher1
    ON AdditionalTeacher1.TeacherCode = Class.ClassAdditionalTeacherCode1
    AND NOT EXISTS (SELECT 1
    FROM SENAlertHistory
    WHERE SENAlertHistoryTeacherCode = AdditionalTeacher1.TeacherCode
    AND SENAlertHistorySENAlertID = SENAlert.SENAlertID
    LEFT OUTER JOIN dbo.Teacher AdditionalTeacher2
    ON AdditionalTeacher2.TeacherCode = Class.ClassAdditionalTeacherCode2
    AND NOT EXISTS (SELECT 1
    FROM SENAlertHistory
    WHERE SENAlertHistoryTeacherCode = AdditionalTeacher2.TeacherCode
    AND SENAlertHistorySENAlertID = SENAlert.SENAlertID
    LEFT OUTER JOIN dbo.Teacher AdditionalTeacher3
    ON AdditionalTeacher3.TeacherCode = Class.ClassAdditionalTeacherCode3
    AND NOT EXISTS (SELECT 1
    FROM SENAlertHistory
    WHERE SENAlertHistoryTeacherCode = AdditionalTeacher3.TeacherCode
    AND SENAlertHistorySENAlertID = SENAlert.SENAlertID
    WHERE Student.StudentID = 011763
    AND (Teacher.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher1.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher2.TeacherUsername = 'admin\dsmith'
    OR AdditionalTeacher3.TeacherUsername = 'admin\dsmith')
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Bex Query how to add two results fields to give a new total

    Hello gurus,
    I am using the Bex query designer to develop a report for invoice payments for different document types. One of the requirements of the report is to add a total column to give a total of the results for the document types added together.
    For example, how do I add the results for document x and document y to give me a total of the two? Any help will be much appreciated.
    Thanks
    Forhad
        document X  document Y     total
    A       15                20
             10                30
    result  25               50                (25+50) 75?? (how do i get this total)
    Edited by: Forhad Hussain on Oct 24, 2008 10:32 AM

    Dear Reddy,
    Order
    Resource 
    Actual Time
    Planned Time
    Variance
    782
    A
    7.05
    48
    40.95 (I Kept in Hide)
    B
    7.15
    48.85 (I Kept in Hide)
    Results
    14.20
    48
    33.8
    I tried with CKF's in above way but iam not getting the Results Calculations 14.20 - 48 = 33.8
    Can you help me in this...
    Thanks in advance

  • Adhoc Query (How to add search help for IT0008-subty)

    Hello Experts,
    My requirement is to get the search help on Field
    <b>SUBTY IT0008</b>,
    Field SYBTY is already there in the infoset,
    but when query has been run and we try to select SUBTY values using F4 it doen't give anthing. 
    Is there is anyway of cusotmization to meet this requirement.
    Any help would be appreciated and awarded. 
    With regrads,
    Harsh

    Go to SPRO >Personnel Management>Personnel Administration>Payroll Data>Basic Pay>Define reason for change   here u can create ur own subtypes for Basic PAY Infotype 0008

  • REG: How to add new fields to existing Infospoke

    Hi All,
    We have an existing Infospoke, It has ODS as a Source and some fields are being pulled.
    But here is the scenario....
    I need some extra fields that are available in other ODS. How to add that fields ?
    By some research came to know that.. I need to lookup on the ODS and populate into existing Infospoke.How to add these fields to InfoSpoke using BAdi implementaion.
    Note: already BAdi is active in the infospoke.
    Please help me in this regard

    Hi Chase,
    As you mentioned that BADI is already implemented then I guess you should be able to extend the existing BADI itself.
    For doing lookup first you should know the key fields of source DSO then by using the existing field of Infospoke you will have to read data from DSO.
    DATA: it_data type standard table of 'DSO Name'.
    select Key1 key2 etc...
              Field1 Field2 Field3 Field4
              from DSO
              into table it_data
              for all entries of infospoke
             where key1 = infospoke1-key1
                         key2 = infospoke2-key2.
    This will give you all the required records from source DSO into internal table it_data. Now you using this table you can populate the original table of Infospoke.
    Regards,
    Durgesh.

  • Cannot perform dml operation inside a query

    I have created a function which does some dml opration.
    when I use it in a through a transformation operator, and execute the map,
    it throws the following error:
    cannot perform dml operation inside a query
    how to handle this?

    Hi,
    if you want to execute the dml within a mapping, use the pre or post mapping procress operator. Or use a sql*plus activity in the process flow.
    Regards,
    Carsten.

  • Retain existing query strings on seach

    Hi All
    I have the following scenario. Any help is appreciated.
    I have an existing query string in the url . When I click the search button, it removes all existing query string and adds its own and redirects to the results page.
    Is there a way I can retain the existing query strings in the url.
    I'm using Sharepoint 2013

    Hi,
    For reproducing this issue, please give the detailed information about the existing query string in URL and the parameter.
    In addition, please check if the link is useful for you:
    http://techmikael.blogspot.com/2013/09/appending-query-terms-in-sharepoint.html
    Best Regards,
    Wendy
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • How to add a new column to a report

    I want to add a new column to my existing report. I modified the using report wizard and added the new column. When I pressed 'Finish' I lost all my previous settings. Without changing the existing stuff, how can add a column to a table.
    Any help is appreciated.
    Regards
    Leena
    null

    go to the data model editor
    click on query .
    add new columns in select statement.
    add the corresponding fields in layout model
    compile it
    run it
    srini

  • How to add new operand in installation facts through FMs

    Hi,
    Please guide me for the following query:
    How to add new operands in installation facts through Function Module?
    Please guide me with any function module through which i can acheive the above requirement.
    Thanks and Regards

    Hi,
    Thanks for your reply please.
    Through the BAPI which you have specified i have not been able to add any new operand in the installation facts.
    Please guide me with the parameters which are required to be passed to create a new operand entry in the installation facts.
    Please guide.
    Thanks and Regards
    Edited by: MP Vashishth on Aug 11, 2011 1:43 PM

  • How to add rows using DefaultTableModel?

    I'm trying to figure out the ins-and-outs of creating a dynamic table.
    I looked for a tutorial, but none of the example programs I found dealt with my particular issue, nor did they explain anything.
    For instance, most of the examples looked like this:
            table = new JTable();
            defaultModel = new DefaultTableModel(10,5);
            setModel(defaultModel);But that doesn't work for me -- I'm creating a class that EXTENDS DefaultTableModel, and so if I want to send in parameters, I have a problem.
              public MyTableModel() {
                            super(data, columnNames);
                    ...The above code complains that "you can't access data or columnNames until you call super()!"
    But I've seen plenty of example code that DOES send in parameters to super(). How do they get away with that? Is it because they are parameters to the constructor, whereas private/public members of the class are somehow different?
    I thought a variable is a variable -- why is one allowed but not the other?
    Lastly, I'm trying to figure out, practically speaking, how to load a database with X items, and be able to add rows later. Does that mean I need to use a vector for my data instead of Object[][]?
    I've seen BOTH AbstractDataModel and DefaultTableModel used with dynamic tables -- is there any difference between them as far as dynamic tables are concerned?
    I was under the impression that I needed to "switch" from ADM to DTM if I wanted to be able to add rows to my table at runtime.
    Here are some references to previous discussions I found/participated in on this topic:
    http://forum.java.sun.com/thread.jspa?threadID=5224966
    http://forum.java.sun.com/thread.jspa?threadID=439141&start=0&tstart=0
    Thanks in advance for any help,
    Matthew

    -> I'm creating a class that EXTENDS DefaultTableModel, and so if I want to send in parameters, I have a problem
    Well, you would pass in the data and columnNames as parameter when you create your table model.
    -> Lastly, I'm trying to figure out, practically speaking, how to load a
    -> database with X items, and be able to add rows later. Does that
    -> mean I need to use a vector for my data instead of Object[][]?
    You just said you extended DefaultTableModel. Well you don't need to do anything it already manages the data for you. You just use the methods provided to update and change the data.
    -> I've seen BOTH AbstractDataModel and DefaultTableModel used with dynamic tables
    No you haven't. You really don't understand what an AbstractDataModel is do you? Its nothing. You can't use it. It doesn't have any storage for the data, and since there is no data storage you can't access the data or change it. You can't even create an AbstractTableModel. Read your Java text on what an Abstract class it.
    The DefaultTableModel extends the AbstractTableModel to provide data storage and ways to get the data and change the data. It notifies the table when any change is made to the data so the table can repaint itself.
    -> How to add rows using DefaultTableModel?
    Finally, the DTM provides methods that make the model dynamic. Methods for adding and removing rows or adding and removing columns. Read the API for more information.
    If you still don't understand how how the methods work then search the forum for examples that use the methods you don't understand. I've posted an example the uses the methods required to add rows or columns.

  • How to use the not exists query

    Hello,
    Using obiee 11g,
    i would like to know how to deal with not exists query in the obiee11g,
    the query is like this
    select count(a.col1),a.col2 from table1 a ,table2 b where a.col1=b.col1
    and not exists
    (select d.col1 from table3 d, table4 e
    where d.col1=e.col1 and d.col1=a.col1)
    group by a.col2
    there are 4 tables.
    table1,table2,table3,tabel4
    this is what i need in a report,these tables are used in othere places also,
    would prefer if its possible to do in for a report i.e.e analysis itself.
    If not possible then may be in rpd since if i change in rpd it may reflect every place
    and not in for a report itself.
    thanks

    Implement this in report itself can be a tricky and tedious job..But better to have it in RPD - Physical - Select Table type
    USE NOT IN in place of NOT Exist..You can simply use SELECT statement in SELECT TABLE TYPE
    Hope it clear

Maybe you are looking for

  • How can I see what equipment is connected to my network?

    I use a Time Capsule for my home network. Is there a way for me to see each device that is connected to my network at any given time? There has been an unknown device continue to show up in finder under "shared".... I've reset my network password a c

  • Error while trying to change or save as  query in query designer

    Hi , When we are trying to change or do a save as to a query in the query designer,it is giving us an error stating"'An unexpected object variable or with block variable not set'error occured in wdbrlog 1 error(s) are logged". We have searched in SDN

  • Animated GIF broken on iPhone, iPad Safari

    [This is a cross-post with the using iPhone forum https://discussions.apple.com/message/17908717#17908717 ] I develop a web site and I use animated GIFs. They used to work fine on both iPhone and iPad, but now they're broken. The animation plays but

  • HP Pavilion dv7-6b21sm Entertainment Notebook PC Motherboard - disc info

    Hello guys I have pavilion dv7 series laptop like it's stated up in the subject I bought cable with conector for 2nd disk. Now the question is what speed is possible through this cable or what my motherboard supports? I want to install SSD disk insid

  • Windows 8 and OEM media with updates and software

    Recently i had an idea to create a custom Windows 7, 8 OEM image that includes all the Windows updates and also has couple of softwares inside it (adobe reader, java, flash etc). I used MDT to do this. I came to this idea because every time i prepare