Question involving select... more partition...

Hello

I use SQL Cookbook by Anthony Molinaro to learn more about SQL Oracle-specific features. Recipe 3.9 on "Joins when Performing using aggregates" States 'find the sum of the salaries of employees in the dept 10 as well as the sum of their bonus' gives the following query as a solution to the problem:
select distinct deptno, total_sal, total_bonus
  from (
        select e.empno,
               e.ename,
               sum(distinct e.sal) over (partition by e.deptno) as total_sal,
               e.deptno,
               sum(e.sal * case
                             when eb.type = 1 then .1
                             when eb.type = 2 then .2
                             when eb.type = 3 then .3
                             else 0
                           end)    over (partition by e.deptno) as total_bonus
          from emp e, emp_bonus eb
         where e.empno = eb.empno
           and e.deptno = 10
       );
This code works, but only if there is no two deptno 10 employees who have the same salary. If two different 10 deptno employees the same wages then the 'sum (distinct e.sal)' causes the sum of salaries to fall short. To make sure that this conclusion was correct, I used the following data tables emp and emp_bonus respectively (where emp 7840 was deliberately added to cause and show the problem):
     EMPNO ENAME             SAL     DEPTNO
---------- ---------- ---------- ----------
      7782 CLARK            2450         10
      7839 KING             5000         10
      7934 MILLER           1300         10
      7840 AJONESA          5000         10

     EMPNO RECEIVED        TYPE
---------- --------- ----------
      7934 17-MAR-05          1
      7934 15-FEB-05          2
      7839 15-FEB-05          3
      7782 15-FEB-05          1
      7840 17-MAR-05          1
using the tables above, the result should I get is
    DEPTNO  TOTAL_SAL TOTAL_BONUS
---------- ---------- -----------
        10      13750        2635
that the query above does not produce because employees 7839 and 7840 happen to have the same salary (total sal is 8750 instead of 13750, and the returned total bonus is correct).

The following query gets the right result:
SQL> select d.deptno,
  2         d.total_sal,
  3         sum(e.sal * case
  4                       when eb.type = 1 then .1
  5                       when eb.type = 2 then .2
  6                       when eb.type = 3 then .3
  7                       else 0
  8                     end) as total_bonus
  9    from emp e,
 10         emp_bonus eb,
 11         (
 12          select deptno, sum(sal) as total_sal
 13            from emp
 14           where deptno = 10
 15           group by deptno
 16         ) d
 17    where e.deptno = d.deptno
 18      and e.empno  = eb.empno
 19    group by d.deptno, d.total_sal;
I tried to find a query using sum (.) on (partition...) which would give the same result as this one and this is what left me speechless. Is there a way to get the same result as this last request, using 'select... sum (sal...) courses (partition...). similar to the one shown in the first code snippet?

Thank you for your help,

John.

Hi, John,.

It gives you the results you want:

select distinct deptno,
     SUM ( CASE
            WHEN  r_num = 1
            THEN  sal
           END
         ) OVER (PARTITION BY deptno) AS total_sal,
     total_bonus
  from (
        select e.empno,
            e.sal,
            ROW_NUMBER () OVER ( PARTITION BY e.empno
                        ORDER BY      NULL
                      )     AS r_num,
               e.deptno,
               sum(e.sal * case
                             when eb.type = 1 then .1
                             when eb.type = 2 then .2
                             when eb.type = 3 then .3
                             else 0
                           end)    over (partition by e.deptno) as total_bonus
          from emp e, emp_bonus eb
         where e.empno = eb.empno
           and e.deptno = 10
       );

As I said previously, the ROW_NUMBER analytic function assigns the number 1 in exactly one line per person, so only 1 numbered lines are counted in the SUM which produces total_sal. analytical functions cannot be nested (for example, analytical SUM argument can be an expression that uses Analytics ROW_NUMBER), that is why the SUM of total_sal is made in the main query.
Personally, I wouldn't use the analytical SUM for it. When there is a choice between the aggregate functions and analytical, it is almost always easier and more efficient to use the aggregate functions when you want a line of output for each group (or partition); Use the analytical functions only when you want a line of output for each line of raw data. In general, think very carefully whenever you are tempted to use SELECT DISTINCT ; It is not very often used in well-designed applications.
Here's how to get the same results using global funds:

select  deptno,
     SUM ( CASE
            WHEN  r_num = 1
            THEN  sal
           END
         )          AS total_sal,
     SUM (bonus)     AS total_bonus
  from (
        select e.empno,
            e.sal,
            ROW_NUMBER () OVER ( PARTITION BY e.empno
                        ORDER BY      NULL
                      )     AS r_num,
               e.deptno,
               e.sal * case
                             when eb.type = 1 then .1
                             when eb.type = 2 then .2
                             when eb.type = 3 then .3
                             else 0
                       end          as bonus
          from emp e, emp_bonus eb
         where e.empno = eb.empno
           and e.deptno = 10
       )
GROUP BY  deptno;

No query above depends on whether you're dealing only with a deptno.
The two queries above calculate tha total_salary after that the inner join is made. If an employee doesn't have at least one line in emp_bonus, that wages will not be included in total_sal.

In the case where you wonder why you got an error when you tried:

sum(sal) over (partition by distinct(empno || sal)) as total_sal

"PARTITION BY * SEPARATE * x ' is not allowed. Partitions are always based on distinct values, so there is no point in adding a DISTINCT keyword.
In addition, you probably don't want say "PARTITION BY (empno |)» SAL). Consider what would happen if you had these two lines:

EMPNO   SAL
-----   -----
1234    500
123     4500

The empno expression | SAL is the same ('1234500') for the two lines, so that they would go in the same partition.
What made you proabably "PARTITION BY empno, sal.

Tags: Database

Similar Questions

  • More partition

    Hello world

    I HAVE HP DV7 WITH 750 GB HARD DRIVE AND I WANT TO DO MORE PARTITION AND I ALSO HAVE A RECOVERY DISK. CAN I MAKE MULTIPLE DRIVES 750 GB HARD DRIVE BY REINSTALLING THE RECOVERY DISK WINDOW. AND IF IT IS POSSIBLE, HOW?

    PLEASE EXPLAIN

    WITH GREETINGS

    IMRAN IJAZ BUTT

    Hello

    No need to re - install Windows. Please use the following tutorials:

    http://www.PCWorld.com/article/2066191/how-to-partition-a-hard-drive.html

    https://www.YouTube.com/watch?v=8ln5jsdmB_A

    Kind regards.

  • Question about Table of Partition MBR

    I have a Toshiba laptop running Vista Home Premium SP2 with processor Dual-Core of AMD Athlon 64 X 2, 1 GB of ram and 150 GB of HARD drive.

    My question is about the partition table in MBR. It shows 4 partitions it.

    I have the Ultimate Boot CD, so I took a look at the MBR. Here are 4 partitions occupying its table:

    1. (without drive letter)-file system: blank - EISA config partition type code'n - 1.46 GB -: x 27
    2 C: - fs: ntfs - system, boot, primary, active partition - 92,01 GB - code: x 07
    3 D: - fs: ntfs - primary - 5.98 f - code: x 07
    4. (without drive letter)-fs: blank - primary - 5,64 GB - code: x 17

    c: is free of 17%, d: is 99% free, the other two are 100% free

    Can you explain what the purpose of D:? What about the other two (with no drive letter)? I read somewhere that ' x 17' code means "hidden IFS (ex: HPFS)' & 'x 27' means a rescue partition... true?". Do I need them, or I can remove the part'ns 3 (other than c :) & make use of space?

    Looks like the recovery and BIOS utilities. You need to check with Toshiba so one any of them can be deleted.

  • BlackBerry 10 10.3.2 Hub "Select more" now requires scrolling

    My Q10 has been upgraded to 10.3.2.440.  Select more in the hub requires now all the way to the bottom of the vertical selection of scroll bar.

    My main task in the hub is removing 95% of my email that is spam or previously read on another device.

    Please move "select more" higher on the vertical bar, so it can be accessed without scrolling.  I now have to scroll twice to get to this selection.  It worsens to a major nuisance of umpteenth checking messages of the day.

    OTOH, I thank you for making the fastest message deletion.

    Thanks for your comments @kscale

    At the moment you cannot rearrange the context menu so I'll pass it along to your comments for further review.

  • I have to remove the duplicate files, but having problems by selecting more than one file in the player at the time.

    I have to remove the duplicate files, but having problems by selecting more than one file in the player at the time. How can I select multiple files at the same time so I can just delete them. I used to be able to do this in previous versions of Media Player, but this function seems now blocked or unavailable. The problem is this: through previous use of media player, it plays all my multimedia files several times whenever a device support has been added. He would try to save the files on my main drive, but since there is not enough room this would save the files somewhere else. This happened several times, I suppose, because when I upgraded my computer and a media scan was conducted he found duplicates of all my media various times of at least 6-8. I've consolidated since my plates on 3-disc multi to, but have now records duplicated hundreds and thousands of duplicate media files. Previous versions of Media Player would allow me to select all files and then delete them both of the reader, but also from my hard drive. So now I'm stuck with more than 300 GB of duplicate media I have to search line by line through all my hard drives to find each duplicate both, unless I can get media player to do what it can. Any help in this area would be appreciated.

    Hello

    1. did you of recent changes on the computer?

    2. is Windows media player files in double creation during playback of music files?

    To delete duplicate entries, click on another feature tab in the drive (for example the current playback), and then click library.

    If this does not remove duplicates from your library, you can use the Add to library dialog box to search computer to analyze a file on your computer the duplicate entries not valid-pointing. The player will remove invalid entries in your library that point to files that no longer exist in the folder.

    Complete the steps above for files that are stored in the hard disks.

    Method 1: Use the add to Library dialog box search for computers

    (a) start Windows Media Player.

    (b) press F3 on your keyboard to open Add to the library of the find computers dialog box.

    (c) click the Browse button to locate the folder on your computer so that your library contains invalid entries. Specify the location of the folder in the box look in.

    If you are not sure what duplicate in your library entry is not valid, you can add a path column to display in your library. Specify the folder on your computer that corresponds to the path not valid file displayed in your library.

    (d) click on the Search button.

    The player will search for digital media files and playlists in the folder that you specify and remove invalid entries in your library that point to files that no longer exist in the folder. If not valid in double entries point to other folders on your computer, repeat this procedure, specify a different folder every time.

    If only a small number of duplicate entries exist, you can delete those invalid manually by right-clicking on the invalid entry, and then clicking Delete .

    For large double-number of entries in your library (or if all your library is duplicated), it might be better to create a new library and delete the files.

    Method 2:

    After you remove the duplicate entries, run the troubleshooting of Windows Media Library settings to solve this problem.

    Open the troubleshooter in Windows Media Library

    http://Windows.Microsoft.com/en-us/Windows7/open-the-Windows-Media-Player-library-Troubleshooter

    Let us know the results.

    I hope this helps.

  • How can I see questions and answers more than 4 hours ago

    How can I return to questions and answers more than 4 hours ago.

    In Adobe Muse, it gives me the options to go back a page, but in this forum

    I can only 4 hours. There is an option 'More', but it is not go back more than 7 hours.

    Hello

    Click on "Content" in the menu bar.

    Stone

  • Cannot select more than one bitmap image in desktop Animate

    I can't select more than one bitmap image in desktop Animate. Always select the same object, what should I do?

    I guess these bitmaps are on the scene. They are all on the same layer? Who is always selected is large and, perhaps, on top of all the others?

    If the bitmaps are all on one layer, you can separate each in separate layers. Once you do this, you can lock / unlock layers individually so that you can work with one image at a time.

  • Select to partition interval

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    I have a partitioned table of interval on a range of numbers and to select the data of a partition for a specific value is

    CREATE TABLE part_test(part_key       NUMBER
                          ,part_data      VARCHAR2(10)
                          )
    PARTITION BY RANGE (part_key)
      INTERVAL ( 1 )
      (PARTITION p_base VALUES LESS THAN (0));
    
    INSERT INTO part_test(part_key, part_data)
    VALUES      (-1, '-1');
    INSERT INTO part_test(part_key, part_data)
    VALUES      (0, '0');
    INSERT INTO part_test(part_key, part_data)
    VALUES      (1, '1');
    INSERT INTO part_test(part_key, part_data)
    VALUES      (2, '2');
    COMMIT;
    
    SELECT *
    FROM   part_test PARTITION FOR(1);
    

    What I really want to select the partition is based on a value in another table to be

    CREATE TABLE part_value (part_key NUMBER);
    
    INSERT INTO part_value(part_key)
    VALUES      (1);
    COMMIT;
    
    SELECT *
    FROM   part_test PARTITION FOR(select part_key from part_value);
    

    It gives ORA-00936: lack of expression.  I could use dynamic sql statements to create the query (or generate a view), but it would save instead.  Any ideas if this can be achieved by a simple sql statement?

    Thank you.

    You try something like:

    SELECT *.

    OF part_test pt, part_value pv

    where pt.part_key = pv.part_key

    In most cases a particular partition will be only accessible, no full table scans (not true in all scenarios, however)

  • I can't select more than anything on the window of "program": I can't double click on my titles or my video to resize the... Help, please! Really urgent!

    I can't select more than anything on the window of "program": I can't double click on my titles or my video to resize the... Help, please! Really urgent!

    It works if you click 'Motion' in the effects Panel

  • When I Zoom, suddenly I do not see my selection more...

    When I Zoom, suddenly I do not see my selection more... I need to close and reopen the document so you can see what I select. Very annoying. Solution anyone?

    Stefaan,

    Assuming that CC 2015, try disabling GPU performance by clicking on the icon of the rocket in the bar of the App, or on the top of the menu bar, deselect the box Performance GPU; You may also uncheck performance GPU in your preferences.

  • I can't select more than one object with the SHIFT key, selected in the last Muse app

    Someone who uses the worm. last Muse CC 2014 with Mac OS 10.10.2 and had a problem of selection of several objects by using the Shift key and hold the method has a work around. It's frustrating to try to select several objects and this simple technique does not. This same shift-SELECT approach works fine on the same calculation with other products Adobe such as PS, AI, Id etc.

    I tried your suggestion to select multiple layers but no joy and no floating above the ones I had to choose. I discuss with a representative of Adobe this morning and discovered that it is possible to select more than one by dragging on the elements with the selection arrow. This forces me to block these unwanted layers selected in this shotgun approach first. It's my best work around so far. Shift-click works in the old Muse vs7.4.30 app on the same computer. I enjoyed your suggestion and hope there is a fix for the simple approach shift-click.

  • How to select more than one track

    Hello

    I'm new to Audition and I can't find a way to select more than one clip/track in 2014 Audition multitrack view.  I tried now the shift or command clicking on more than a clip or track, and who doesn't.  I have clips separated on different tracks and I wish I could select them all moving them the same distance.  Is it possible to do this?  Thank you.

    Also, is there something like a "missing manual" online for the hearing?  All I can find are specific tutorials on specific topics.  Thanks again.

    Massi says:

    I tried now the shift or command clicking on more than a clip or track, and who doesn't.

    What operating system do you use? On the PC, the Ctrl key and clicking on the titles is multiple selections, but I don't know if it's the same on Mac computers.

    Oh, and I think that Adobe are looking for an interesting experience. We seem now to be the missing manual, and it's interactive...

  • Select the partition to format RT?

    I currently use a NI PXI machine for my LabView code.  I would also like to be able to use it as a real-time target.  Hoping to do this, I created another partition on the hard disk to install the software in real time.  However, when I boot from the disc USB Utility and select "marker hard disk" I get three options: "1 reformat the first RT-compatible partition on the disk", "2 clear all Acha partitions a single partition. ' and '3. Cancel the Format.  Seem to be how to choose my partition created for these purposes.  If I select '1' I run the risk of losing my c:\ drive, or maybe the "Acronis" partition supplied with the computer.  Is there a way around this, or what I have to re-partition of my drive hard so that the new partition is 'before' the c:\ drive?

    Incidentally, most of the other options of the menu "1. Boot using software installed on the hard disk","2. Start in safe mode"etc. seem to prevent the computer to crash.  Each fixed for this?

    My disk USB Utility has been created with LabView 8.6 use MAX 4.5.0.

    Thank you

    Doug

    Hi Doug,.

    I'm happy to help you with your question! Start with, I would not recommend using the Desktop PC utility USB player with most of the PXI systems. Several recent of National Instruments PXI controllers are provided with the opportunity to start in the Mode of LabVIEW Real-time safe via an EEPROM (with any necessary USB external drive), and so it is generally a better option.

    What PXI controller do you use? If you enter the BIOS at startup of the system and see the real LabVIEW Real-time boot options, then you use one of the controllers with built-in LabVIEW of safe in real-time mode. A cautionary note: you need a LabVIEW Real-time Deployment license for run LabVIEW Real-time on your PXI controller. If you bought the controller and it delivered without LabVIEW Real-time installed, he probably didn't understand this license.

    Assuming you are using one of these controllers, you have two basic options:

    (1) install the files in time real LabVIEW and Windows on the same partition (FAT32). When you start your system, you can use the BIOS options to specify Windows or LabVIEW Real-time. The first time you start the system before loading files in time real LabVIEW, you start in safe mode and then install software of MAX. After this point, you will be able to run LabVIEW Real-time and your application at startup using the options in the BIOS.

    (2) install the files in time real LabVIEW and windows on different partitions. To do this, you can install Windows on the FAT32 partition of second or later on your system, or (more commonly) to install Windows on an NTFS partition. LabVIEW Real-time will use the first partition FAT32 or remedy available. Once more, you can use the BIOS options to specify Windows or LabVIEW Real-time to start.

    At the end of the day, you can use the USB of PC Desktop Utility player to install LabVIEW Real-time on any PC with supported hardware (assuming that you bought a LabVIEW Real-time Deployment license). However, this will cause the real-time system appears under MAX as a PC, and if you format using the command of the utility, you will need to replace the system boot record. It is possible to configure a system to dual-boot with bootloader like GRUB utilities, etc., but I highly recommend using the built-in OS BIOS selection if possible.

    Please let me know if you have any additional questions, and I'm happy to help you further. Have a great day of Doug!

    Kind regards

    Casey Weltzin

    Product Manager, LabVIEW Real-time

    National Instruments

  • Select the partition, it will work?

    Hi, I have a TT_table partiotined:

    CREATE THE TABLE RYBATEMP. T_STAT
    (DATE OF THE ACTDATE,
    COL_LIST, FORMAT_LIST...
    .....)
    TABLESPACE "RYBA_DATA" PARTITION BY RANGE
    ("ACTDATE")
    (PARTITION 'D_20100901' VALUES LESS THAN (TO_DATE (' 2010-09-02 00:00:00 ',' SYYYY-MM-DD HH24:MI:SS ',' NLS_CALENDAR = GREGORIAN '))...)

    I just somehow check that the statement select refer directly wanted partition as expected is not browsing through all the table:

    SELECT COL_LIST FROM T_STAT WHERE
    ACTDATE > = to_date ('01 - SEPT.-10 00:00:00 "," DD-MON-YY HH24:MI:SS') AND
    ACTDATE < = to_date ('01 - SEPT.-10 21:01:16 ',' DD-MON-YY HH24:MI:SS')


    This time the date setting confuses me, even after reading through defintionis where format partition involves with hh. Is there way of anay follow in Plan or something, he never did on Oracle.
    It will take more/less/signs?


    Thank you
    Trent

    Hi Trent,
    Yes you can how things work with partitions in the plans of the explain command.
    I don't know your version, but some explanations in example 9.2:
    http://download.Oracle.com/docs/CD/B10500_01/server.920/a96533/ex_plan.htm#7211
    If you want to know how to have the complete trace etc (for example in point 10.2):
    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14211/SQLTrace.htm
    (Notice the partitioning does not avoid the need to index)
    Best regards
    Phil

  • Restore using the recovery disks on Inspiron 15 3521 supplied with Windows 8 Pro. Question on the recovery partition.

    I created by the user recovery disks.  I created them the day I got the computer and he made three of them.  My brother has deleted my recovery partition just now.  My question is, if I resumed my Inspiron 15 3521 using three recovery disks that I created, be it recreate the recovery partition in the process?   I just need an answer to this question, not responses indicating that, if I have the disks, I don't need the recovery partition, why would I want the recovery partition if I have the drive, etc.  I would like to just a simple answer to the question.  If I use the recovery discs, it will recreate the partition recovery with all the info, she was in it when I got to the computer?  Once the recovery is completed from disks, will be that recovery partition be there as the day I got the computer, so if I wanted to (I don't want to, but I'm just a question) I could still create the recovery of her discs?  Basically, the computer will be exactly as it was the day I got it home, recover partition and everything?  Or, it will restore the computer to the new plant, but not to re-create the recovery partition with everything she had in it the day I got it?

    Thanks in advance.

    Yes, it will be if you select the correct option.

    See these videos:

    http://en.community.Dell.com/support-forums/software-OS/w/microsoft_os/4794.21-Windows-8-restoring-from-recovery-media.aspx

    Recovery of factory - "deletes all data and resets the Partitions.

    Updating of the factory - "Only restores the System Partition and keep other Partition content".

    It might be good for you to ask a Windows OS DVD 8.0/8.1 just in case the recovery media works however.

Maybe you are looking for