Total amount and are rotating back in the same set of results

I want to return a set of results in the following format:
YEARMONTH Total ModelA ModelB ModelC
200101    0     0      0      0
200102    10    5      5      0
200103    8     2      2      4
where the total amount is the sum of the hours for all types of models grouped by yearmonth and columns of each model are the sum of hours per type of model grouped by yearmonth. I can get the correct results with the following query selects nested:
        select distinct yearmonth,
     sum(a.hours) as Total,
     (select sum(b.hours) from model_hours b
         where model = 'ModelA' and a.yearmonth = b.yearmonth) as ModelA,
        (select sum(b.hours) from model_hours b
         where model = 'ModelB' and a.yearmonth = b.yearmonth) as ModelB,
        (select sum(b.hours) from model_hours b
         where model = 'ModelC' and a.yearmonth = b.yearmonth) as ModelC
    from model_hours a
    group by yearmonth
    order by yearmonth
I was curious to try use the pivot function in Oracle 11 to get the same results and I am able to get all the results, EXCEPT the total number of hours by using the following query:
    select * from (
         select yearmonth, hours, model
         from model_hours a
    )
    pivot
    ( 
         sum(hours)
         for model in ('ModelA', 'ModelB', 'ModelC')
    )
    order by yearmonth
which returns this result:
YEARMONTH  ModelA ModelB ModelC
200101     0      0      0
200102     5      5      0
200103     2      2      4
I was not able to understand how to get the sum of hours for all models, grouped by yearmonth, in this result set also. Is this possible? And if yes, isn't - that's likely to be more effective than selects it nested? This particular table has some 200K lines right now.

Hello

As far as I know, the Oracle 11 SELECT... PIVOT function only works on mutually exclusive groups.
You can derive the total revolving data, like this:

WITH        pivoted_data          AS
(
     select  *
     from      (
                       select  yearmonth, hours, model
                from    model_hours a
              )
         pivot
          (
                  sum(hours)
                  for model in ( 'ModelA'     AS modela
                               , 'ModelB'     AS modelb
                     , 'ModelC'     AS modelc
                     )
              )
)
SELECT       yearmonth
,       modela + modelb + modelc     AS totla     -- may need chnaging if values can be NULL
,       modela, modelb, modec
FROM       pivoted_data
ORDER BY  yearmonth
;

Published by: Frank Kulash, 26 March 2012 15:01

Tags: Database

Similar Questions

  • Query to get the data of the column and the metadata in the same set of results.

    Is it possible to build a query to get the values of the columns in a table and also be able to get some metadata (data type, data_length, data_precision, data_scale) for columns in the same set of results.

    If I use a join, have a common value to join on the two tables?

    you use a cross join, not requiring common values.

    create table T (n number, d date, v varchar2(30));
    insert into T values (1,sysdate,'ABC');
    commit;
    
    select C.column_name, c.data_type, c.data_length,
    case c.column_id
     when 1 then to_char(T.N)
     when 2 then to_char(T.D)
     when 3 then T.V
    end VALUE
    from USER_TAB_COLUMNS C, T
    where C.table_name='T'
    order by c.column_id;
    
  • How to have an amount and cut in two at the same time

    Hi everyone, I need to display the data as in the example (4 areas):
    Product Category           Number of tickets by category               Product               Number of tickets by product
    Category 1                              100                            product A                             50
                                                                           product B                              50

    Category 2                               10                             product C                              8
                                                                           product D                               2
    As you can see, I need to see the total number of tickets for each category of product and then cut in two by product.

    In an attempt to have this result, I take the following fields the sector of activity:

    Product category
    Count (tickets_id)
    Product

    but I'm not able to have the amount of tickets per category and the quantity of tickets per product at the same time within the report... because as soon as I shared by product I lose the total number of tickets by category...

    How can I have this two amounts together? The first designating all of the notes of the category and the second split by product at the same time?

    I tried to do a calculation count (count (tickets_id))... but it is not possible... I got the error "Aggregate functions Nested are not permitted..."

    Any help will be appreciated

    Thanks in advance

    Alex

    Hi Alex,
    In cases like yours when you need two different levels of aggregation, you can use the analytical functions.
    From BA to select only the Product_Category elements and product and create new calculations:

    1. on behalf of the tickets:
    Count (tickets_id) more (Product_Category partition)
    2. for the number of tickets per product:
    Count (tickets_id) more (partition Product_Category, product)

    you will get:

    Product_category count by product by product category
    ---------------------------------------------------------------------------------------------------------------------------------
    1 100 A 50 category product
    Category 1 100 product B 50
    Category 2 10 C 8
    Category 2 10 D 2

    Now if you want you can use the sort and apply the sorting group so it will look like your table model.

    Note that I assumed that a product may be in a category.
    If you got another one then just change groups in the calculation according to the needs.

  • An affair hotmail, windows live and windows live Messenger with the same user id and password

    Hi, I need help I changed my hotmail password and it will open my homtail account and account window live messanger but it will not open and synchronize my windows live account.  Whenever I try to update asked my server id and password. I put in the same password for the hotmail/Messenger and it goes back to the same request. How can I fix it is there a way to retrieve the id and the password it asks

    Your message has noting to do with Windows Update, Michaela.

    Solutions Center Online hotmail all replaced the newsgroup & email format support 11-Dec-08
    http://windowslivehelp.com/default.aspx

    Solutions for common problems
       http://windowslivehelp.com/solutions/

    The hotmail community forums<=post all="" hotmail-specific="" questions="">
       http://windowslivehelp.com/community/default.aspx

    ~ Robear Dyer (PA Bear) ~ MS MVP (that is to say, mail, security, Windows & Update Services) since 2002 ~ WARNING: MS MVPs represent or work for Microsoft

  • When you try to create the new thread in a discussion forum, the browser closes and I'm back in the office.

    I tried to create a new thread in a Discussion forum.  Closing the browser window and I was back on the desktop.  I did a system restore, which not only does not solve the problem, but deleted my internet security software.  Has worked with people of tendency to reinstall the software, but I get a message tht says: «Unable to extract the installer...» ».  I have a class of the internet this week and I need this computer.  Can someone please?

    You can undo the last restoration of the system using: http://www.vistax64.com/tutorials/76256-system-restore-undo.html.  This may fix your problem with security software that has uninstalled when you did the system restore (using a date that probably existed before the installation of trend).  I don't know if it works now that you have done other things to try to reinstate the program, but it's worth a try.  I sincerely doubt that this process could help with the problem of browser (and there not so undo it reverses your efforts).

    You use Internet Explorer?  If so, please repost your question in Forum Internet Explorer: http://social.answers.microsoft.com/Forums/en-US/InternetExplorer/threads where the IE specialists will be more than happy to help you with your problem.

    If you are using another brand of browser, please repost your question in the Forum program: http://social.answers.microsoft.com/Forums/en-US/vistaprograms/threads where experts in programs other than Vista or IE will be more than happy to help you with your problem.  You can also try to go to the web site of the Explorer and contacting their technical support staff (or post on a forum if they support a).

    Only trend can help you with installation problems you encounter through their program.  Perhaps it didn't uninstall completely during the restoration of the system.  Check if the trend has a removal tool and use it to uninstall the program, then try to re - install again.  If this does not work, you will need to work with their technical support to solve the problem.

    I hope this helps.

    Good luck. Lorien - a - MCSE/MCSA/network + / A +.

  • While trying to look for something on my computer, it gives a blank screen and then start back on the desktop

    I use a laptop HP H250 running Windows 8.0. Since yesterday whenever I search something on the machine it gives a blank screen and then start back on the desktop. An analysis of the system and the registry has verified. Can anyone help?

    Hi Courtney,

    This issue may have caused due to some missing or corrupted files in the computer. I would like to know some information about this problem so that we can help you further.

    1. have you made changes on the computer before this problem?

    2. do you get any error code or error message while starting?

    3. are you referring to the Virus scan by "system scan"?

    I would suggest trying the following methods and check if it works for you.

    Method 1:

    Run the troubleshooter of System Maintenance and check if it helps. This troubleshooting will help you find and fix common system issues on your computer.

    Follow the steps to run the Troubleshooter:

    (a) press Windows + Q together get the search option.
    (b) the type of Troubleshooting on the search option, and then select Troubleshooting.
    (c) click show all on the left side panel.
    (d) click on The Maintenance of the system and follow the instructions to run the troubleshooter.

    Method 2:

    Put the computer in a clean boot state, and check if the problem persists. This process will help you determine which third party/PPP program is causing this problem.

    How to troubleshoot a problem by performing a clean boot in Windows Vista or in Windows 7 or Windows 8 / 8.1:
    http://support.Microsoft.com/kb/929135 
    Note: After the boot minimum troubleshooting step, see the "How to reset the computer to start as usual after a clean boot troubleshooting" section in the link provided to return the computer to a Normal startup mode.

    Please reply with the status of the issue so that we can better help you.

  • Everything was fine until my Windows 10, lost the start feature, if I ever go back to Windows 7, in this process, I lost my Adobe Reader software, I tried about ten times with different versions and different locations, all with the same download error

    Please forgive me for not knowing how to use this forum, I am an old man and not good at computers, I called Adobe, they said it was my only hope!

    Sorry to repeat

    Everything was fine until my Windows 10, lost the start feature, so I had to go back to Windows 7, in this process, I lost my Adobe Reader software, I tried about ten times with different versions and different locations, all with the same error message that is download "the feature you are trying to use is on an unavailable network resource"... are looking for It seems that it does not find when I search there, and I no longer seem to have the AcroRead.msi... the most difficult file, I try, I get deeper and deeper into things I don't know... I am looking for a simple solution!

    When it gets to this point, it is probably better to start from scratch.

    First of all, download, install and run Adobe Reader cleaning tool to get rid of all remains little. Here is a link to the tool: Download Adobe Reader and Acrobat tool - Adobe Labs

    Then go to the following link to download the full installer for the reader.

    https://get.Adobe.com/reader/Enterprise/

  • I have upgraded to Windows 10 64 Pro and all my apps Adobe has stopped working; I get the popular "MSVCP110.dll is missing" issue; I uninstalled everything, including the creative cloud, rebooted the PC and installed it back again! Same thing! I tried thi

    I have upgraded to Windows 10 64 Pro and all my apps Adobe has stopped working; I get the popular "MSVCP110.dll is missing" issue; I uninstalled everything, including the creative cloud, rebooted the PC and installed it back again! Same thing! I tried this several times. I'm really stuck! Can someone help with how this is solved; I follow online (two solutions) and tried both; not good! No matter what I do, I get the error! The rest of the applications say they are absent from the other files like this; It seems that there is something to do during the installation that keeps this issue alive!

    Run the command to check the file system once:

    https://support.Microsoft.com/en-in/KB/929833

    Also repair Microsoft Redistributable packages from the control panel > programs and features.

    If not installed, download and install the Visual C++ 2013 package.

    Let me know if you need more assistance.

  • When I edit a photo in "develop" and then click back to the library to export the only changes to the photo that carry over is the cropping. I can't export my edited photo. Anyone have this problem? I have the creative cloud package

    When I edit a photo in "develop" and then click back to the library to export the only changes to the photo that carry over is the cropping. I can't export my edited photo. Anyone have this problem? I have the creative cloud package

    Hi stever25146025,

    According to the request, I understand that when you export images from Lightroom it shows only the crops, is not editing.

    It is specific to a single image or all images in the catalog?

    Could you please let me know what file format you choose to export the images and also what version of Lightroom are you using?

    Kind regards

    Tanuj

  • If I do are on back to the PS from LR, use original edit, my LR are not displayed.  Has anyone seen this?

    If I do are on back to the PS from LR, use original edit, my LR are not displayed.  Has anyone seen this?

    1 edit an Original RAW in Photoshop file opens a new pixel (.tiff or .psd) image without any changes in LR.

    2 edit "with Lightroom publishes" opens a new image (.tiff, .psd) pixel with the LR changes preserved in the new file.

    3. change a psd or .tiff file Original in Photoshop (ie. re - open a Photoshop file) opens in Photoshop, exactly as you saved it.

    One problem! If you edit a .psd or .tiff file in Lightroom and then re - open in Photoshop and Lightroom flattens the image and Photoshop previous edition (layers, etc.) is everything HAPPENED!

    Suggest that all first edition lightroom then go into Photoshop for finishing. Consider un.pdf/.tiff your editing "final".

    Create virtual copies if you want to do more editing in Lightroom with Photoshop retouching.

    Big hint - Open images by 'Open as Smart Object in Photoshop' then you have the opportunity to re - open the layer Photoshop (background) (Smart Object) in ACR for more crude adjustment. It uses the ACR version related to Photoshop (not Lightroom)

  • Are the Lutron Caseta and comparable tinted Philips in the same House?

    Are the Lutron Caseta and comparable tinted Philips in the same House? I want to have some switches use the Lutron and some rooms use the tint. I would not mix them. Siri would work with name like office, bedroom etc. Virtual Babysitting would still work?

    Thanks for any help.

    MacAximX wrote:

    Are the Lutron Caseta and comparable tinted Philips in the same House?

    Your question is a little uncertain. Do you mean comparable or hear you compatible? Did you really ask yourself can you use two systems with HomeKit at the same time? I don't see why you wouldn't be able to as long as you use different names.

  • I use an analog input on a PCI-6224 and are having problems with the clock source

    I use an analog input on a PCI-6224 and are having problems with the clock source. I'm trying samples of 16 different analog inputs very quickly. I have the sample mode: Timed Single Point material. The rate, that I am running is the maximum (250 kHz (15625Hz per channel)). I left the default clock source and trying to taste several times. The analogue input works for a short time (2-3 seconds) and then everything stops. I'm doing something wrong or is there something I'm missing? Any advice would be great.

    That's how you samples using the sample clock clock. If you see a delay then something is wrong with how you track/data visualization.

    Single point NI the hardware is for PID control with a real-time operating system.

  • Windows XP 07 login I get the black and white screen where is safe mode option and then just loop it back to the same screen.

    When I powered on my Windows O.S. ' 07 I got the black and white screen where the option of safe mode and when I entered beginning that normally he just came back to the same screen of the suggestions?

    Hi RudyNY,

    1. you have Windows 7 or Windows XP installed on the computer?

    2. you receive an error during the process of loop?

    3. did you of recent changes on the computer?

    4 when was the last time it was working fine?

    Check to see if you can boot into safe mode in the Advanced menu options and the last good known Configuration.

    http://www.Microsoft.com/windowsxp/using/Setup/support/nostart.mspx

    A description of the options to start in Windows XP Mode

    http://support.Microsoft.com/kb/315222

  • I am trying to install a printer wifi (Kodak ESP 7250) and they are both connected to the same wifi but when I try to add the printer, my PC can't find it.

    I am trying to install a printer wifi (Kodak ESP 7250) and they are both connected to the same wifi but when I try to add the printer, my PC can't find it. I made sure they are both connected to my home network and when I go to control panel / printer and try to add a network printer / wireless. He won't find it. Any ideas?

    Hello

    I suggest you to visit these links and check if it helps:

    http://Windows.Microsoft.com/en-us/Windows-Vista/add-or-remove-a-printer

    http://Windows.Microsoft.com/en-us/Windows-Vista/troubleshoot-network-printer-problems

    It will be useful.

  • ScanJet 4890 light bar trips at the end of the scanning glass and not coming back in the beginning

    My ScanJet 4890 sometimes does not work properly.  In preview mode, the light bar will sometimes travel to the end of the scanner with light on and not go back.  The engine receives a sharp wail.  I have to turn the scanner and then turn the GET the light bar to return to the starting position.  Sometimes, it works correctly.    How can I fix my scanner?

    I found an unusual solution to my own problem.  I drilled a hole of the housing of the scanner.  Then when the light bar gets stuck, I push a pencil into the hold and kick the light bar.  The bar is moving on its own and returns to its storage position. So, I can continue without turning the scanner on/off.  I suspect that the engine needs to be replaced, I can do later if I can find one that suits.

Maybe you are looking for