An interesting problem in emp table of scott/tiger schema, kindly help

I want to write SQL which can give me the result in columns
ENAME, EMPNO, DEPTNO, DEPT_WISE_PCT_OF_EMP_SAL,
CMP_WISE_PCT_OF_EMP_SAL
i write a sql by using multiple select statements like
select ename, ename, emp.deptno, sal, (sal/a.cmp_sal )*100 cmp_sal_pct, (sal/dept_sal.dept_sal)*100 dept_sal_pct
from emp, (select sum(sal) cmp_sal from emp) a,
(select deptno, sum(sal) dept_sal from emp group by deptno) dept_sal
where dept_sal.deptno = emp.deptno
ENAME ENAME DEPTNO SAL CMP_SAL_PCT DEPT_SAL_PCT
CLARK CLARK 10 2450 8.4409991386736 28
KING KING 10 5000 17.226528854436 57.142857142857
MILLER MILLER 10 1300 4.4788975021533 14.857142857143
SMITH SMITH 20 800 2.7562446167097 7.3563218390805
ADAMS ADAMS 20 1100 3.7898363479759 10.114942528736
FORD FORD 20 3000 10.335917312661 27.586206896552
SCOTT SCOTT 20 3000 10.335917312661 27.586206896552
JONES JONES 20 2975 10.249784668389 27.35632183908
ALLEN ALLEN 30 1600 5.5124892334195 17.021276595745
BLAKE BLAKE 30 2850 9.8191214470284 30.31914893617
MARTIN MARTIN 30 1250 4.306632213609 13.297872340426
JAMES JAMES 30 950 3.2730404823428 10.106382978723
TURNER TURNER 30 1500 5.1679586563307 15.957446808511
WARD WARD 30 1250 4.306632213609 13.297872340426
but i want to solve this problem using single sql
i write the required sql but i unable to get the salary on company basis(total sal of all emp)
select emp.ename, emp.deptno, emp.empno, emp.sal,
(emp.sal/sum(dept_emp.sal))*100 dept_sal_pct
from emp, emp dept_emp
where emp.deptno = dept_emp.deptno
group by emp.ename,emp.deptno, emp.empno, emp.sal
ENAME DEPTNO EMPNO SAL DEPT_SAL_PCT
CLARK 10 7782 2450 28
KING 10 7839 5000 57.142857142857
MILLER 10 7934 1300 14.857142857143
ADAMS 20 7876 1100 10.114942528736
FORD 20 7902 3000 27.586206896552
JONES 20 7566 2975 27.35632183908
SCOTT 20 7788 3000 27.586206896552
SMITH 20 7369 800 7.3563218390805
ALLEN 30 7499 1600 17.021276595745
BLAKE 30 7698 2850 30.31914893617
JAMES 30 7900 950 10.106382978723
MARTIN 30 7654 1250 13.297872340426
TURNER 30 7844 1500 15.957446808511
WARD 30 7521 1250 13.297872340426
thanks in advance

Try this (untested)
select ename, ename, emp.deptno, sal,
  RATIO_TO_REPORT(sal) OVER ()*100 cmp_sal_pct,
  RATIO_TO_REPORT(sal) OVER (PARTITION BY dept)*100 dept_sal_pct
from empRATIO_TO_REPORT is the key. If this doesn't work then check the docs.

Similar Messages

  • Grant permission on SCOTT/tiger schema

    grant select, insert, update, delete on EM to BOMBAC
    I want to add permission that user BOMBAC can see EM table from SCOTT/tiger scheme and use it. But he can not see and use it. Any idea why?
    regards

    yes i try with public but it seems that i do not have enought privilegies
    *Cause:    An attempt was made to change the current username or password
    without the appropriate privilege. This error also occurs if
    attempting to install a database without the necessary operating
    system privileges.
    When Trusted Oracle is configure in DBMS MAC, this error may occur
    if the user was granted the necessary privilege at a higher label
    than the current login.
    *Action:   Ask the database administrator to perform the operation or grant
    the required privileges.
    For Trusted Oracle users getting this error although granted the
    the appropriate privilege at a higher label, ask the database
    administrator to regrant the privilege at the appropriate label.

  • Hi, I'm on my iPhone wifi problem by disabling my WiFi does not deal kindly help me

    Hi, I'm on my iPhone wifi problem by disabling my WiFi does not deal kindly help me

    Hi 481525018,
    Welcome to the Support Communities!
    The article below may be able to help you troubleshoot your Wi-Fi issue.
    iOS: Troubleshooting Wi-Fi networks and connections
    http://support.apple.com/kb/TS1398?viewlocale=en_US
    Cheers,
    - Judy

  • Trouble logging into sql using scott/tiger..please help..URGENT!!!

    hai friends!
    I installed D2K recently. Iam unable to login to sql (directly or from D2K forms) using the usual username/password..scott/tiger,system/manager,demo/demo. I get the following error message :
    TNS error: Unable to connect to destination
    Please help me out!
    thanX,
    raji

    what is your error number.
    Did you try to look up this number in the oracle error book?
    Usually you can also find the solution there.
    My bet is that your tnsnames.ora file is not set properly.

  • Basic SQL queries in Scott/tiger schema...

    One of my team member who is a fresher wants to learn SQL.
    Access to Scott/tiger is available.
    Can some links or queries be posted which can be tried and will be useful to learn SQL.

    Hi,
    Read the following links:
    http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14261/toc.htm
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/toc.htm
    http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14251/toc.htm
    Regards
    Avinash

  • Problem Deploying a Table in not default schema from NWDS

    Hello Experts,
    I've a webService that works on a Table deployed in my Portal DB..
    I deployed it from my NWDS using an archive file created in a Dictionary Project.
    Now I've created a new Datasource (type VendorSQL) with a new Schema..
    How can I deploy my Tables in the NEW custom Schema??
    Could you help me?
    Best Regards
    Alessandro

    Hello Alessandro,
      You will have to manually deploy this. The SQLJ and Java Dictionary capabilities that is used to deploy only is possible in the default database schema as far as I know
    Hope that helps
    Abdul

  • Problem accessing sys table from user's schema

    Hi,
    I had a store procedure whose owner is say user1.Inside of that store procedure i am executing the following select query.
    select * from sys.all_coloumns where owner = 'USER1';
    This is resulting me "no data found".
    When i am executing the same select query from SQL*plus it is giving me the proper output.
    I think this is happening because of less systeme privileges/role assigned to schema USER1.
    Can you please suggest which priviledges i need to grant, so that my proc. will work fine.

    User1 needs select rights on sys.all_columns directly.
    Not via the DBA role.

  • SQL query on emp table(SCOTT Schema): Highest salary per department

    From emp table of SCOTT schema. I would like to know the name, sal,the deptno he belongs of the highest paid employee from each dept.
    Explaining further:
    The following query only returns deptno and max(sal) of each department. I need the name of the highest paid employee from each dept along with the below result.
    SQL> SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BY deptno;
    DEPTNO MAX(SAL)
    30 2850
    20 3000
    10 5000

    It seems you want just one ename if two employees have the same highest salary. In that case you can use this construct:
    SQL> select deptno
      2       , max(sal)
      3       , max(ename) keep (dense_rank last order by sal)
      4    from emp
      5   group by deptno
      6  /
        DEPTNO   MAX(SAL) MAX(ENAME)
            10       5000 KING
            20       3000 SCOTT
            30       2850 BLAKE
    3 rijen zijn geselecteerd.Regards,
    Rob.

  • How to Model Emp table

    Hi Experts,
    I am new to obiee,i just want to model the normal emp Table in scott schema,
    Could You please explain me how to configure only Emp table like this
    and report should be like
    Manager Name Promt
    Table should be like
    Mangaer Name,Avg salary of employee under the manager
    Edited by: user1005945 on Feb 6, 2011 12:40 PM

    Hi Experts,
    I am new to obiee,i just want to model the normal emp Table in scott schema,
    Could You please explain me how to configure only Emp table like this
    and report should be like
    Manager Name Promt
    Table should be like
    Mangaer Name,Avg salary of employee under the manager
    Edited by: user1005945 on Feb 6, 2011 12:40 PM

  • Simple scott/tiger query in horizontal format

    Hi Guys, this is my first thread to this forum, as I m a java developer and does not come across sql frequently.
    I have a simple requirement.
    I ll need this example to let u know, what is all I need.
    You must be very well aware of the famous scott/tiger schema in Oracle database where the emp table has a many-one relationship with dept table.
    Is there any way, where I can get the data in the below format (assuming one dept will have maximum 4 employees and minimum 0 employees)
    DeptNo Employee1 Employee2 Employee3 Employee4
    Accounting CLARK KING MILLER Null
    Research JONES SCOTT ADAMS FORD
    Sales ALLEN WARD NULL NULL
    thanks in advance,
    Abzee

    Hi, Abzee,
    Welcome to the forum!
    That's called a Pivot and here's one way to do it:
    WITH     got_rnum     AS
         SELECT     deptno
         ,     ename
         ,     ROW_NUMBER () OVER ( PARTITION BY  deptno
                             ORDER BY        ename
                           )     AS rnum
         FROM     scott.emp
    SELECT       d.dname
    ,       MIN (CASE WHEN r.rnum = 1 THEN r.ename END)     AS employee1
    ,       MIN (CASE WHEN r.rnum = 2 THEN r.ename END)     AS employee2
    ,       MIN (CASE WHEN r.rnum = 3 THEN r.ename END)     AS employee3
    ,       MIN (CASE WHEN r.rnum = 4 THEN r.ename END)     AS employee4
    FROM       scott.dept     d
    JOIN       got_rnum     r     ON     r.deptno     = d.deptno
    GROUP BY  d.dname
    ;To do a pivot, you must have columns or expressions that tell what row and column of the output correspond to each row of the input.
    In this example, d.dname uniquely identifies the row of the output, but there's nothing that says that 'JONES' belongs in the employee1 column rather than the employee2 column. I used ROW_NUMBER to assign each employee a number, starting with 1 in each deptartment, for just that reason. I used ORDER BY ename, so that the employees will appear in alphabetic order. You could use any other way of sorting them (e.g., by salary or descending hiredate).
    This site normally compresses multiple spaces into one space.
    To post formatted text, such as your desired reuslts, type these 6 characters:
    (all small letters, inside curly brackets) before and after the formatted sections.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Finding column,table names in a schema with search data

    Hi All,
    I want to list all the columns,tables in a schema with any search value.
    Ex: Take an example of EMP table, if I search with 'MARTIN', result should be ename (column name) , emp(table name).
    Can you please help me on this regard to write a query.
    Regards,
    Rajasekhar

    SQL> set line 1000
    SQL> VAR val VARCHAR2(30)
    SQL> execute :val:='SMITH';
    PL/SQL procedure successfully completed.
    SQL> SELECT   DISTINCT
      2               SUBSTR(:val, 1, 11) "Searchword",
      3               SUBSTR(table_name, 1, 14) "Table",
      4               SUBSTR(t.COLUMN_VALUE.getstringval(), 1, 50) "Column/Value"
      5  FROM     cols,
      6               table(XMLSEQUENCE(DBMS_XMLGEN.getxmltype(   'select '
      7                                                        || column_name
      8                                                        || ' from '
      9                                                        || table_name
    10                                                       || ' where (UPPER('''
    11                                                       || :val
    12                                                       || ''')=UPPER('
    13                                                       || column_name
    14                                                       || '))').EXTRACT('ROWSET/ROW/*'))) t
    15  WHERE    table_name IN ('EMP', 'DEPT','EMPLOYEES') --limiting the table names, you can omit this.
    16  ORDER BY "Table";
    Searchword                                   Table                                                    Column/Value
    SMITH                                        EMP                                                      <ENAME>SMITH</ENAME>
    SMITH                                        EMPLOYEES                                                <LAST_NAME>Smith</LAST_NAME>

  • Connect string for scott/tiger

    i've personal oracle 8i with only scott/tiger schema on my pc. i installed forms 6i. when i try to connect to my scott/tiger schema it gives me tns listener error. i'm using scott/tiger as username/password. i tried keeping Host string null and 'ORCL' but no luck....
    any help is appreciated
    thank you very much
    ---himanshu

    Himanshu,
    When u have the default database and Oracle Forms on the same machine, from SQL prompt it will connect to the database without any connect string. But when u connect from Forms u NEED to have a TNS entry. Open explorer --> Oracle Home directory --> Network --> Admin and u will find the Tnsnames.ora Open this file and create a new TNS entry ( u will find examples ) Give ur machine IP address/or user name and SID ( which will be ORCL by default ) save this file. Then connect from Forms it will connect. Good luck
    orcl.world =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS =
    (COMMUNITY = tcp.world)
    (PROTOCOL = TCP)
    (Host = your machine IP)
    (Port = 1521)
    (CONNECT_DATA = (SID = ORCL)
    now orcl is ur TNS name

  • How to get emp and dept tables of scott schema if they acdientally deleted

    Hii I accedentally modifiled emp and dept tables in scott schema.... Can you please tell how to recreate fresh emp and dept tables ? is there any way to get emp and dept tables? please help regarding this

    If you are on Oracle 10g version...
    The demo tableds under scott schema can be created using oracle supplied script.
    http://www.oracle.com/technology/sample_code/tech/sql_plus/htdocs/demobld.html
    Alternatively, this script exists in $ORACLE_HOME/sqlplus/demo directory.
    vr,
    Sudhakar B.

  • Error when trying to issue a grant statement for Scott's emp table

    I'm trying to complete a lab assignment in which we have to grant the select on Scott's emp table. Here is the statement I used. I'm wondering why did i receive the error that the table doesn't exist.
    SQL> GRANT SELECT ON SCOTT.EMP TO DEV;
    GRANT SELECT ON SCOTT.EMP TO DEV
    ERROR at line 1:
    ORA-00942: table or view does not exist
    Edited by: user566571 on Dec 4, 2009 8:19 AM

    Are you the owner of the emp table ? If not then
    Do the user(which you are using to grant the privilege) has GRANT ANY PRIVILEGE or GRANT ANY OBJECT PRIVILEGE privileges ?
    Do the user has been granted the appropriate privileges on the SCOTT.EMP WITH GRANT option ?
    To grant the privileges to the role, first your user should have those privileges and that with the option to grant to others using WITH GRANT.

  • How to generate XSD from simple table (e.g. emp from scott/tiger)?

    Please excuse the ignorance...
    I've been trying to determine if there's a simple way (i.e. using a command in sqlplus or via a GUI wizard), to point to a simple table (such as the demo 'emp' table), and have Oracle then generate an XML Schema (XSD) file that contains the representation for that table, so that I can use that XSD file with various XML-based tools.
    Any help is appreciated.
    Thanks,
    Mike

    Hi,
    Please refer this how to guide which may give some idea for creation formula:
    http://www.pioneerb1.com/wp-content/uploads/2012/04/How-to-work-with-Crystal-Reports-8.8.pdf
    Thanks & Regards,
    Nagarajan

Maybe you are looking for