Sql query efficiency problem.

Hello,
I have a table - Users: | id | name | manager_id |
The manager_id references the User.id,
I need to find an employee by an id or name and this employee has to be a manager to someone else.
Creating a sub-select that checks if employee's id is present within manager_id column takes a bit of time, is there a way to, for example, inner join the table to it self leaving only the rows that are managers.

Thank you both for your quick answers.
@ Rene Argento
The self-join is something I am interested in, but I don't exactly know how to write it so that it returns the same result set as the sub-select query you wrote.
@ Frank Kulash
Thanks for the query, but is there a possibility to re-write Rene's query to a JOIN query which excludes all the employees who are not managers to someone.
Or maybe there is another way to create the needed query which would be faster than using sub-select for each user ?
Thanks in advance.
Edited by: 909522 on 2012.22.1 10:09
Edited by: 909522 on 2012.22.1 10:10

Similar Messages

  • Sql query/function problem??

    Hi ,
    I have problems writing a difficult sql query, please help me
    I have a table t in which there are 50000 records
    the table has columns like
    create table t
    (MATCH_ID     NUMBER(4) NOT NULL,
    TEAM_ID NUMBER(4),
    PLAYER_ID     NUMBER(4),
    RUNS     NUMBER(3))
    here match_id, player_id and team_id are jointly primary key
    SQL> SELECT * FORM T WHERE MATCH_ID < 10
    MATCH_ID    TEAM_ID      PL_ID RUNS
            1          2       1228 8
            1          2       1203 82
            1          2       1316 24
            1          1       1150 27
            1          1       1278 13
            1          1       1243 60
            2          1       1278 37
            2          1       1291 0
            2          1       1243 53
            2          2       1228 25
            2          2       1285 103
            2          2       1316 60
            3          2       1228 8
            3          2       1285 25
            3          2        858 43
            3          1       1278 52
            3          1       1394 6
            3          1       1243 31
            4          1       1278 61
            4          1       1394 6
            4          1       1243 3
            4          2       1228 41
            4          2       1285 40
            4          2        858 5
            6          2       1228 20
            6          2       1285 100
            6          2       1408 0
            7          2       1228 15
            7          2       1285 34
            7          2       1408 44
            8          2       1228 0
            8          2       1420 31
            8          2       1340 66
            9          2       1420 19
            9          2       1385 28
            9          2       1340 0
    .....so on upto 50000 records..
    the problem is that I want to extract how many times each player_id in each
    match exists in the table, prior to that match_id (or current_match_id)
    along with that in another column, I also want the sum of 'RUNS' for each
    player_id  prior to that match_id (or current_match_id)
    my disired output is:
    MATCH_ID    TEAM_ID   player_ID RUNS   NO_OF_OCCURENCES    SUM(RUNS)
                                           BEFORE_THIS_MATCH   BEFORE_THIS_MATCH
                                           FOR_THIS_PLAYER_ID  FOR_THIS_PLAYER_ID
            1          2       1228 8      0                   0
            1          2       1203 82     0                   0
            1          2       1316 24     0                   0
            1          1       1150 27     0                   0
            1          1       1278 13     0                   0
            1          1       1243 60     0                   0
            2          1       1278 37     1                   13
            2          1       1291 0      0                   0
            2          1       1243 53     1                   60
            2          2       1228 25     1                   8
            2          2       1285 103    0                   0
            2          2       1316 60     1                   24
            3          2       1228 8      2                   33
            3          2       1285 25     1                   103
            3          2        858 43     0                   0
            3          1       1278 52     2                   50
            3          1       1394 6      0                   0
            3          1       1243 31     2                   113
            4          1       1278 61     3                   102
            4          1       1394 6      1                   6
            4          1       1243 3      3                   144
            4          2       1228 41     3                   41
            4          2       1285 40     2                   128
            4          2        858 5      1                   43
            6          2       1228 20     4                   82
            6          2       1285 100    3                   168
            6          2       1408 0      0                   0
            7          2       1228 15     5                   102
            7          2       1285 34     4                   268
            7          2       1408 44     1                   0
            8          2       1228 0      6                   117
            8          2       1420 31     0                   0
            8          2       1340 66     0                   0
            9          2       1420 19     1                   31
            9          2       1385 28     0                   0
            9          2       1340 0      1                   66
    as you can see from the above data (5TH COLUMN), i have mentioned the
    existance of each player_id in each match prior to the current_match_id
    since match_id = 1 is the 1st match in the table so no player_id comes in the
    table before match number 1.   
    In match number 2 , player_id = 1278 was also present in match_id = 1 so
    thats why Number_OF_OCCURENCES = 1 for  player_id = 1278 in match_id = 2
    and so on..
    same is the case with 'RUNS' column but here RUNS are the SUM of each
    player_id's 'RUNS' before the current match
    Note: if some player_id does not exist in the table before the current
    match_ID then the query should return zero for that player_id ( as in 4th and
    5th columns of no_of_occurances and sum(runs) respectively)
    for example: in above data
    MATCH_ID    TEAM_ID  PLayer_ID RUNS   NO_OF_OCCURENCES    SUM(RUNS)
                                          BEFORE_THIS_MATCH   BEFORE_THIS_MATCH
                                          FOR_THIS_PLAYER_ID  FOR_THIS_PLAYER_ID
           9          2       1385 28     0                   0
    I hope this will clear my problem
    i would be extremely grateful if someone helps me out??
    here is sample ddl of the above data
    create table t
    (MATCH_ID     NUMBER(4) NOT NULL,
    TEAM_ID         NUMBER(4),
    PLAYER_ID     NUMBER(4),
    RUNS     NUMBER(3))
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 2, 1228, 8);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 2, 1203, 82);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 2, 1316, 24);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 1, 1150, 27);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 1, 1278, 13);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (1, 1, 1243, 60);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 1, 1278, 37);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 1, 1291, 0);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 1, 1243, 53);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 2, 1228, 25);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 2, 1285, 103);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (2, 2, 1316, 60);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 2, 1228, 8);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 2, 1285, 25);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 2, 858, 43);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 1, 1278, 52);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 1, 1394, 6);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (3, 1, 1243, 31);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 1, 1278, 61);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 1, 1394, 6);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 1, 1243, 3);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 2, 1228, 41);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 2, 1285, 40);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (4, 2, 858, 5);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (6, 2, 1228, 20);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (6, 2, 1285, 100);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (6, 2, 1408, 0);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (7, 2, 1228, 15);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (7, 2, 1285, 34);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (7, 2, 1408, 44);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (8, 2, 1228, 0);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (8, 2, 1420, 31);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (8, 2, 1340, 66);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (9, 2, 1420, 19);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values  (9, 2, 1385, 28);
    insert into t (MATCH_ID, TEAM_ID, PLAYER_ID, RUNS) values (9, 2, 1340, 0);regards
    ramis.

    Ramis,
    SQL> select * from tt;
      MATCH_ID    TEAM_ID  PLAYER_ID       RUNS
             1          2       1228          8
             1          2       1203         82
             1          2       1316         24
             1          1       1150         27
             1          1       1278         13
             1          1       1243         60
             2          1       1278         37
             2          1       1291          0
             2          1       1243         53
             2          2       1228         25
             2          2       1285        103
             2          2       1316         60
             3          2       1228          8
             3          2       1285         25
             3          2        858         43
             3          1       1278         52
             3          1       1394          6
             3          1       1243         31
             4          1       1278         61
             4          1       1394          6
             4          1       1243          3
             4          2       1228         41
             4          2       1285         40
             4          2        858          5
             6          2       1228         20
             6          2       1285        100
             6          2       1408          0
             7          2       1228         15
             7          2       1285         34
             7          2       1408         44
             8          2       1228          0
             8          2       1420         31
             8          2       1340         66
             9          2       1420         19
             9          2       1385         28
             9          2       1340          0
    36 rows selected.
    SQL> select tt.match_id,
           tt.team_id,
           tt.player_id,
           tt.runs,
           nvl(a.sum_player,0) OCC,
           nvl(b.sum_runs,0) SUM_RUNS
    from
    tt,
    (select a.match_id,a.team_id,a.player_id,a.runs,count(*) sum_player
    from tt a,
          (select match_id,player_id
           from   tt
           group by match_id,player_id) b
    where a.match_id>b.match_id
    and   a.player_id=b.player_id
    group by a.match_id,a.team_id,a.player_id,a.runs
    ) a,
    (select a.match_id,a.team_id,a.player_id,a.runs,sum(b.runs) sum_runs
    from tt a,
          (select match_id,player_id,runs
           from   tt) b
    where a.match_id>b.match_id
    and   a.player_id=b.player_id
    group by a.match_id,a.team_id,a.player_id,a.runs
    ) b
    where tt.match_id=a.match_id(+)
    and tt.team_id=a.team_id(+)
    and tt.player_id=a.player_id(+)
    and tt.match_id=b.match_id(+)
    and tt.team_id=b.team_id(+)
    and tt.player_id=b.player_id(+)
      MATCH_ID    TEAM_ID  PLAYER_ID       RUNS        OCC   SUM_RUNS
             1          1       1150         27          0          0
             1          1       1243         60          0          0
             1          1       1278         13          0          0
             1          2       1203         82          0          0
             1          2       1228          8          0          0
             1          2       1316         24          0          0
             2          1       1243         53          1         60
             2          1       1278         37          1         13
             2          1       1291          0          0          0
             2          2       1228         25          1          8
             2          2       1285        103          0          0
             2          2       1316         60          1         24
             3          1       1243         31          2        113
             3          1       1278         52          2         50
             3          1       1394          6          0          0
             3          2        858         43          0          0
             3          2       1228          8          2         33
             3          2       1285         25          1        103
             4          1       1243          3          3        144
             4          1       1278         61          3        102
             4          1       1394          6          1          6
             4          2        858          5          1         43
             4          2       1228         41          3         41
             4          2       1285         40          2        128
             6          2       1228         20          4         82
             6          2       1285        100          3        168
             6          2       1408          0          0          0
             7          2       1228         15          5        102
             7          2       1285         34          4        268
             7          2       1408         44          1          0
             8          2       1228          0          6        117
             8          2       1340         66          0          0
             8          2       1420         31          0          0
             9          2       1340          0          1         66
             9          2       1385         28          0          0
             9          2       1420         19          1         31
    36 rows selected.
    SQL> Is this do you want ?
    Nicolas.

  • SQ01, SQ01 - Sql Query  Dislplay problem

    Hi
    Need help in SQL Query
    I generated one sql query which has the following output in general .
    Customer      name            description        amount
    asrq1           sharekhan      Amount payed           10
    asrq1           sharekhan      Amount Advance        20
    asrq1           sharekhan      Amount due           30
    but i need the output in the following way
    Customer      name            AMount payed     Amount Advance    Amount Due
    asrq1           sharekhan             10             20                        30
    and iam new this sql query but came to know we can write code ..but iam unable to initiliaze to write
    a peace of code as i dont know what are the select-options defined ..i saw in the include but didnt got it
    % comes prefix of select-options,and iam unable to get he internal table which is displayed in the query .
    can anyone help me in this answers will be awarded points.

    and transaction FBL5N does not work in your company because ... ???

  • SQL query / thread problem!

    I have the following code. when the execute mkethod is called, an instance of TransactionRunner is created. within the run method of TransactionRunner an SQLQuery is executed. I want to return the result set when the query has been executed, how can I do this, at the moment it tries to return the resultSet before the thread has finished. any ideas? I hav tried using a boolean as a flag but it doesn't appear to work.
    public static ResultSet execute(String SQL, boolean rs)
    System.out.println("in execute, dbm") ;
    SQLStatement = SQL ;
    result = rs ;
    complete = false ;
    tr = dbm.new TransactionRunner(SQL) ;
    //SOME FORM OF INDICATION TO THE END OF SQL EXECUTION
    return resultSet ;
    class TransactionRunner implements Runnable
         public ResultSet rs ;
         private Thread queryThread ;
         public String SQLStatement ;
         public TransactionRunner(String SQL)
              SQLStatement = SQL ;
              queryThread = new Thread(this) ;
              queryThread.start() ;
         public void run()
              if (result == true)
                   executeQuery(SQLStatement) ;
                   complete = true;
              else
                   update(SQLStatement) ;
              }

    I am up against the same problem, but I believe that
    you could fire off a progress monitor during the
    thread task and have the thread return the ResultSet.If you're planning to use Swing, then yes, you do need to execute the command in a spawned thread, otherwise your event thread will hang. BUT, spawning a sub-thread from a Swing app is not equivalent to spawning one from a simple console app, because the Swing event thread will keep your process alive. Instead, I would suggest doing something like the following:
    1) In the ActionListener that invokes the query (may be hung off a menu item, or a button, depending on your app):
            public void actionPerformed(ActionEvent actionEvent)
                (new Thread(new MyQuery())).start();
                displayWaitIndication();
            }This method runs in the event thread, and spawns a sub-thread to run the query. It also displays some sort of wait indication, maybe a progress box or a WAIT_CURSOR (I prefer the wait cursor, as you can't accurately display the progress of a server-side operation).
    2) MyQuery implements Runnable:
            public void run()
                List data = // call something to perform DB op here
                SwingUtilities.invokeLater(new Runnable()
                    public void run()
                        mainWindow.refresh(data);
            }This may be a bit confusing, as it leaves a lot of stuff out. I've used "List" as the return from the database function; chances are that you'll actually implement something like MyResultSet. You don't want to use an actual java.sql.ResultSet, because it (may) perform(s) a client-server call to retrieve each row. So, you process the real result-set into a container object (List or whatever) in the sub-thread.
    Then, you pass that container object back to the display window, on the event thread. I've used a variable to access the display window; you may do things differently. The key thing is the use of SwingUtilities.invokeLater(), to perform the actual update on the event thread.
    To summarize:
    1) You initiate the action on the event-thread.
    2) You perform the action in a sub-thread, put the resuls into a container.
    3) You update your display on the event-thread, using SwingUtilities.invokeLater().

  • SQL Query join problem

    Hi,
    I have two tables in my db, the patient table holds details of patients, the contact table holds details of contacts related to the patient.
    patient table
    patientid - primary key - int
    patient name - varchar
    contact table
    contactid - primary key - int
    patientid - foreign key from patient table - int
    incontact - foreign key from paient table to relate contacts - int
    My contact table has the following data:
    contactid patientid incontact
    2              19           21
    2               19          22
    I can retrieve data for the patients that have been in contact with patientid 19, by using the following join:
    SELECT * FROM PATIENT
    LEFT JOIN incontact
    ON incontact.incontact = patient.patientid
    WHERE incontact.patientid = 19
    However, can you please tell me how I can get data from the patients table where the incontact is 21?
    So I can get contacts from patients and patients from contacts?
    Thankyou

    Thanks very much for your replies again.
    Basically, I have a table of patients, and I would like to link them to each other, many to many.
    I populate the incontact table, which stores the original posted patientID, and the new generated patientID. I populate the DB with the code below.
    <cfquery name="update" datasource="staffdirectory">
    SET NOCOUNT ON
    INSERT INTO patient (title,firstname,surname,gender,DOB,telephone,address1,address2,address3,town,postcode
    VALUES ('#title#','#firstname#','#surname#','#gender#',#createodbcdate(createdate(startyear, startmonth, startday))#    ,'#telephone#','#address1#','#address2#','#address3#','#town#','#postcode#
    SELECT @@Identity AS newid   
    SET NOCOUNT OFF
    </cfquery>
          <cfquery name="insert" datasource="staffdirectory">
      INSERT INTO incontact (related,incontact)
    VALUES (#patientid#,#update.newid#)
    </cfquery>
    This works fine and I can get all related patients by usingthe following query:
    SELECT * FROM patient
    LEFT JOIN incontact
    ON patient.patientid  = incontact.related
    WHERE  incontact.incontact = #patientid#
    MY problem occurs when I want to find related patients from the related column, the reverse relationship. I use this seperate query:
    SELECT * FROM patient
    LEFT JOIN incontact
    ON patient.patientid  = incontact.incontact
    WHERE  incontact.related= #patientid#
    Is there a way I can combine both queries, or is there a better way I can achieve the same result?
    Many thanks again

  • Sql query results problem

    Hi All
    When i run the following query am getting the below results:
    SELECT STG_ITS_EMISSIONFACTOR.OPERATION,STG_ITS_EMISSIONFACTOR.SPEED,STG_ITS_EMISSIONFACTOR.VOC,STG_ITS_EMISSIONFACTOR.CO,STG_ITS_EMISSIONFA CTOR.NOX
    from STG_ITS_EMISSIONFACTOR
    results:
    OPERATION SPEED VOC CO NOX
    Operation Speed VOC CO Nox
    Indianapolis 0 4.301 24.651 2.048
    Indianapolis 1 4.301 24.651 2.048
    But when i use the following query am getting the below results :
    SELECT STG_ITS_EMISSIONFACTOR.OPERATION,STG_ITS_EMISSIONFACTOR.SPEED,STG_ITS_EMISSIONFACTOR.VOC,STG_ITS_EMISSIONFACTOR.CO,STG_ITS_EMISSIONFA CTOR.NOX,STG_ITS_SEGMENTCATEGORY_LUT.RTDIRNTYMEAS,STG_ITS_SEGMENTCATEGORY_LUT.SEGMENT,STG_ITS_SEGMENTMULTIPLIER.MULTIPLIER
    from STG_ITS_EMISSIONFACTOR,STG_ITS_SEGMENTCATEGORY_LUT,STG_ITS_SEGMENTMULTIPLIER
    am getting the results as :
    OPERATION SPEED VOC CO NOX RTDIRNTYMEAS SEGMENT MULTIPLIER
    Operation Speed VOC CO Nox 69 s m 15.7 23 4.69
    Operation Speed VOC CO Nox 465 e m 25.1 35 4.69
    Operation Speed VOC CO Nox 70 e m 84.7 47 4.69

    sorry the actual problem was
    Hi all
    When i use the UNION i'm getting strange results:
    SELECT OPERATION,SPEED,VOC,CO,NOX from STG_ITS_EMISSIONFACTOR
    union
    select RTDIRNTYMEAS,NULL,NULL,NULL,NULL from STG_ITS_SEGMENTCATEGORY_LUT
    RESULTS:
    OPERATION     SPEED     VOC     CO     NOX
    31 n m 126.3                    
    31 n m 127.3                    
    31 n m 131.1                    
    31 n m 132.3                    
    31 n m 134.7                    
    31 s m 125.5                    
    31 s m 129.5                    
    Indianapolis     40     0.63     8.935     1.191
    Indianapolis     43     0.6186     8.548     1.2162
    Indianapolis     44     0.6148     8.419     1.2246
    Indianapolis     47     0.6042     8.8396     1.2578
    Indianapolis     48     0.6008     9.1144     1.2702
    Indianapolis     49     0.5974     9.3892     1.2826
    Please any help is greatly appreciated.
    Thanks
    Edited by: thinkingeye on Apr 3, 2009 2:45 PM

  • SQL QUERY COUNT PROBLEM

    Employees:
    Employee_ID Name
    01 Hansen, Ola
    02 Svendson, Tove
    03 Svendson, Stephen
    04 Pettersen, Kari
    Orders:
    Prod_ID Product Employee_ID
    234 Printer 01
    657 Table 03
    865 Chair 03
    SELECT Employees.Name, Orders.Product
    FROM Employees, Orders
    WHERE Employees.Employee_ID=Orders.Employee_ID
    My question is :how to take the count of particular field from above query from the second table.

    Please don't re-post or cross-post.
    SELECT employee.name, COUNT(*)
    FROM employee, product
    WHERE employee.employee_id = product.employee_id
    GROUP BY employee.name
    - Saish

  • OBIEE SQL Query formate problem

    Hai All,
    i am tring to see viewlog in obiee it is givinn the following formate.
    WITH
    SAWITH0 AS (select sum(T338777.CST) as c1,
    sum(T338777.FINAL_INVOICE_AMT) as c2,
    T66199.X_DISP_FSCL_YEAR as c3,
    T66199.FSCL_MONTH as c4,
    substr(T66199.MONTH_NAME , 1, 3) as c5,
    T37817.NAME as c6,
    T101991.NAME as c7,
    T66199.FSCL_YEAR as c8,
    T101991.ROW_WID as c9
    from
    W_INT_ORG_D T37817,
    W_ORG_D T101991 /* Division (W_ORG_D) */ ,
    W_MONTH_D T66199 /* Generic Month (W_MONTH_D) */ ,
    WC_INVC_SPARES_PO_A T338777 /* Spare PO(WC_INVC_SPARES_PO_A) */
    where ( T37817.ROW_WID = T338777.ORG_WID and T66199.ROW_WID = T338777.MONTH_WID and T66199.X_DISP_FSCL_YEAR = '2010-11' and T101991.ROW_WID = T338777.DIVN_WID and T101991.DIVN_FLG = 'Y' )
    group by T37817.NAME, T66199.FSCL_MONTH, T66199.FSCL_YEAR, T66199.X_DISP_FSCL_YEAR, T101991.NAME, T101991.ROW_WID, substr(T66199.MONTH_NAME , 1, 3))
    I need the  following formate. what should i do?
    select sum(T338777.CST) as c1,
    sum(T338777.FINAL_INVOICE_AMT) as c2,
    T66199.X_DISP_FSCL_YEAR as c3,
    T66199.FSCL_MONTH as c4,
    substr(T66199.MONTH_NAME , 1, 3) as c5,
    T37817.NAME as c6,
    T101991.NAME as c7,
    T66199.FSCL_YEAR as c8,
    T101991.ROW_WID as c9
    from
    W_INT_ORG_D T37817,
    W_ORG_D T101991 /* Division (W_ORG_D) */ ,
    W_MONTH_D T66199 /* Generic Month (W_MONTH_D) */ ,
    WC_INVC_SPARES_PO_A T338777 /* Spare PO(WC_INVC_SPARES_PO_A) */
    where ( T37817.ROW_WID = T338777.ORG_WID and T66199.ROW_WID = T338777.MONTH_WID and T66199.X_DISP_FSCL_YEAR = '2010-11' and T101991.ROW_WID = T338777.DIVN_WID and T101991.DIVN_FLG = 'Y' )
    group by T37817.NAME, T66199.FSCL_MONTH, T66199.FSCL_YEAR, T66199.X_DISP_FSCL_YEAR, T101991.NAME, T101991.ROW_WID, substr(T66199.MONTH_NAME , 1, 3))
    Regards,
    Ram

    Hi,
    You messed up with database features in OBIEE rpd. Check the below link
    http://gerardnico.com/wiki/dat/obiee/vertical_fragmentation_sql
    Regards,
    Sandeep

  • SQL Query efficiency

    Can anyone tell me , is there any difference between count(*) and count(1)
    I believe count(1) is faster than count(*), as it is taking count for 1st column.
    Is there any other difference or my belief is not right.
    Can anyone plz explain it with example....

    1) There is no difference in performance for any vaguely recent version of Oracle.
    2) If there ever is a difference, COUNT(*) would be the form that would be optimized. So if there ever is a difference COUNT(*) will be faster.
    3) COUNT(1) does not count the first column. It counts the literal number 1 for every row. You'd get the same behavior if you did COUNT('BadgerBadgerBadger') or COUNT( date '2012-01-15' ).
    4) If I see code that has a bunch of COUNT(1)'s, I generally assume that whoever wrote it is prone to believing random myths they've found on the internet rather than testing thing for themselves so I assume that the surrounding code is more likely to have bugs. Particularly if I'm doing a code review.
    Justin

  • Reagrding some typical sql query in oracle8i

    hi all,
    i am facing a problem in sql query,My problem is like that
    table emp data like that :
    emp_id sal
    10 1000
    20 2000
    30 3000
    and i want to show data as follows-
    emp_id sal
    10 1000
    20 3000
    30 6000
    just show the cumulative total of salary column.
    plz its urgent.
    ok
    bye

    SQL> create table t (emp_id number
      2  ,sal number
      3  );
    Table created.
    SQL> insert into t values (10, 1000);
    1 row created.
    SQL> insert into t values (20, 2000);
    1 row created.
    SQL> insert into t values (30, 3000);
    1 row created.
    SQL>
    SQL>
    SQL> select emp_id
      2       , sal
      3       , Sum (sal) over (order by emp_id) sum_sal
      4    from t
      5  /
        EMP_ID        SAL    SUM_SAL
            10       1000       1000
            20       2000       3000
            30       3000       6000

  • Problem in making SQL Query

    Hello All,
    I have one problem regarding sql query.
    I have one internal table which contains equnr and bis as fields. There are two database tables egerr and eastl. The structure for tables are as follows:
    Fields for egerr:
    equnr, bis, logiknr in which first two fields form key.
    Field for eastl:
    anlage, bis, logiknr in which all fields form primary key.
    I want to select records from internal table which does not have record in eastl.
    For the reference we can extract logiknr from egerr by using intarnal table and then use this logiknr to check entry in table eastl. but i want those equnr which are in internal table but not mapped in eastl.
    I want the most efficient solution for this as there are many records.
    Thanks..... and if you have any queries then let me know.
    Jignesh.

    hi,
    as per ur statement, u want the field equnr which exists in the internal table but not in eastl. now for comparing with eastl u will need to check for all the three fields as they form the key...
    get data from egerr for matching equnr and bis in your internal table
    i.e. assuming ur table is itab  and itab_logiknr contains a single field logiknr
    select logiknr from egerr
      into table itab_logiknr
      for all entries in itab
      where equnr eq itab-equnr
        and bis eq itab-bis.
    now from this data (itab_egerr), compare the data with that in eastl for matching (or non matching) values of logiknr
    assuming data from eastl lies in itab_eastl
    select anlage bis logiknr from eastl into itab_eastl
    for all entries in itab_logiknr
    where logiknr eq itab_logiknr-logiknr.
    for non matching entries u can select data from eastl which is not present in itab_eastl now....
    (but mind you....since all fields of eastl form the key, u might not be getting the correct data) so if possible study ur scenario again and see if u can search the eastl table comparing all fields in the primary key)
    try this....get back in case of any clarifications
    hope it gives u some pointers...
    regards,
    PJ

  • Problem with SQL query region source containing OLAP clauses

    Hi team,
    I believe I found a bug when HTMLDB validates the SQL Query in a report region.
    My query includes an ORDER BY clause within a windowing function and HTMLDB refuses to accept the source owing to the presence of the ORDER BY and the column heading sort preference.
    Clearly the order-by in a window function has little to do with the column heading sort, but this error prevents me from updating the conditional display item in the page definition.
    Note also that the page was imported smoothly from the 1.5 version, so probably the region source is not checked at that time, but only when you update it "manually".
    So, in the end, if I don't change anything the page works because the sql query is assumed to be correct but unfortunately I need to change the condition and I cannot.
    The problem shows up in page 126 of app 21670, SQL query region.
    Bye,
    Flavio
    PS: may be I can work around this by using the pl/sql function returning the sql query.

    Flavio,
    We're aware of this problem. For now, your workaround is described in this thread:
    HTMLDB 1.6 and "order by" in analytic functions
    Sergio

  • Problem writing a sql query for a select list based on a static LOV

    Hi,
    I have the following table...
    VALIDATIONS
    ID          Number     (PK)
    APP_ID          Number     
    REQUESTED     Date          
    APPROVED     Date          
    VALID_TIL     Date
    DEPT_ID          Number     (FK)
    I have a search form with the following field item variables...
    P11_DEPT_ID (select list based on dynamic LOV from depts table)
    P11_VALID (select list based on static Yes/No LOV)
    A report on the columns of the Validations table is shown based on the values in the search form. So far, my sql query for the report is...
    SELECT v.APP_ID,
    v.REQUESTED,
    v.APPROVED,
    v.VALID_TIL,
    d.DEPT
    FROM DEPTS d, VALIDATIONS v
    WHERE d.DEPT_ID = v.DEPT_ID(+)
    AND (d.DEPT_ID = :P11_DEPT_ID OR :P11_DEPT_ID = -1)
    This query works so far. My problem is that I don't know how to do a search based on the P11_VALID item - if 'yes' is selected, then the VALID_TIL date is still valid. If 'no' is selected then the VALID_TIL date has passed.
    Can anyone help me to extend my query to include this situation?
    Thanks.

    Hello !
    Let's have a look at my example:create table test
    id        number
    ,valid_til date
    insert into test values( 1, sysdate-3 );
    insert into test values( 2, sysdate-2 );
    insert into test values( 3, sysdate-1 );
    insert into test values( 4, sysdate );
    insert into test values( 5, sysdate+1 );
    insert into test values( 6, sysdate+2 );
    commit;
    select * from test;
    def til=yes
    select *
      from test
      where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
           =decode('&til','yes',1,-1);
    def til=no
    select *                                                                               
      from test                                                                            
      where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
           =decode('&til','yes',1,-1);  
    drop table test;  It's working fine, I've tested it.
    The above changes to my first idea I did because of time portion of the DATE datatype in Oracle and therefore the wrong result for today.
    For understandings:
    1.) TRUNC removes the time part of DATE
    2.) The difference of to date-values is the number of days between.
    3.) SIGN is the mathematical function and gives -1,0 or +1 according to an negative, zero or positiv argument.
    4.) DECODE is like an IF.
    Inspect your LOV for the returning values. According to my example they shoul be 'yes' and 'no'. If your values are different, you may have to modify the DECODE.
    Good luck,
    Heinz

  • Problem using alias field names in a sql query

    Hello,
    I have a question regarding a simple Oracle database SQL query writeup:
    In the following (badly written but properly working) SQL query:
    Query 1:
    select
    soe.field1,
    (soe.field2 + soe.field3) as field4,
    (soe.field5 - (soe.field2 + soe.field3)) as field6,
    (select comp.parValue*soe.field7
    from
    CompTable comp) as parValue,
    (select soe.field8 - (comp.parValue*soe.field7+ soe.field9)
    from
    CompTable comp) as field10
    from
    SomeTable soe
    PROBLEM 1:
    I am re writing the code (soe.field2 + soe.field3) to get the alias field4 or column name field4 that I have created on the fly in the previously for use with the following fields. Can't I rewrite the query as follows. There is something simple missing!
    Query 2:
    select
    soe.field1,
    (soe.field2 + soe.field3) as field4,
    soe.field5 - field4 as field6, <<< field4 does not work here
    (select
    comp.parValue*soe.field7
    from
    CompTable comp) as parValue,
    (select
    soe.field8 - (comp.parValue*soe.field7+ soe.field9)
    from
    CompTable comp) as field10
    from
    SomeTable soe
    PROBLEM 2:
    Similar to the above problem, I was thinking to get a field parValue out of the CompTable table and re-use many times rather than the code shown in Query 1:
    Query 3:
    select
    soe.field1,
    (soe.field2 + soe.field3) as field4,
    soe.field5 - field4 as field6,
    soe.field7* (select comp.parValue from CompTable comp) as parValue1,
    soe.field8 - (parValue1*soe.field7+ soe.field9) as field10      <<<< parvalue1 does not work here
    parValue1*soe.field9 as TaxCondition               <<<< parvalue1 does not work here
    from
    SomeTable soe
    See that the query becomes so simple, but the above query does not work. There is something fundamentally wrong in my usage of the alias field names in creating other fields. The Query1 seems to be the only working option but its very slow as I am redoing and re-writing the whole code again and again to get the parValue field out of the CompTable table for use to create many other fields.
    I will appreciate if you can guide me in the right direction on this issue.
    Thanks and Regards
    Rama

    SELECT tmp.contract_no, tmp.Actual, tmp.Actual - tmp.NbHours
    FROM ( SELECT t.contract_no, sum(l.hrs) AS Actual, (c.labour_hours * c.labour_progress_per) / 100 AS NbHours
    FROM TASK_DELEGATION t
    INNER JOIN COST_CODE c
    ON t.cost_code = c.cost_code AND t.contract_no = c.contract_no AND t.is_inactive=0
    INNER JOIN Labour.dbo.LABOURALLOT l
    ON l.contractNo = c.contract_no AND l.costcode = c.cost_code AND l.pm = 'N'
    GROUP BY t.contract_no, c.labour_hours, c.labour_progress_per
    ) tmp

  • SQL query problem - select max (case... aggregate function)

    Hi,
    I have a problem with below sql query, it gives me problem/error message 'ORA-00937: not a single-group group function', why?
    select sag.afdeling, sag.sagsnr, to_char(sag.start_dato, 'yyyy-mm-dd'), sag.stat, BOGF_TRANS.TRANSTYPE,
    max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
    max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
    max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
    from sag
    join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
    where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
    group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
    If I exclude (columns) as below it give me correct summations (max (case... sum(...)) but then I miss some important info that I need
    select
    max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
    max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
    max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
    from sag
    join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
    where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
    group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
    Any ideas?

    Moved to more sutable forum, sorry.

Maybe you are looking for