Selecting and joining differnent tables based on columns populated

I have a table for audit purpose
Table paudit
pauditid (NOT NULL)
operationid (NOT NULL)               
typeid     (NOT NULL) -- Electronics, furniture, entertainment, department     
operatorid (NOT NULL)
electronicsid
furnitureid
entertainmentid                    
departmentid               
location (NOT NULL)
changeto
changefrom
memberelectronicsid               
memberfurnitureid               
memberentertainmentid          
memberdepartmentname     
paudittime     
deptfrom          
deptto
operatorname     name     type          operation     location     deptfrom     deptto     membername
operatorname1     name1     Electronics     install          abcbcbcb
          name2     furniture     uninstall     nnddnd
          name3     furniture     move          jdjdj          abc          def
               entertainment     product_add     gjhg                         adfgfgffdg
               entertainment     product_del     gfhfghj                         dgfdfgdfgf
Operatorname2As you can see, based on the typeid and operationid differnt columns get populated. But the proble is that is all these columnid's have to be joined to the resipective tables to get the names and the result will have be all consolidated into a single column. Like the column "name", in the sample above, to get the name, based on the type, would have to be joined with 4 differnt tables to get the "name". Based on the "operation" 3 diffent tables would have to joined to get the "membername". Hope this is clear.
I was thinking about using "With as" and "parition by". But I got confused. So I would like some one to help me out if they have done something similar or point me in the right direction.
Also these reports could be run for long periods of time or often and hence performance is also important.

Well one of the main reason this was done was to enforce foreign keys when it comes to using ids and referring from other tables and the columns were split based on the produt type.
pauditid     operationid     operatorid     electronicsid     furnitureid     entertainmentid     departmentid     location     changeto     changefrom     memberelectronicsid     memberfurnitureid     memberentertainmentid     memberdepartmentname     paudittime     deptfrom     deptto     typeid
41     2     3     74                    UYWZrKifzeVVGJzhocmh                                                  1
42     3     3          66               ngqmfBaagQyLMTXNjjju                                                  2
43     2     13               9          NnLBvOJjZAPJFtkJLQcg                                                  3
44     4     13                    16     NRYMhvySvbrsiuLKAgfH                                                  4
45     2     7     10                    hZAMQcoGeeMXjrNcfBsH                                                  1
46     4     50          11               vKswkxnfNnRkcKkFxanP                                                  2
47     3     13               4          opBYnyQLArRwxOQTURww                                                  3
48     1     22                    14     SaGoHjQGtlMQjjOzfFEO                                                  4
49     4     41     53                    DaPXkQVAZOvqgWPXFlEi                                                  1
108     1     13                    14     OfYVYvhBJpwExyfrIDux                                                  4
109     4     6     81                    GODUpaejHEdpcOGTYGsF     iSHTF     aJtmf                                        1
110     2     21          88               cpOVLkRuZtaMlLgKmCXz     MIMoN     NcFwD                                        2
111     1     32               36          DyaqlSglgvDTzQMMQmsa     gipDq     bIoTb                                        3
112     4     47                    48     ncwzqYjSCgIcBaeOxXkk     XVuZi     fEuYj                                        4
113     1     33     62                    PzHlvjpYjJkPhFYfZDzN     ryVIB     tPHoA                                        1
114     4     46          16               jnVNDWHuUnDAyOFItlaD     EoRsW     tkkYU                                        2
115     4     43               45          lvJvmTYmQrwkNJnBirEX     MAFgG     xbqym                                        3
116     4     25                    98     JtBYgfCSAoBafXbcovhr     yDstl     llGPL                                        4
186     7     3               2          kvymkGFDPsPcqtRqmdFF                    32                              3
187     8     18               6          YcwhKhgRdMlUspaLlGWj                         50                         3
188     7     19               8          EGtVVpNJYuUunmUsEBxj                              jrCYkboqxXadjqDgWuaB                    3
189     7     4               8          gcuEqHTTJspnohiLSNom               15                                   3
190     8     12               13          KgvuLHNQLmHOruIzpNOR                    33                              3
191     7     8               24          jjNJseqqIqzCPgkUfxBg                         55                         3
192     8     6               5          bDEYJnZtQJEDlDHUcGQG                              bsTVKQxsQaGKmShBRPkZ                    3
193     8     3               4          AkroIDSnEkfdlTCRZOfE               7                                   3
241     9     8     19                    vbrGYWcdrTbCQdtGAVlZ                                        15     6     1
242     9     7          13               AoKZgAtBkkvXqFwcpZQl                                        25     10     2
243     9     16               14          CEKuwaMwPCfEXWecDXaG                                        17     20     3
244     9     17                    20     uCLddsBDtVTdbGxAmbcN                                        14     14     4
245     9     3     22                    wrsiJORxHWXaTNzIXziy                                        14     14     1
246     9     14          20               zCULRoirtEMnHSAArrEu                                        10     24     2
247     9     5               6          bKLEEijvtNuaCApBajNa                                        16     1     3
                                                                      

Similar Messages

  • Inserting rows into table Based on Column Values.

    Hi,
    I am trying to inserting rows into a table based on Column.
    Id      Name        
    Data
    N 105.1.1
    http://www.example.com/New/105.1.1
    U 105.1.2               http://www.example.com/Used/105.1.2
    S 105.1.3               http://www.example.com/Sold/105.1.3
    I want a table like this. 
    I want to insert Data column value based on Id and Name Columns
    If Id = N and Name = 105.1.1 then Data value should be insert as   http://www.example.com/New/105.1.1
    If Id = U and Name = 105.1.2 then Data value should be  insert as  http://www.example.com/Used/105.1.2
    If Id = S and Name = 105.1.3 then Data value should be insert as   http://www.example.com/Sold/105.1.3
    Can you please help us to write query to insert above Data Column values based on Id and Name Columns.

    INSERT INTO tbl (col) SELECT
    CASE WHEN id='N' and Name='105.1.1 ' THEN 'http://www.example.com/New/105.1.1'
              WHEN id='U' and Name='105.1.2 ' THEN 'http://www.example.com/New/105.1.2'
    END col
    FROM anothertbl
    Sorry , cannot test it right now.
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Join Two tables based on TO_DATE,TRIM,UPPER Functions in RPD

    Hi All,
    I have a requirement where I have to join 2 tables based on the functions TO_DATE,TRIM,UPPER.
    I have Date Of Birth, Person Name . So I have to use these functions and then join in the RPD.
    Is it possible to achieve. I am stuck in this problem.
    Regards,
    Krishna

    Hi Dann,
    Thanks for your reply. I had tried adding these functions but I get the error 'Only columns,designated predicates and operators are allowed". Niether in the Business not in the Physical layer we are allowed
    to enter any functions in the join conditions between the two tables.
    I am not very sure whether this is the normal behaviour or I can avoid this by doing something.
    Thanks,
    Krishna

  • Select and join one month record

    I receive a request from my customer . He want generate a total sales record for a month and those data stored in daily sales table and customer table.
    tblcustomer-20140101
    tblcustomer-20140102
    tblcustomer-20140103
    tblcustomer-20141231
    tblsales-20140101
    tblsales-20140102
    tblsales-20140103
    tblsales-20141231
    is there anyway when user select month name from application(ex: April), it will choose all april record and join it together?

    Hi kjleong,
    Generally to say, it is not a good practice to store the same entity data individually for each day, there are many shortages, such as the problem you posted in this thread. You can put all of them into one table and tag them with a DATE column. Anyway
    in this case, to achieve your requirement, you may refer the below stored procedure(SP).
    CREATE PROC ProcGetSales
    @Year VARCHAR(20),
    @Month VARCHAR(20)
    AS
    DECLARE @Date DATE;
    DECLARE @Days INT;
    DECLARE @SqlStr NVARCHAR(MAX);
    SET @Date = @Year+'-'+@Month+'-01';
    SET @Days = DATEDIFF(DAY,@Date,DATEADD(MM,1,@Date)); --get the days of the specific month
    create a temp table
    CREATE TABLE #Temp
    col1 int --you should put the columns after join in your real envrioment here
    table join statement, put the join result into a temp table
    I dont know the columns so I use * here and the join condition is just based on assumption
    you should modify the statement basing on your real environment
    SET @SqlStr= 'INSERT INTO #Temp SELECT * FROM [tblcustomer-WhichDay] tc JOIN [tblsales-WhichDay] ts ON tc.customerID = ts.customerID';
    DECLARE @Counter INT = 1;
    WHILE @Counter <= @Days --loop the tables named after the specific month
    BEGIN
    DECLARE @Sql NVARCHAR(MAX) = REPLACE(@SqlStr,'WhichDay',CONVERT(VARCHAR(8),CAST(@Year+'-'+@Month+'-'+LTRIM(STR(@Counter)) AS DATE),112));
    EXEC sp_executesql @Sql;
    SET @Counter = @Counter+1;
    END
    SELECT * FROM #TEMP
    The SP is not ready to use, you have to make some modification basing on your real environment before it works.
    If you have any question, feel free to let me know.
    Best regards,
    Eric Zhang
    If you have any feedback on our support, please click
    here.

  • Select and join

    Hi
    I want to know when is good to do join between 2 tables
    or to do select and loop and in the loop select single
    When it good to use for all entries
    Thanks
    have a nice day

    Hi
    JOIN is faster to fetch the data from database tables provided they have some connecting fields in both tables
    for all entries
    The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:
    SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ...
    <cond> may be formulated as described above. If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.
    The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared. Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.
    You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
    Select single and Upto 1 rows
    Difference Between Select Single and Select UpTo One Rows
    According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
    select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
    The best way to find out is through sql trace or runtime analysis.
    Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
    The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
    The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
    Mainly: to read data from
    The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
    Mainly: to check if entries exist.
    Reward points for useful Answers
    Regards
    Anji

  • Using functions in select statement(joining 5 tables) taking long time in Oracle

    Hi,
    I have created a query in oracle which joins 5 tables and uses two functions(function names are 'ca_concat' and 'ca_concat_noseq').
    Query takes approximately 40 secs to execute around 12000 records. If I remove the functions from query it excutes within a second..
    Note : I have used the oracle SQL Developer for testing the query.
    It would be appriciated if anybody helps me to improve the perfomance of the query.
    Below are the querie with and without functions:
    1. Query with functions:
    select
    imsAuditEvent12.id as ID,
    imsAuditEvent12.audit_time as AUDIT_TIME,
    imsAuditEvent12.admin_dn as ADMIN_DN,
    imsAuditEvent12.admin_name as ADMIN_NAME,
    imsAuditEvent12.event_name as EVENT_NAME,
    imsAuditEvent12.event_description as EVENT_DESCRIPTION,
    imsAuditEvent12.event_state as EVENT_STATE,
    imsAuditEvent12.envname as ENVNAME,
    imsAuditTaskSession12.task_name as TASK_NAME,
    imsAuditTaskSession12.id as TASK_ID,
    imsAuditTaskSession12.task_description as TASK_DESCRIPTION,
    imsAuditTaskSession12.task_priority as TASK_PRIORITY,
    S1.OBJECT_ID,
    S1.OBJECT_NAME as OBJECT_NAME,
    S1.OBJECT_TYPE as OBJECT_TYPE,
    S2.ATTRIBUTE_NAME as ATTRIBUTE_NAME,
    S2.ATTRIBUTE_OLDVALUES as ATTRIBUTE_OLDVALUES,
    S2.ATTRIBUTE_NEWVALUES as ATTRIBUTE_NEWVALUES,
    S3.OBJECT_DN as OBJECT_DN,
    S3.OBJECT_TYPE as IMSOBJECT_TYPE,
    S3.CONTAINER_NAME as CONTAINER_NAME,
    S3.CONTAINER_DN as CONTAINER_DN,
    S3.CONTAINER_TYPE as CONTAINER_TYPE
    from
    imsAuditEvent12 LEFT JOIN imsAuditTaskSession12 ON imsAuditTaskSession12.id=imsAuditEvent12.tasksession_id LEFT JOIN
    (select parent_event_id,
    ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_name','imsAuditEventObject12') as OBJECT_NAME,
    ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_type','imsAuditEventObject12') as OBJECT_TYPE,
    ca_concat_noseq('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.ID','imsAuditEventObject12') as OBJECT_ID
    from
    imsAuditEventObject12 group by parent_event_id) S1
    ON imsAuditEvent12.id = S1.parent_event_id LEFT JOIN
    (select
    parent_object_id,
    ca_concat('parent_object_id',parent_object_id,'attribute_name','imsauditobjectattributes12') as ATTRIBUTE_NAME,
    ca_concat('parent_object_id',parent_object_id,'attribute_oldvalue','imsauditobjectattributes12') as ATTRIBUTE_OLDVALUES ,
    ca_concat('parent_object_id',parent_object_id,'attribute_newvalue','imsauditobjectattributes12') as ATTRIBUTE_NEWVALUES
    from
    imsauditobjectattributes12 group by parent_object_id) S2
    ON S1.OBJECT_ID = S2.parent_object_id LEFT JOIN
    (select
    parent_event_id,
    ca_concat('parent_event_id',parent_event_id,'OBJECT_DN','imsauditobjectrelationship12') as OBJECT_DN,
    ca_concat('parent_event_id',parent_event_id,'OBJECT_TYPE','imsauditobjectrelationship12') as OBJECT_TYPE ,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_NAME','imsauditobjectrelationship12') as CONTAINER_NAME,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_DN','imsauditobjectrelationship12') as CONTAINER_DN,
    ca_concat('parent_event_id',parent_event_id,'CONTAINER_TYPE','imsauditobjectrelationship12') as CONTAINER_TYPE
    from
    imsauditobjectrelationship12 group by parent_event_id) S3
    ON imsAuditEvent12.id =S3.parent_event_id where imsauditevent12.id > 0 and imsauditevent12.id <12000 order by imsauditevent12.id ASC;
    2. Query without using functions:
    select * from imsauditeventobject12 left join imsauditevent12 on imsauditeventobject12.id=imsauditevent12.id left join imsauditobjectattributes12 on imsauditeventobject12.id=imsauditobjectattributes12.parent_object_id left join imsaudittasksession12 on imsauditevent12.tasksession_id=imsaudittasksession12.id left join imsAuditObjectRelationship12 on imsAuditEvent12.id =imsAuditObjectRelationship12.parent_event_id where imsauditevent12.id >0 and imsauditevent12.id < 12000 order by imsauditevent12.id asc;
    Thanks,
    Badri

    Hi,
    Please find the below more information about the query.
    DB version: Oracle 11g Enterprise Edition Release 11.2.0.1.0
    Below are source of the functions:
    create or replace function ca_concat( ca_key_name in varchar2,
                           ca_key_val  in varchar2,
                           ca_other_col_name in varchar2,
                           ca_tname     in varchar2 )
       return varchar2
          as
           type rc is ref cursor;
           l_str    varchar2(32000);
           l_sep    varchar2(1);
           l_val    varchar2(32000);
           l_count   number(6);
           l_cur    rc;
       begin
       l_count :=1;
       l_str := '';
           open l_cur for 'select '|| ca_other_col_name ||'
                             from '|| ca_tname || '
                            where ' || ca_key_name || ' = '
                       using ca_key_val;
           loop
               fetch l_cur into l_val;
               l_val := SUBSTR(l_val,0,102);
               exit when (l_cur%notfound or l_count > 38);
               l_str := l_str || l_sep || l_count || '.' || l_val;
               l_sep := ',';
           l_count := l_count + 1;
           end loop;
           close l_cur;
           return l_str;
       end;
      create or replace function ca_concat_noseq( ca_key_name in varchar2,
                           ca_key_val  in varchar2,
                           ca_other_col_name in varchar2,
                           ca_tname     in varchar2 )
       return varchar2
          as
           type rc is ref cursor;
           l_str    nvarchar2(32000);
           l_sep    varchar2(1);
           l_val    varchar2(32000);
           l_count   number(6);
           l_cur    rc;
       begin
       l_count :=1;
           open l_cur for 'select '||ca_other_col_name||'
                             from '|| ca_tname || '
                            where ' || ca_key_name || ' = '
                       using ca_key_val;
           loop
               fetch l_cur into l_val;
               exit when (l_cur%notfound or length(l_val)>3000 or l_count>1);
               l_str := l_str || l_sep || l_val ;
               l_sep := ',';
           l_count := l_count + 1;
           end loop;
           close l_cur;
           return l_str;
    end;
    Below are the tables structures:
    DESC imsauditevent12;
    Name                           Null     Type                                                                                                                                                                                      
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    TASKSESSION_ID                 NOT NULL NUMBER                                                                                                                                                                                       
    TASKSESSION_OID                         VARCHAR2(100)                                                                                                                                                                                
    PARENT_EVENT_OID                        VARCHAR2(100)                                                                                                                                                                                
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    EVENT_OID                      NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ADMIN_DN                       NOT NULL VARCHAR2(512)                                                                                                                                                                                
    ADMIN_NAME                              VARCHAR2(255)                                                                                                                                                                                
    EVENT_NAME                     NOT NULL VARCHAR2(255)                                                                                                                                                                                
    EVENT_DESCRIPTION                       VARCHAR2(4000)                                                                                                                                                                               
    EVENT_STATE                             VARCHAR2(100)                                                                                                                                                                                
    ENVNAME                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENV_OID                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    DESC imsauditeventobject12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_EVENT_ID                NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    OBJECT_TYPE                    NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_NAME                             VARCHAR2(255)                                                                                                                                                                                
    DESC imsauditobjectattributes12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_OBJECT_ID               NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    DISPLAY_NAME                            VARCHAR2(255)                                                                                                                                                                                
    ATTRIBUTE_NAME                 NOT NULL VARCHAR2(255)                                                                                                                                                                                
    ATTRIBUTE_OLDVALUE                      VARCHAR2(4000)                                                                                                                                                                               
    ATTRIBUTE_NEWVALUE                      VARCHAR2(4000)                                                                                                                                                                               
    DESC imsaudittasksession12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_TS_OID                           VARCHAR2(100)                                                                                                                                                                                
    PARENT_EVENT_OID                        VARCHAR2(100)                                                                                                                                                                                
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    TASKSESSION_OID                NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ADMIN_DN                       NOT NULL VARCHAR2(512)                                                                                                                                                                                
    ADMIN_NAME                              VARCHAR2(255)                                                                                                                                                                                
    TASK_NAME                               VARCHAR2(255)                                                                                                                                                                                
    TASK_TAG                       NOT NULL VARCHAR2(255)                                                                                                                                                                                
    TASK_DESCRIPTION                        VARCHAR2(4000)                                                                                                                                                                               
    TASK_PRIORITY                           NUMBER                                                                                                                                                                                       
    STATE                          NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENVNAME                        NOT NULL VARCHAR2(100)                                                                                                                                                                                
    ENV_OID                        NOT NULL VARCHAR2(100)                                                                                                                                                                
    DESC imsAuditObjectRelationship12;
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER                                                                                                                                                                                       
    PARENT_EVENT_ID                NOT NULL NUMBER                                                                                                                                                                                       
    AUDIT_TIME                     NOT NULL TIMESTAMP(6)                                                                                                                                                                                 
    OBJECT_TYPE                    NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_DN                      NOT NULL VARCHAR2(512)                                                                                                                                                                                
    CONTAINER_TYPE                 NOT NULL VARCHAR2(100)                                                                                                                                                                                
    OBJECT_NAME                    NOT NULL VARCHAR2(255)                                                                                                                                                                                
    CONTAINER_NAME                 NOT NULL VARCHAR2(255)                                                                                                                                                                                
    CONTAINER_DN                   NOT NULL VARCHAR2(512)                                                                                                                                                                          
    OPERATION                      NOT NULL VARCHAR2(50)                                                                                                                                                                                 
    Thanks,
    Badri

  • Joining Two Tables based on Conditions.

    Hi All,
    i would like to join two tables emp and dept with conditional join like
    for example: select emp.column1...............
    left outer join DEPT ON if Dept.column1='A" then emp.column1=dept.column1
    else emp.column2=dept.column3
    i know that bu using the UNION we can dothis, but i donot want to USE UNION.
    please let me know if any workaround for this.
    Thanks,
    Kalyan

    SQL> select * from etl
      2  /
         EMPNO ENAME      JOB              MGR        SAL       COMM     DEPTNO AAA                     DEPT_NO
          7566 JONES      MANAGER         7839       2975                    20 2                         20
          7654 MARTIN     SALESMAN        7698       1250       1400         30 3                         10
          7698 BLAKE      MANAGER         7839       2850                    30 3                         10
          7782 CLARK      MANAGER         7839       2450                    10 1                         20
          7788 SCOTT      ANALYST         7566       3000                    20 2                         20
          7839 KING       PRESIDENT                  5000                    10 1                         20
          7844 TURNER     SALESMAN        7698       1500                    30 3                         10
          7876 ADAMS      CLERK           7788       1100                    20 2                         20
          7900 JAMES      CLERK           7698        950                    30 3                         10
          7902 FORD       ANALYST         7566       3000                    20 2                         20
          7934 MILLER5    CLERK           7782       1300                    10 1                         20
          7936 MILLER7    CLERK           7782       1300                    10 1                         20
    12 rows selected.
    SQL> select * from dept;
        DEPTNO DNAME          LOC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
    SQL> ed
    Wrote file afiedt.buf
      1  select
      2     e.empno,
      3     e.ename,
      4     e.deptno,
      5     aaa,
      6     e.dept_no,
      7     d.deptno,
      8     d.dname
      9  from
    10     etl e ,
    11     dept d
    12  where
    13     (e.aaa =1 and e.deptno = d.deptno) or
    14*    (e.aaa =2 and e.dept_no =d.deptno)
    SQL> /
         EMPNO ENAME          DEPTNO AAA                     DEPT_NO     DEPTNO DNAME
          7566 JONES              20 2                            20         20 RESEARCH
          7788 SCOTT              20 2                            20         20 RESEARCH
          7876 ADAMS              20 2                            20         20 RESEARCH
          7902 FORD               20 2                            20         20 RESEARCH
          7782 CLARK              10 1                            20         10 ACCOUNTING
          7839 KING               10 1                            20         10 ACCOUNTING
          7934 MILLER5            10 1                            20         10 ACCOUNTING
          7936 MILLER7            10 1                            20         10 ACCOUNTING
    8 rows selected.Regards
    Singh

  • Joining 3 tables based on the Value from 1 Table

    Dear Oracle Guru's
    I have a Table called Resourcemaster , which stores the details of Resources for the project , The Resources can be of 2 types either our own employees as well as Hired Developers from other consultancies. The following is the proposed Resourcemaster table structure
    Resourcemaster
    Project_id
    Resource_ID
    Resource_type (E - Employees /H - Hired)
    Resource_code
    Employeemaster
    Emp_code
    Emp_Name
    Emp_address
    Hiredresourcemaster
    Hired_code
    Hired_Name
    Hired_Address
    I need to query the resourcemaster , my result should be containing Project id, resource id , Resource type, Resource Name and Resource Address. for which the details are in the above 3 tables . How these 3 tables can be joined and the join is based on the condition, if the resource type is E then resource code = Emp_code from employee master or if the resource type is H then resource_code = Hired_code.
    I am not clear how to join them in a single query and i got stuck
    Kindly Clarify me
    With Warm Regards
    ssr

    ssr wrote:
    I have a Table called Resourcemaster , which stores the details of Resources for the project,So this table should really be called PROJECT_RESOURCES :-)
    The Resources can be of 2 types either our own employees as well as Hired Developers from other consultancies. So you have a table called RESOURCES (id [PK], code [UK?], name, address, resource_type [E or H])
    And the PROJECT_RESOURCES table is this: (project_id [FK to PROJECTS], resource_id [FK to RESOURCES])
    And by modelling your data like this, you'll also find that querying because much easier.
    Hope this helps.
    Regards,
    Rob.

  • How to Insert-Select and Updade base table

    Hi All,
    How is the best way to do this?
    I have a table A that has 1million rows.
    I need to Insert-Select into table B from a Group by on table A,
    and Update all rows in table A that were used into Insert with a flag.
    (select a hundred from A and insert 10 thousands into B)
    What to do?
    1-Update table A with flag before Insert-Select?
    2-Insert-Select group by into table B, before Update?
    3-Another way?
    Thanks in advance,
    Edson

    Either way. But you may find that updating the source flag first and then using that flag as part of your where clause when extracting rows to put in the destination table is a bit faster.
    In any case, you will commit only once after all of the work is done.

  • Select query on scapptseg table based on tst_from  field.

    Hi everybody,
    We enhanced the activity search with date type and date fields. User will enter date along with the datetype field to search. He can enter even ranges also for date field.
    I tried by converting the date into timestamp and querid the table but in vain as timestamp is different from the timestamp in table even though date part is same.
    Please let me know how to query this table using date. This may be a basic question. Please help me.

    Thanks for your reply.
    I know that. For example user entered 5/15/2014 in date field on web ui.
    in SCAPPTSEG, tst_from having value 5/15/2014 06:40:00 ( 20140515064000) , when I use convert statement below i am getting a timestamp where date is same but timepart is different. so query is failing.
           CLEAR lv_timestamp.
          CONVERT DATE '20140515'
          TIME sy-uzeit
          INTO TIME STAMP lv_timestamp TIME ZONE sy-zonlo.
    My question is when we have a timestamp filed in database , can i query the database when i am having only date values at my hand and i dont have any time information.

  • How and when does table SWIWIOBJCT being updated / populated with workitem

    Hi Experts,
    I am currently debugging an issue wherein a Invoice Workflow is being displayed in the Purchase Order Workflow Overview. I debugged this and found out that this is because there are entries being selected from table SWIWIOBJCT and this will be also displayed in the workflow overview.
    So what I am trying to look into is how does this table being populated. Like, what are the criteria and requirements that this table will be updated / populated.
    Any help is highly appreciated.
    Thanks so much in advance,
    Larissa
    Edited by: Larissa Taguiamon on Apr 14, 2010 7:48 PM

    Abdullah
    The EKBE table will have items related to PO history - so if you go to a PO line item and look at the PO history you will see an invoice but you will NOT see a payment.   Payments are not directly related to a PO and therefore no payment information would appear in your PO history and therefore it wouldn't be on the EKBE table either 
    Sandra

  • Is it possible to use a case statement when joining different tables based on input parameters?

    Hi,
    I have a scenario where my stored procedure takes 5 parameters and the users can pass NULL or some value to these parameters and based on the parameters, I need to pull data from various tables.
    Is it possible to use a case statement in the join, similar the one in the below example. I'm getting error when I use the below type of statement.
    select a.*
    from a
    case
    when parameter1=1 then
    inner join a on a.id = b.id
    when parameter1=2 then
    inner join a on a.id = c.id
    end;
    Please let me know, if this type of statement works, and if it works will it create any performance issues?. If the above doesn't work, could you please give me some alternate solutions?
    Thanks.

    Here's a technique for joining A to B or C depending on the input parameters. In theory, you are joining to both tables but the execution plan includes filters to skip whichever join is not appropriate. The drawback is that you have to do outer joins, not inner ones.
    CREATE TABLE A AS SELECT LEVEL ak FROM dual CONNECT BY LEVEL <= 100;
    CREATE TABLE b AS SELECT ak, bk
    FROM A, (SELECT LEVEL bk FROM dual CONNECT BY LEVEL <= 10);
    CREATE TABLE c(ak, ck) AS SELECT ak, bk*10 FROM b;
    variable p1 NUMBER;
    variable p2 NUMBER;
    exec :p1 := 1;
    exec :p2 := 20;
    SELECT /*+ gather_plan_statistics */ A.ak, nvl(b.bk, c.ck) otherk FROM A
    LEFT JOIN b ON A.ak = b.ak AND :p1 IS NOT NULL AND b.bk = :p1
    LEFT JOIN c ON A.ak = c.ak AND :p1 is null and :p2 IS NOT NULL and c.ck = :p2
    WHERE A.ak <= 9;
    SELECT * FROM TABLE(dbms_xplan.display_cursor(NULL,NULL,'IOSTATS LAST'));
    | Id  | Operation             | Name            | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    |   0 | SELECT STATEMENT      |                 |      1 |        |      9 |00:00:00.01 |       7 |
    |*  1 |  HASH JOIN OUTER      |                 |      1 |      9 |      9 |00:00:00.01 |       7 |
    |*  2 |   HASH JOIN OUTER     |                 |      1 |      9 |      9 |00:00:00.01 |       7 |
    |*  3 |    TABLE ACCESS FULL  | A               |      1 |      9 |      9 |00:00:00.01 |       3 |
    |   4 |    VIEW               | VW_DCL_5532A50F |      1 |      9 |      9 |00:00:00.01 |       4 |
    |*  5 |     FILTER            |                 |      1 |        |      9 |00:00:00.01 |       4 |
    |*  6 |      TABLE ACCESS FULL| B               |      1 |      9 |      9 |00:00:00.01 |       4 |
    |   7 |   VIEW                | VW_DCL_5532A50F |      1 |      9 |      0 |00:00:00.01 |       0 |
    |*  8 |    FILTER             |                 |      1 |        |      0 |00:00:00.01 |       0 |
    |*  9 |     TABLE ACCESS FULL | C               |      0 |      9 |      0 |00:00:00.01 |       0 |
    Predicate Information (identified by operation id):
       1 - access("A"."AK"="ITEM_0")
       2 - access("A"."AK"="ITEM_1")
       3 - filter("A"."AK"<=9)
      5 - filter(:P1 IS NOT NULL)
       6 - filter(("B"."AK"<=9 AND "B"."BK"=:P1))
       8 - filter((:P2 IS NOT NULL AND :P1 IS NULL))
       9 - filter(("C"."AK"<=9 AND "C"."CK"=:P2))
    You can see that table C was not really accessed: the buffer count is 0.
    exec :p1 := NULL;
    SELECT /*+ gather_plan_statistics */ A.ak, nvl(b.bk, c.ck) otherk FROM A
    LEFT JOIN b ON A.ak = b.ak AND :p1 IS NOT NULL AND b.bk = :p1
    LEFT JOIN c ON A.ak = c.ak AND :p1 is null and :p2 IS NOT NULL and c.ck = :p2
    WHERE A.ak <= 9;
    SELECT * FROM TABLE(dbms_xplan.display_cursor(NULL,NULL,'IOSTATS LAST'));
    Now table B is not accessed.
    | Id  | Operation             | Name            | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
    |   0 | SELECT STATEMENT      |                 |      1 |        |      9 |00:00:00.02 |       7 |      2 |
    |*  1 |  HASH JOIN OUTER      |                 |      1 |      9 |      9 |00:00:00.02 |       7 |      2 |
    |*  2 |   HASH JOIN OUTER     |                 |      1 |      9 |      9 |00:00:00.01 |       3 |      0 |
    |*  3 |    TABLE ACCESS FULL  | A               |      1 |      9 |      9 |00:00:00.01 |       3 |      0 |
    |   4 |    VIEW               | VW_DCL_5532A50F |      1 |      9 |      0 |00:00:00.01 |       0 |      0 |
    |*  5 |     FILTER            |                 |      1 |        |      0 |00:00:00.01 |       0 |      0 |
    |*  6 |      TABLE ACCESS FULL| B               |      0 |      9 |      0 |00:00:00.01 |       0 |      0 |
    |   7 |   VIEW                | VW_DCL_5532A50F |      1 |      9 |      9 |00:00:00.01 |       4 |      2 |
    |*  8 |    FILTER             |                 |      1 |        |      9 |00:00:00.01 |       4 |      2 |
    |*  9 |     TABLE ACCESS FULL | C               |      1 |      9 |      9 |00:00:00.01 |       4 |      2 |

  • Adding one more table to the select statement joining 4 tables gives dump

    Hi All,
    There is a select statement using which four tables namely VBAK,VBAP,LIPS and LIKPUK(view) are joined(inner join).Here, date and time fields are selected from LIPS and used.
    My requirement is to consider the Date (LIKP - WADAT_IST) instead of LIPS-ERDAT  and time(LIKP-SPE_WAUHR_IST) is to be used instead of LIPS-erzet.
    Neither LIPS nor LIKPUK contains time(SPE_WAUHR_IST) field. And, I cannot remove LIPS table or LIKPUK view as each contains a unique field which is used in the report.
    When I tried to join LIPS in the select query, it is going to dump.
    Can someone suggest a good approach ?
    Thanks,
    Pavan

    Thank you for the detailed explanation of the dump.
    The dump details together with your code lead to the answer: 42.
    Regards,
    Clemens

  • SQLPLUS sub quey and joining multiple tables

    SELECT a.ac_number AS ac_number,
           m.mod_name AS Model,
           m.mod_manufacturer AS Make,
           ch.char_trip,
           ch.destination
    FROM tables.aircraft a
            SELECT a.ac_number,
                   count(char_trip) numtrip,
                   avg(count(char_trip)) OVER() avgnumtrip
            FROM tables.charter
            GROUP BY charter.ac_number)
    charter
    WHERE a.ac_number = ch.ac_number
    AND a.mod_code = m.mod_code
    AND charter.numtrip > charter.avgnumtrip
    Sqlplus> @ 2
    ERROR at line 7:
    ORA-00933: SQL command not properly endedCan someone help me with that? also my where clause doesnt seem right. Im trying to do a left join on. thanks

    Hi,
    SELECT a.ac_number AS ac_number,
           m.mod_name AS Model,
           m.mod_manufacturer AS Make,
           ch.char_trip,
           ch.destination
    FROM tables.aircraft a
            SELECT a.ac_number,
                   count(char_trip) numtrip,
                   avg(count(char_trip)) OVER() avgnumtrip
            FROM tables.charter
            GROUP BY charter.ac_number)
    charter
    WHERE a.ac_number = ch.ac_number
    AND a.mod_code = m.mod_code
    AND charter.numtrip > charter.avgnumtrip
    ;Few things which i could notice
    refer comments
    SELECT a.ac_number AS ac_number,
           m.mod_name AS Model, /* what table is m */
           m.mod_manufacturer AS Make, /* what table is m */
           ch.char_trip,/* assume you want to alias charter as ch */
           ch.destination/* assume you want to alias charter as ch */
    FROM tables.aircraft a /* no comma */
            SELECT a.ac_number,  /* cannot refer A (tables.aircraf) table here */
                   count(char_trip) numtrip,
                   avg(count(char_trip)) OVER() avgnumtrip
            FROM tables.charter
            GROUP BY charter.ac_number)
    charter
    WHERE a.ac_number = ch.ac_number /* assume you want to alias charter as ch */
    AND a.mod_code = m.mod_code /* what table is m */
    AND charter.numtrip > charter.avgnumtrip /* change charter to CH if you alias charter as CH */

  • To Select Data from Mutiple Table having different column name

    Dear All,
    I have 100 tables under one user Operation, each table has column containg the code of the Data Entry Operator, But name of the column in each table is different.
    Can anyone give the script to run from SQL PLUS so the we can find the total entry of each D.E. Operator per month.
    Thanks and Regards,
    Manoj

    Perhaps the UNION ALL operator is what you want.
    SELECT data_entry_operator, count(*) AS qty
    FROM(
    SELECT de_oper AS data_entry_operator FROM a
    UNION ALL
    SELECT d_e_operator FROM b
    UNION ALL
    SELECT d_e_op FROM c
    GROUP BY data_entry_operator;

Maybe you are looking for

  • How can I open a cc2014 file in my cs6 version of Indesign ?

    How can I open a cc2014 file in my cs6 version of Indesign ? I have several files that has been saved in a cc2014 version and I can't open them in my CS6 version of Indesign. I don t have access to anyone who have the cc 2014 version to convert the f

  • ITunes Match Appears To Have 'Broken' A Song

    I haven't seen anything like this particular issue in the iTunes Match–Discussions I scanned, so here goes... Recently I have been playing with iTunes Match, and it's been working quite well.  The best way I learn is to actively engage with something

  • Clean installation of Win 7 using UEFI and RAID

    Hi, I am trying to perform a clean UEFI installation of Windows 7 with SATA mode switched to RAID. I have two identical HDDs for RAID and 80 GB Intel 310 to put the OS on. The BIOS/UEFI can see all 3 drives and the two HDDs are configured for RAID th

  • Can't get WiFi sync to work with iOS 5 devices

    I have an iPad 1 and an iPhone 3GS, both running iOS 5.  I can't get either device to synch to my iTunes library wirelessly.  I have seen the procedures in the KB, but the part that I am missing is that neither device has the "Sync with this iOS devi

  • Compiling and running jsp-code on the fly.

    I am intending to use jsp as a report engine. The 'templates' should be jsp-code stored in the db, loaded and executed as if they where inside a plain jsp-file in the application. I cannot write new files on the file system since this is prohibited o