Query to get result

Hi All,
We are using SQL Server 2012. We have Table1 like below.
Table1:
SecurityKey
Description
PID
110
qqq
1
111
ddd
1
112
fff
1
113
vvvv
1
114
bbb
1
115
ggg
1
116
hhhh
1
One of the third party system adds records into the following table like this.
Table2
ID
MasterID
Type
Amount
SecurityKey
Date
TransNum
1
110
Buy
600
0
01/13/2014
2178
2
111
Buy
100
110
01/13/2014
2179
3
112
Buy
200
110
01/13/2014
2180
4
113
Buy
300
110
01/13/2014
2181
5
114
Sell
100
110
01/14/2014
6
115
Sell
200
110
01/14/2014
7
116
Sell
300
110
01/14/2014
In above table, primary key is combination of ID, MasterID and Type. All ‘Buy’ type transactions are entered into Table2 by Vendor system by running scheduled job. SecurityKey 110 has child records. So, in Table2, 111, 112, 113 belong to 110. Next day, another
system creates exact matching ‘Sell’ transactions to all buy transactions except for parent (here record with MasterID 110). When ‘Sell’ transactions are created, TransNum is not populated.
I need to write a select query in such a way that I need to get all ‘Buy’ Transactions for a particular date and get corresponding ‘Sell’ Transactions with TransNum populated.
The result set should look like this.
2
111
Buy
100
110
01/13/2014
2179
3
112
Buy
200
110
01/13/2014
2180
4
113
Buy
300
110
01/13/2014
2181
5
114
Sell
100
110
01/14/2014
2179
6
115
Sell
200
110
01/14/2014
2180
7
116
Sell
300
110
01/14/2014
2181
How can I write select query to get result set above by applying ‘Buy’ transaction TransNum to
 corresponding ‘Sell’Transaction. I see only amount and SecurityKey as joining columns.
Thanks,
Spunny

;With CTE
AS
SELECT ROW_NUMBER() OVER (PARTITION BY SecurityKey,Type ORDER BY MasterID) AS Seq,*
FROM Table2
WHERE SecurityKey > 0
SELECT t1.ID,
t1.MasterID,
t1.Type,
t1.Amount,
t1.SecurityKey,
t1.[Date],
COALESCE(t1.TransNum,t2.TransNum) AS TransNum
FROM CTE t1
INNER JOIN CTE t2
ON t1.SecurityKey = t2.SecurityKey
AND t1.Seq = t2.Seq
AND t2.Type = 'Buy'
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page

Similar Messages

  • How to run user enter value query and get result on Text item

    Hi ,
    I'm really newbie at JDeveloper and ADF , I want to run the query below , before with Forms i just adding :TEXT_NAME to get the entered users value , what I have to do now with ADF , really it's not clear question and I would appreciate any help with this ,
    select
    nvl(min(substr(a.starttime,1,8)),0) Service_Start_Time, nvl(sum(a.sessionvolume),0) Total_Traffic_KB
    FROM
    aaa_bill a
    where
    msid=:TEXT_ITEM
    and starttime >=(select max(fee) FROM aaa_bill
    where msid=:TEXT_ITEM and accountreason=5) ;

    You can use a ADF parameter form for this requirement.
    https://blogs.oracle.com/shay/entry/combining_multiple_queries_and

  • Query to get the hierarchical results

    Hi,
    Please help me in writing a Query to get the hierarchical results. I want a result like follows...
    course-----groupname---TotalMembers---NotStarted---INProgress---Completed
    Course1---country1--------12---------------6----------3-------------3
    Course1-----state11-------12---------------6----------3-------------3
    Course1------District111--10---------------5----------0-------------0
    Course1--------City1111----0---------------0----------0-------------0
    Course1--------City1112----1---------------0----------0-------------1
    Course1--------City1113----6---------------3----------2-------------1
    Course1---country2--------12---------------6----------3-------------3
    Course1----state21--------12---------------6----------3-------------3
    Course1------District211--10---------------5----------0-------------0
    Course1--------City2111----0---------------0----------0-------------0
    Course1--------City2112----1---------------0----------0-------------1
    Course1--------City2113----6---------------3----------2-------------1
    Course2---country1--------12---------------6----------2-------------3
    Course2----state11--------12---------------6----------2-------------3
    Course2------District111--10---------------5----------0-------------0
    Course2--------City1111----0---------------0----------0-------------0
    Course2--------City1112----1---------------0----------0-------------1
    Course2--------City1113----6---------------3----------1-------------2
    Course2---country2--------12---------------6----------3-------------3
    Course2-----state21-------12---------------6----------3-------------3
    Course2------District211--10---------------5----------0-------------0
    Course2--------City2111----0---------------0----------0-------------0
    Course2--------City2112----1---------------0----------0-------------1
    Course2--------City2113----6---------------3----------2-------------1
    These are the Tables available to me.
    (I have just given some examle data in tables, to get the idea)
    "Groups" Table (This table gives the information of the group)
    GROUPID-----NAME-------PARENTID
    1---------Universe--------1
    2---------country1--------1
    3---------state11---------2
    4---------District111-----3
    5---------City1111--------4
    6---------City1112--------4
    7---------City1113--------4
    8---------country2--------1
    9---------state21---------8
    10--------District211-----9
    11--------City2111--------10
    12--------City2112--------10
    13--------City2113--------10
    "Users" Table (This table provides the user information)
    userID----FIRSTNAME---LASTNAME
    user1-----------Jim-------Carry
    user2-----------Tom-------lee
    user3-----------sunny-----boo
    user4-----------mary------mall
    "User-Group" Tables (This table provides the relation between the groups
    and the members)
    GROUPID---userID
    3-------------user1
    3-------------user2
    3-------------user4
    4-------------user5
    5-------------user6
    5-------------user7
    user_score (This table provides the user scores of different courses)
    USERID----course-----STATUS
    user1------course1-----complete
    user1------course2-----NotStarted
    user2------course1-----NotStarted
    user2------course2-----complete
    user3------course1-----complete
    user3------course2-----InProgress
    user4------course2-----complete
    user4------course1-----NotStarted
    I will explain the first four lines of the above result.
    Course1---country1--------12---------------6----------4-------------2
    Course1-----state11-------12---------------6----------4-------------2
    Course1------District111--10---------------5----------3-------------2
    Course1--------City1111----0---------------0----------0-------------0
    Course1--------City1112----1---------------0----------0-------------1
    Course1--------City1113----6---------------3----------2-------------1
    # "city1111" group has 0 members
    # "city1112" group has 1 member (1 member completed the course1)
    # "city1113" group has 6 members(3 members notStarted,2 members
    InProgress,1 member completed the course1)
    # "District111" is the parent group of above three groups, and has 3
    members.(2 members NotStarted,1 member InProgress the course1). But this
    group has child groups, so the scores of this group has to rollup the
    child groups scores also. Thats why it has 2+3+0+0=6 members Not
    Started,1+2+0+0=3 members InProgress,0+0+1+1=2 members completed.
    # "state11" group also same as the above group.
    I am able to get the group hierarchy by using "Connect By" like follows
    "select name,groupid,parentid from groups_info start with groupid=1 connect by parentid = prior groupid;"
    But i want to get the result as i have mentioned in the begining of this discussion.
    I am using oracle 8i (oracle8.1.7).
    Thank you for any help
    Srinivas M

    This may not be exactly what you want,
    but it should be fairly close:
    SET      LINESIZE 100
    SET      PAGESIZE 24
    COLUMN   groupname FORMAT A20
    SELECT   INITCAP (user_score.course) "course",
             groupnames.name "groupname",
             COUNT (*) "TotalMembers",
             SUM (NVL (DECODE (UPPER (user_score.status), 'NOTSTARTED', 1), 0)) "NotStarted",
             SUM (NVL (DECODE (UPPER (user_score.status), 'INPROGRESS', 1), 0)) "InProgress",
             SUM (NVL (DECODE (UPPER (user_score.status), 'COMPLETE', 1), 0)) "Completed"
    FROM     user_score,
             user_group,
             (SELECT ROWNUM rn,
                     name,
                     groupid
              FROM   (SELECT     LPAD (' ', 2 * LEVEL - 2) || name AS name,
                                 groupid
                      FROM       groups
                      START WITH groupid = 1
                      CONNECT BY PRIOR groupid = parentid)) groupnames
    WHERE    user_score.userid = user_group.userid
    AND      user_group.groupid IN
             (SELECT     groupid
              FROM       groups
              START WITH groupid = groupnames.groupid
              CONNECT BY PRIOR groupid = parentid)
    GROUP BY user_score.course, groupnames.name, groupnames.rn
    ORDER BY user_score.course, groupnames.rn
    I entered the minimal test data that you
    provided and a bit more and got this result
    (It was formatted as you requested,
    but I don't know if it will display properly
    on this post, or wrap around):
    course  groupname            TotalMembers NotStarted InProgress  Completed
    Course1 Universe                        6          2          0          4
    Course1   country1                      5          2          0          3
    Course1     state11                     5          2          0          3
    Course1       District111               2          0          0          2
    Course1         City1112                1          0          0          1
    Course1         City1113                1          0          0          1
    Course1   country2                      1          0          0          1
    Course1     state21                     1          0          0          1
    Course1       District211               1          0          0          1
    Course1         City2113                1          0          0          1
    Course2 Universe                        5          1          1          3
    Course2   country1                      4          1          1          2
    Course2     state11                     4          1          1          2
    Course2       District111               1          0          1          0
    Course2         City1113                1          0          1          0
    Course2   country2                      1          0          0          1
    Course2     state21                     1          0          0          1
    Course2       District211               1          0          0          1
    Course2         City2113                1          0          0          1
    Here is the test data that I used, in case
    anyone else wants to play with it:
    create table groups
      (groupid  number,
       name     varchar2(15),
       parentid number)
    insert into groups
    values (1,'Universe',null)
    insert into groups
    values (2,'country1',1)
    insert into groups
    values (3,'state11',2)
    insert into groups
    values (4,'District111',3)
    insert into groups
    values (5,'City1111',4)
    insert into groups
    values (6,'City1112',4)
    insert into groups
    values (7,'City1113',4)
    insert into groups
    values (8,'country2',1)
    insert into groups
    values (9,'state21',8)
    insert into groups
    values (10,'District211',9)
    insert into groups
    values (11,'City2111',10)
    insert into groups
    values (12,'City2112',10)
    insert into groups
    values (13,'City2113',10)
    create table user_group
      (groupid number,
       userid  varchar2(5))
    insert into user_group
    values (3,'user1')
    insert into user_group
    values (3,'user2')
    insert into user_group
    values (3,'user4')
    insert into user_group
    values (4,'user5')
    insert into user_group
    values (5,'user6')
    insert into user_group
    values (5,'user7')
    insert into user_group
    values (7,'user8')
    insert into user_group
    values (13,'user9')
    insert into user_group
    values (11,'use11')
    insert into user_group
    values (6,'use6')
    create table user_score
      (userid varchar2(5),
       course varchar2(7),
       status varchar2(10))
    insert into user_score
    values ('use6','course1','complete')
    insert into user_score
    values ('user9','course1','complete')
    insert into user_score
    values ('user9','course2','complete')
    insert into user_score
    values ('user8','course1','complete')
    insert into user_score
    values ('user8','course2','InProgress')
    insert into user_score
    values ('user1','course1','complete')
    insert into user_score
    values ('user1','course2','NotStarted')
    insert into user_score
    values ('user2','course1','NotStarted')
    insert into user_score
    values ('user2','course2','complete')
    insert into user_score
    values ('user3','course1','complete')
    insert into user_score
    values ('user3','course2','InProgress')
    insert into user_score
    values ('user4','course2','complete')
    insert into user_score
    values ('user4','course1','NotStarted')

  • Can we use ad hoc query to get a temporary report of payroll result?

    Hi,All
    can we use ad hoc query to get a temporary report of payroll result?or we have to get a report of payroll result by customizing and ABAP?
    and how can I customizing a report on the basis of standard SAP report for payroll?

    Hi
    As of my Knowledge You cannot get the payroll report through Adhoc query you have to go for ABAP Devlopment
    Thanks
    Mahantesh

  • HOW TO CHANGE SQL QUERY TO GET SPECIFIC RESULT

    I HAVE FOLLOWING QUERY AND ITS FUNCTIONING BUT I WANT ALL THOSE RECORD SHOULD BE DISPLADE WICH HAVE NO TRANSACTION IN SALES ANS SALES RETURN WHILE RETREVING DATA FROM DATABASE.
    I TRIED TO USE + OPERATOR BUT I DON'T KNOW HOW TO CHANGE THE QUERY TO GET THE RESULT
    MAIN TABLE IS BEING USED TO FETCH RECORD
    SALES_HEADER,SALES_DETAIL
    SALES_HEADERR,SALES_DETAILR
    STOCK_REG
    AND THE QUERY IS
    select s.item_code,S.DESCR,s.GRAND_TOTAL,
    sum(decode(d.dayno,01,NVL(S.QTY,0),0)) DAY1,
    sum(decode(d.dayno,02,NVL(S.QTY,0),0)) DAY2,
    sum(decode(d.dayno,03,NVL(S.QTY,0),0)) DAY3,
    sum(decode(d.dayno,04,NVL(S.QTY,0),0)) DAY4,
    sum(decode(d.dayno,05,NVL(S.QTY,0),0)) DAY5,
    sum(decode(d.dayno,06,NVL(S.QTY,0),0)) DAY6,
    sum(decode(d.dayno,07,NVL(S.QTY,0),0)) DAY7,
    sum(decode(d.dayno,08,NVL(S.QTY,0),0)) DAY8,
    sum(decode(d.dayno,09,NVL(S.QTY,0),0)) DAY9,
    sum(decode(d.dayno,10,NVL(S.QTY,0),0)) DAY10,
    sum(decode(d.dayno,11,NVL(S.QTY,0),0)) DAY11,
    sum(decode(d.dayno,12,NVL(S.QTY,0),0)) DAY12,
    sum(decode(d.dayno,13,NVL(S.QTY,0),0)) DAY13,
    sum(decode(d.dayno,14,NVL(S.QTY,0),0)) DAY14,
    sum(decode(d.dayno,15,NVL(S.QTY,0),0)) DAY15,
    sum(decode(d.dayno,16,NVL(S.QTY,0),0)) DAY16,
    sum(decode(d.dayno,17,NVL(S.QTY,0),0)) DAY17,
    sum(decode(d.dayno,18,NVL(S.QTY,0),0)) DAY18,
    sum(decode(d.dayno,19,NVL(S.QTY,0),0)) DAY19,
    sum(decode(d.dayno,20,NVL(S.QTY,0),0)) DAY20,
    sum(decode(d.dayno,21,NVL(S.QTY,0),0)) DAY21,
    sum(decode(d.dayno,22,NVL(S.QTY,0),0)) DAY22,
    sum(decode(d.dayno,23,NVL(S.QTY,0),0)) DAY23,
    sum(decode(d.dayno,24,NVL(S.QTY,0),0)) DAY24,
    sum(decode(d.dayno,25,NVL(S.QTY,0),0)) DAY25,
    sum(decode(d.dayno,26,NVL(S.QTY,0),0)) DAY26,
    sum(decode(d.dayno,27,NVL(S.QTY,0),0)) DAY27,
    sum(decode(d.dayno,28,NVL(S.QTY,0),0)) DAY28,
    sum(decode(d.dayno,29,NVL(S.QTY,0),0)) DAY29,
    sum(decode(d.dayno,30,NVL(S.QTY,0),0)) DAY30,
    sum(decode(d.dayno,31,NVL(S.QTY,0),0)) DAY31,
    sum(S.qty) TU
    FROM
    (select nvl(sales_detail.qty+sales_detail.bonus,0) as qty,
    sales_detail.item_code AS ITEM_CODE,
    stock_reg.descr AS descr,
    s_date as sale_date,stock_reg.GRAND_TOTAL AS GRAND_TOTAL FROM
    SALES_DETAIL,SALES_HEADER,STOCK_REG
    WHERE SALES_DETAIL.S_ID = SALES_HEADER.S_ID AND
    SALES_DETAIL.ITEM_CODE = STOCK_REG.ITEM_CODE(+) AND
    STOCK_REG.COMCODE='C1'
    UNION all
    select -1*nvl(sales_detailR.qty+sales_detailR.bonus,0) as qty,
    sales_detailR.item_code AS ITEM_CODE,
    stock_reg.descr AS descr,
    RETURN_date as sale_date,stock_reg.GRAND_TOTAL AS GRAND_TOTAL FROM
    SALES_DETAILR,SALES_HEADERR,STOCK_REG
    WHERE SALES_DETAILR.SR_ID = SALES_HEADERR.SR_ID AND
    SALES_DETAILR.ITEM_CODE = STOCK_REG.ITEM_CODE(+) AND
    STOCK_REG.COMCODE='C1') S,
    (select level as dayno
    from dual
    connect by level <= 31) d
    where d.dayno = to_char(s.sALE_date, 'DD') AND TO_DATE(S.SALE_DATE) BETWEEN '&FROMDATE' AND '&TODATE'
    GROUP BY S.ITEM_CODE,S.DESCR,S.GRAND_TOTAL

    well my query running successfully just the quesion was that i wanted to bring some changes in the query to retrieve the complete record which has no transaction in sales
    for exmple
    if i write the quey
    select dept.deptno,sum(sal) from emp,dept
    where dept.deptno = emp.deptno
    group by dept.deptno
    the recult comes as
    DEPTNO SUM(SAL)
    10 8750
    20 10875
    30 9400
    though there is another deptno which is deptno no 40
    have no sal
    and when i use this query
    select dept.deptno,sum(sal) from emp,dept
    where dept.deptno = emp.deptno(+)
    group by dept.deptno
    it shows the dept no 40 also
    DEPTNO SUM(SAL)
    10 8750
    20 10875
    30 9400
    40
    so to get this i posted query which was to long to understand easily.
    i accept that i sholud post short query to make other understand
    so check out this
    select STOCK_rEG.item_code AS ITEM_CODE,SUM(qty) as qty
    FROM
    SALES_DETAILR,SALES_HEADERR,STOCK_REG
    WHERE SALES_DETAILR.SR_ID = SALES_HEADERR.SR_ID AND
    SALES_DETAILR.STOCKCODE = STOCK_REG.STOCKCODE
    AND RETURN_DATE = '01-APR-08'
    GROUP BY STOCK_rEG.item_code
    ORDER BY TO_NUMBER(ITEM_CODE) ASC
    when i execute this query i get the following result
    ITEM_CODE QTY
    84 2
    102 3
    108 3
    116 6
    166 3
    170 3
    thoug there is other items are aalso available in stock which transaction
    yet not been done or not been sold out/
    so when i use the outer join i get the same result
    select STOCK_rEG.item_code AS ITEM_CODE,SUM(qty) as qty
    FROM
    SALES_DETAILR,SALES_HEADERR,STOCK_REG
    WHERE SALES_DETAILR.SR_ID = SALES_HEADERR.SR_ID AND
    SALES_DETAILR.STOCKCODE(+) = STOCK_REG.STOCKCODE
    AND RETURN_DATE = '01-APR-08'
    GROUP BY STOCK_rEG.item_code
    ORDER BY TO_NUMBER(ITEM_CODE) ASC
    ITEM_CODE QTY
    84 2
    102 3
    108 3
    116 6
    166 3
    170 3
    hope i tried to make u understand on better way

  • Need to create report query to get latest open and last closed period for given application

    Hi All,
    I need to create a report query to get below result displayed in report output.
    1)   -   Application name
    2)   -    Ledger name
    -o/  -Operating Unit
    3)   -  Last Closed Period
    4)   -  Current Open Period
    5)   -  Date Closed – Last Closed Period
    6)   -  Date Open – Current Open Period
    I tr I tried to create the query below is the same. Please let me know if it looks fine.
    SELECT *
      FROM (SELECT fav.application_name ,
                   hou.name Operating_Unit_Name,
                   gl.name Ledger_name,
                   gl.latest_opened_period_name,
                   gps.period_name Period_Name,
                   DECODE(gps.closing_status, 'O', 'Open', 'C', 'Closed') status,
                   gps.last_update_date Last_status_modified_date
              FROM gl_period_statuses gps,
                   gl_sets_of_books   gsob,
                   fnd_application_vl fav,
                   hr_operating_units hou,
                   gl_ledgers         gl
             WHERE gps.period_name = gps.period_name
               AND gps.closing_status ='C'
               AND fav.application_short_name =
                   NVL('&p_application_short_name', fav.application_short_name)
               AND gps.application_id = fav.application_id
               AND gsob.set_of_books_id = gps.set_of_books_id
               AND hou.set_of_books_id = gps.set_of_books_id
               AND gl.ledger_id = gsob.set_of_books_id
               AND hou.organization_id=NVL('&p_operating_unit',hou.organization_id)
               AND gl.ledger_id=NVL('&p_ledger_id',gl.ledger_id) 
             ORDER BY gps.last_update_date desc )WHERE ROWNUM = 1 
    UNION ALL
    SELECT *
      FROM (SELECT fav.application_name Application_Name,
                   hou.name Operating_Unit_Name,
                   gl.name Ledger_name,
                   gl.latest_opened_period_name,
                   gps.period_name Period_Name,
                   DECODE(gps.closing_status, 'O', 'Open', 'C', 'Closed') status,
                   gps.last_update_date Last_status_modified_date
              FROM gl_period_statuses gps,
                   gl_sets_of_books   gsob,
                   fnd_application_vl fav,
                   hr_operating_units hou,
                   gl_ledgers         gl
             WHERE gps.period_name = gps.period_name
               AND gps.closing_status = 'O'
               AND fav.application_short_name =
                   NVL('&p_application_short_name', fav.application_short_name)
               AND gps.application_id = fav.application_id
               AND gsob.set_of_books_id = gps.set_of_books_id
               AND hou.set_of_books_id = gps.set_of_books_id
               AND gl.ledger_id = gsob.set_of_books_id
               AND hou.organization_id=NVL('&p_operating_unit',hou.organization_id)
               AND gl.ledger_id=NVL('&p_ledger_id',gl.ledger_id) 
             ORDER BY gps.last_update_date desc)
             WHERE ROWNUM = 1

    It is within the table I believe (I'm not a DBA or a developer) since I created a BLOB column and then used the file browse feature to allow users to attach a resume to the table in order to be able to perform a search of the attached documents.
    I'm just having a hard time pointing the link in the search results report to the document in the blob column.
    The information on that page is great if you're trying to create a link to the document on the initial report.
    But I created a query using Oracle Text to run a report that does a boolean search of the attached word documents in the table.
    When it displays the search results, it doesn't create a link to the document and I can't figure out how to do it.
    Here's a link the the instructions I used to create the initial search report with Oracle Text, mind you I only created the index and query, I didn't add in all the link data since they're using documents on websites and I'm using documents in a table.
    http://www.oracle.com/technology/products/database/application_express/pdf/apex_text_application_v1.6.pdf
    If you can help me with this I'd really appreciate it.
    Thanks again.
    Greg
    Edited by: gjones77 on Dec 2, 2008 8:14 AM

  • Query to get the data of all the columns in a table except any one column

    Can anyone please tell how to write a query to get the data of all the columns in a table except one particular column..
    For Example:
    Let us consider the EMP table.,
    From this table except the column comm all the remaining columns of the table should be listed
    For this we can write a query like this..
    Select empno, ename, job, mgr, sal, hiredate, deptno from emp;
    Just to avoid only one column, I mentioned all the remaining ( 7 ) columns of the table in the query..
    As the EMP table consists only 8 columns, it doesn't seem much difficult to mention all the columns in the query,
    but if a table have 100 columns in the table, then do we have to mention all the columns in the query..?
    Is there any other way of writing the query to get the required result..?
    Thanks..

    Your best best it to just list all the columns. Any other method will just cause more headaches and complicated code.
    If you really need to list all the columns for a table because you don't want to type them, just use something like...
    SQL> ed
    Wrote file afiedt.buf
      1  select trim(',' from sys_connect_by_path(column_name,',')) as columns
      2  from (select column_name, row_number() over (order by column_id) as column_id
      3        from user_tab_cols
      4        where column_name not in ('COMM')
      5        and   table_name = 'EMP'
      6       )
      7  where connect_by_isleaf = 1
      8  connect by column_id = prior column_id + 1
      9* start with column_id = 1
    SQL> /
    COLUMNS
    EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,DEPTNO
    SQL>

  • Need help in SQL query to get only distinct records

    Hi all.
    I am new to oracle SQL and i want to write a query to get only distinct values from 2 tables.
    for example i hade 2 tables table1(employee_id,employee_no,grade) and another table2 with same structure.
    for example data in table1 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    1 101 D
    and data in table 2 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    i want to display only the fourth row like the data that doesnt exist in table2 i,e (1 101 D)
    I had too much records in both tables and i jst want to see the records that is in table 1 not in table 2 only.
    employeeid and employeeno are the same in both tables
    I hope u understand my query. I user left outer join and right outer join bt it displays all the results that and matching and unmatching. I want jst unmatched records.
    Any help is highly Appreciated.
    Thanx in Advance.
    I am using oracle 10G on windows

    Try MINUS:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries004.htm#i2054381
    SQL> -- generating sample data:
    SQL> with t1 as (
      2  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      3  select 1, 101, 'B' from dual union
      4  select 1, 101, 'E' from dual union
      5  select 1, 101, 'D' from dual
      6  )
      7  ,  t2 as (
      8  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      9  select 1, 101, 'B' from dual union
    10  select 1, 101, 'E' from dual
    11  )
    12  --
    13  -- actual query:
    14  --
    15  select employeeid
    16  ,      employeeno
    17  ,      grade
    18  from   t1
    19  minus
    20  select employeeid
    21  ,      employeeno
    22  ,      grade
    23  from   t2;
    EMPLOYEEID EMPLOYEENO G
             1        101 D
    1 row selected.

  • Passing Parameters to SQL Query and Displaying Result

    The user enters a serial number in a text box and then pushes a button to get info about the serial number. I do this with two regions. The first region has the text box and the button. The second region is a Report type region and contains the following SQL:
    select case
    when "WARRANTYSTATUS"."OUTOFWARRANTYDATE" >= sysdate then
    'Serial Number '||"WARRANTYSTATUS"."SERIALNUMBER"||' is in warranty.'
    else
    'Serial Number '||"WARRANTYSTATUS"."SERIALNUMBER"||' is out of warranty.'
    end as "The result is:"
    from "WARRANTYSTATUS"
    where "WARRANTYSTATUS"."SERIALNUMBER" in (:P1_SERIALNUMBER)
    For a single serial number of the form CU5-0799 every thing works. I am a little suprised this works because the SERIALNUMBER in the WARRANTYSTATUS table is of type varchar2 which means the serial number in the where clause must be surrounded by single quoties. Not sure where the single quoties are being added.
    However, what I like to do is to enter more that one serial number, seperated by commas, in the same text box, and then the query return a result for each serial number. What I would like to enter is:
    CU5-0799, CU5-07132, CU5-89345
    The problem is this string of three serial numbers will needs to be have single quotes around each serial number at the level of the sql statement. So the question is how do I make this work?? Thanks for the help in advance.

    Bob,
    If you would change your OTN handle to something more mnemonically friendly, that would help us. Thanks.
    This topic is addressed fully, with various techniques described, here: Re: Search on a typed in list of values .
    I am a little suprised this works because the SERIALNUMBER in the WARRANTYSTATUS table is of type varchar2 which means the serial number in the where clause must be surrounded by single quoties.
    Not true. Quotes are used for literals, not bind variables.
    Scott

  • Is it possible to submit a list item and at same time query/search the results if parameters are matched.

    Hello,
    Is it possible to submit a list item and at same time query/search the results if parameters are matched.
    Example - user logon to site enter search parameters and hit submit button. Once done parameters gets saved in list and shows search results on page. I have been asked to do this with
    SP designer and InfoPath doesn’t work due items limits.
    Please suggest.
    Thanks,
    Manish
    Manish

    Hi Manish,
    may i ask if you need,
    when user account click the login button, it will be authenticate the user and then it will show search result page?
    may i know how the keyword of words to be put? is it together with the user account box, password and keyword?
    or it will be like, after user authenticate, it will redirect to search page, so that user may use the search page to input the keyword?
    Regards,
    Aries
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

  • How to get result without single cotes in ''Cast(Multiset( '' Result.

    select cast(multiset(select column_name
    from user_tab_columns
    where table_name = 'DAILY_PRODN_MIS'
    and column_name like '%STOCK%'
    order by column_name) as tab_type) result from dual;
    RESULT
    TAB_TYPE('BAGS_STOCK', 'BLUE_DUST_STOCK', 'CEMENT_STOCK', 'CEMENT_STOCK_33', 'CEMENT_STOCK_43', 'CEMENT_STOCK_53', 'CK_ADJ', 'COAL_IND_D_STOCK', 'COAL_IND_D_STOCK_ADJ', 'COAL_IND_E_STOCK', 'COAL_IND_E_STOCK_ADJ', 'COAL_IND_F_STOCK','OCK_ADJ', 'MTD_COAL_IMP_D_STOCK_ADJ', 'MTD_COAL_IMP_E_STOCK_ADJ', 'MTD_COAL_IND_A_STOCK_ADJ', 'MTD_COAL_IND_B_STOCK_', 'YTD_COAL_IMP_B_STOCK_ADJ', 'YTD_COAL_IMP_C_STOCK_ADJ', 'YTD_COAL_IMP_D_STOCK_ADJ', 'YTD_COAL_IMP_E_STOCK_ADJ')
    How can i get result without single cotes for each column.

    Your query currently returns a collection type (tab_type) whereas it appears you want to return a delimited string.
    There are actually quite a few ways to achieve this - with your own function, with a user-defined aggregate functions (e.g. Tom Kyte's stragg), with the MODEL clause or with CONNECT BY, e.g.
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    SQL> SELECT table_name,
      2         SUBSTR (MAX (SYS_CONNECT_BY_PATH (
      3            column_name, ',')), 2) column_names
      4  FROM  (SELECT table_name, column_name,
      5                ROW_NUMBER () OVER (
      6                   PARTITION BY table_name
      7                   ORDER BY column_id) column_order
      8         FROM   user_tab_columns
      9         WHERE  table_name = 'BANK_ACCOUNT'
    10         AND    column_name LIKE '%U%') utc
    11  START WITH column_order = 1
    12  CONNECT BY column_order = PRIOR column_order + 1
    13  AND    table_name = PRIOR table_name
    14  GROUP BY   table_name;
    TABLE_NAME           COLUMN_NAMES
    BANK_ACCOUNT         ACCOUNT_NAME,ACCOUNT_NUMBER
    SQL>

  • How to get result of querry independet of a datatype?

    Hi All!
    I try to write a program, that calls stored procedures in a database independent of a name and arguments list. To get a result of a query I must call one of the functions getInt(), getString() etc. of CallableStatement. I'd like to write a wrap function, for instance getResult(), that get the result independent of the result's type. Has anybody any idea, how I can do it?
    Any help will be appreciate.
    Regards,
    Andrej.

    Hi Justin,
    Even we have been writing a kind of these generic methods to send a query to database, fetch the Resultset and then populate an ArrayList with the Resultset data.
    What do you get out of this ?
    -> Less code to maintain
    -> Single point of access to datasource
    -> JDBC Code reuse
    i agree that it might cost some cpu cycles in populating an ArrayList and then doing the casting again. But isn't it worth all the above credits.
    An example of such a method is
      public ArrayList execute( String query, ArrayList params )
                    throws SQLException {
        Connection con = null; // Database Connection object
        ArrayList rowset = new ArrayList();
        try {
          // Get the Database Connection
          con = connector.getConnection();
          // Use PreparedStatement object to execute the SQL Query
          PreparedStatement pst = con.prepareStatement( query );
          // Bind the columns values from the ArrayList object
          if( params != null ) {
            for( int i = 0; i < params.size(); i++ ) {
              pst.setObject( i + 1, params.get( i ) );
          // Execute the Query to get the ResultSet object
          ResultSet rst = pst.executeQuery();
          // Get column count from ResultSet Metadata
          int colCount = rsmtdt.getColumnCount();  // This may not be the correct syntax
          // Populate ArrayList
          while(rset.next()) {
            ArrayList row = new ArrayList();
         for(int i=1;i<=colCount;i++)
           row.add(rset.getObject(i);
         rowset.add(row);
          // Close the ResultSet and Statement
          rst.close();
          pst.close();
        } catch( SQLException ex ) {
          throw new DBException( "Unable to execute the SQL Query in execute "+
                                     "method of DBBroker class of given status " +
                                 " : " + ex.getMessage() );
        } catch( Exception ex ) {
          throw new DBException( "Exception thrown in execute() method of " +
                                 "DBBroker class of given status : " +
                                  ex.getMessage() );
        } finally {
          try {
            connector.releaseConnection( con ); // Releases the connection
          } catch( DBConException ex ) {
            throw new DBException( "Unable to release the connection of given " +
                                   "status : " + ex.getMessage() );
        return rowset;
      }Regards
    Elango.

  • Oracle query to get all occurences of a text in a string

    Hi
    Does anybody know how to write a query to get all occurences of a text in a string in different rows of the table
    For eg:
    I have a string <aa>bb</aa><aa>cc</aa><aa>ddd</aa>
    I have to find every occurence of <aa> and get the data between <aa> and </aa>
    So the output should be
    bb
    cc
    ddd
    I think this can be done by using a regular expression but I dont know how.
    Can anyone help me?
    Thanks in advance.

    user2360027 wrote:
    BluShadow,
    The query gives correct results if i only have <aa></aa>
    What if i have <ff></ff>?
    Example:
    <aa>bb</aa><aa>cc</aa><aa>ddd</aa><ff>dsd</ff>
    I want to have the following (dsd should not be there in the result)
    bb
    cc
    dddThis is a new requirement and not what you asked for in your original post. Perhaps you should tell us exactly what you are trying to achieve and what database version you are using to save us guessing.
    To crokitta,
    This version will deal with multiple rows..
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select '<aa>bb</aa><aa>cc</aa><aa>ddd</aa>' as x from dual union all
      2             select '<aa>xx</aa><aa>yy</aa>' from dual)
      3  -- end of test data
      4  select x, vals
      5  from (
      6    select x, regexp_replace(REGEXP_SUBSTR (x, '>[^<]+', 1, lvl),'[<>]') as vals
      7    from t
      8        ,(select level lvl from dual connect by level <= (select max(length(regexp_replace(x,'[^>]*'))/2) from t))
      9    )
    10  where vals is not null
    11* order by 1,2
    SQL> /
    X                                  VALS
    <aa>bb</aa><aa>cc</aa><aa>ddd</aa> bb
    <aa>bb</aa><aa>cc</aa><aa>ddd</aa> cc
    <aa>bb</aa><aa>cc</aa><aa>ddd</aa> ddd
    <aa>xx</aa><aa>yy</aa>             xx
    <aa>xx</aa><aa>yy</aa>             yy
    SQL>

  • Help in query to get the max(date)

    Hi I have query like below and based on the query I get the following results,
    SELECT a.UsageId,a.product,ProductDate
    FROM dbo.table1 a
    JOIN dbo.table2 b ON a.SID= b.SID
    JOIN dbo.table3 c ON b.CID = c.CID
    UsageId        Product          UsageDate
    1 Yellow 2014-01-01
    2 Yellow 2014-01-02
    3 Yellow 2014-01-03
    4 Yellow 2014-01-04
    5 Red 2014-01-01
    6 Red 2014-01-02
    7 Blue 2014-01-03
    8 Blue
    2014-01-04
    Now I want to add a new column which gives me the Max(UsageDate) of the column Prdouct last Usage.
    UsageId        Product          UsageDate          Max(UsageDate)
    1 Yellow
    2014-01-01          2014-01-04
    2 Yellow
    2014-01-02    2014-01-04
    3 Yellow
    2014-01-03          2014-01-04
    4 Yellow
    2014-01-04          2014-01-04
    5 Red 2014-01-01          2014-01-02 
    6 Red 2014-01-02          2014-01-02
    7 Blue
    2014-01-03          2014-01-04
    8 Blue
    2014-01-04          2014-01-04

    Simply use:
    SELECT a.UsageId,a.product,ProductDate, MAX(ProductDate) OVER (PARTITION BY a.ProductID) as [Latest Product Usage Date]
    FROM dbo.table1 a
    JOIN dbo.table2 b ON a.SID= b.SID
    JOIN dbo.table3 c ON b.CID = c.CID
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Query  for getting records  max  reported  timestamp and 2nd max report

    query for getting records in between
    max reported timestamp and 2nd max reported timestamp
    HERE IS ALL RESULT SET
    TIME DOMAIN
    30:jun:2006:20:08:45 TOMCAT
    30:jun:2006:20:08:45 TOMCAT
    30:jun:2006:20:07:04 TOMCAT
    30:jun:2006:20:07:04 TOMCAT
    30:jun:2006:20:07:24 TOMCAT
    30:jun:2006:20:07:24 TOMCAT
    30:jun:2006:20:07:45 TOMCAT
    30:jun:2006:20:07:45 TOMCAT
    30:jun:2006:20:08:05 TOMCAT
    30:jun:2006:20:07:04 TOMCAT
    30:jun:2006:20:08:05 TOMCAT
    PD_REPORTED_TIMESTAM PD_USER
    30:jun:2006:20:08:25 TOMCAT
    30:jun:2006:20:08:25 TOMCAT
    30:jun:2006:20:08:45 TOMCAT
    30:jun:2006:20:08:45 TOMCAT
    30:jun:2006:20:07:24 TOMCAT
    30:jun:2006:20:07:04 TOMCAT
    30:jun:2006:20:07:24 TOMCAT
    30:jun:2006:20:07:45 TOMCAT
    30:jun:2006:20:07:45 TOMCAT
    30:jun:2006:20:08:05 TOMCAT
    30:jun:2006:20:08:05 TOMCAT
    PD_REPORTED_TIMESTAM PD_USER
    30:jun:2006:20:08:25 TOMCAT
    30:jun:2006:20:08:25 TOMCAT
    QUERY RESULT TO COME
    TIME DOMAIN
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45
    Message was edited by:
    user517983

    Hi,
    can we write query like this.
    1 select pd_user,PD_REPORTED_TIMESTAMP
    2 from sp_process_detail_current spdc
    3 where host_id='DSCP02469'and pd_user='TOMCAT'
    4 and exists(
    5 select PD_REPORTED_TIMESTAMP from sp_process_detail_current
    6* having max(PD_REPORTED_TIMESTAMP)-spdc.PD_REPORTED_TIMESTAMP=0)
    SQL> /
    PD_USER PD_REPORTED_TIMESTAM
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45
    TOMCAT 30:jun:2006:20:08:45

Maybe you are looking for

  • Is there a way to find the email address of the user's from SCCM database.

    Hi All, I am using SCCM 2012 R2 and trying to find a query/report that can help in finding the email address of the user the machine is assigned to (Primary device). And if that is a machine which is being shared between different user, I need to gat

  • Hp Blue Screen sad face Unmountable Boot Volume and Automatic reoair loop error

    Someone Please help.I have an hp laptop that refuses to work.The screen stays on the automatic repair screen until it fades to black.I have tried to do a system recovery with f11, but all it does is bring up the blue screen with a sad face for a few

  • "...There is not enough memory available."

    I'm running the most recent version of iTunes. I've got quite a bit of purchased music. So, I'm coming home from college. Just before I leave, I download and install the newest update. I come home, fire up iTunes, and I am told I need to authorize. I

  • The cable and itunes is stuck on my iphone

    I was trying to update my Iphone 3 and now a picture of a cablee and itunes is stuck on my screen

  • Okay, who's the dummy here?

    Not sure where to post this because I'm not sure exactly where the problem is. I'm trying to open a file by clicking a file:// URL in a mail message. Mail responds with the message, "Mail was unable to open the URL 'file://Volumes/bla/bla/bla/spreads