GROUPING and CUMULATIVE: Why won't my ORDER BY?

Second part of the query in the next post.

I have a PTD report that displays totals for the store, district and
society. Each one has its own PDF page and I need to find a page id and a very specific sort order:
Must order page of the company, the totals of the region, then each
totals of the front center of its stores. The summary must be see the first.
I think it was my group that is causing the problem. I have attached the
request and the order you want. These are not duplicate records. This is the report of the first week of the year if this day week = period to date = year to date.


with  sample_data as
(
     select 22 storeid , 1 week_nbr, 15942 net_sales, 372 buns, 176 condiments  from dual union all
      select 22 storeid , 6 week_nbr, 15942 net_sales, 372 buns, 176 condiments  from dual union all
      select 22 storeid , 7 week_nbr, 15942 net_sales, 372 buns, 176 condiments  from dual union all
      select 23 storeid , 1 week_nbr, 25302 net_sales, 481 buns, 221 condiments  from dual union all
      select 23 storeid , 6 week_nbr, 25302 net_sales, 481 buns, 221 condiments  from dual union all
      select 23 storeid , 7 week_nbr, 25302 net_sales, 481 buns, 221 condiments  from dual union all
      select 24 storeid , 1 week_nbr, 29347 net_sales, 598 buns, 238 condiments  from dual union all
      select 24 storeid , 6 week_nbr, 29347 net_sales, 598 buns, 238 condiments  from dual union all
      select 24 storeid , 7 week_nbr, 29347 net_sales, 598 buns, 238 condiments  from dual union all
      select 25 storeid , 1 week_nbr, 17637 net_sales, 360 buns, 165 condiments  from dual union all
      select 25 storeid , 6 week_nbr, 17637 net_sales, 360 buns, 165 condiments  from dual union all
      select 25 storeid , 7 week_nbr, 17637 net_sales, 360 buns, 165 condiments  from dual union all
      select 27 storeid , 1 week_nbr, 22010 net_sales, 405 buns, 172 condiments  from dual union all
      select 27 storeid , 6 week_nbr, 22010 net_sales, 405 buns, 172 condiments  from dual union all
      select 27 storeid , 7 week_nbr, 22010 net_sales, 405 buns, 172 condiments  from dual union all
      select 31 storeid , 1 week_nbr, 16836 net_sales, 345 buns, 168 condiments  from dual union all
      select 31 storeid , 6 week_nbr, 16836 net_sales, 345 buns, 168 condiments  from dual union all
      select 31 storeid , 7 week_nbr, 16836 net_sales, 345 buns, 168 condiments  from dual union all
      select 38 storeid , 1 week_nbr, 28244 net_sales, 524 buns, 247 condiments  from dual union all
      select 38 storeid , 6 week_nbr, 28244 net_sales, 524 buns, 247 condiments  from dual union all
      select 38 storeid , 7 week_nbr, 28244 net_sales, 524 buns, 247 condiments  from dual union all
      select 39 storeid , 1 week_nbr, 21011 net_sales, 407 buns, 238 condiments  from dual union all
      select 39 storeid , 6 week_nbr, 21011 net_sales, 407 buns, 238 condiments  from dual union all
      select 39 storeid , 7 week_nbr, 21011 net_sales, 407 buns, 238 condiments  from dual union all
      select 41 storeid , 1 week_nbr, 18026 net_sales, 430 buns, 179 condiments  from dual union all
      select 41 storeid , 6 week_nbr, 18026 net_sales, 430 buns, 179 condiments  from dual union all
      select 41 storeid , 7 week_nbr, 18026 net_sales, 430 buns, 179 condiments  from dual union all
      select 42 storeid , 1 week_nbr, 24821 net_sales, 466 buns, 212 condiments  from dual union all
      select 42 storeid , 6 week_nbr, 24821 net_sales, 466 buns, 212 condiments  from dual union all
      select 42 storeid , 7 week_nbr, 24821 net_sales, 466 buns, 212 condiments  from dual union all
      select 65 storeid , 1 week_nbr, 13356 net_sales, 281 buns, 136 condiments  from dual union all
      select 65 storeid , 6 week_nbr, 13356 net_sales, 281 buns, 136 condiments  from dual union all
      select 65 storeid , 7 week_nbr, 13356 net_sales, 281 buns, 136 condiments  from dual union all
      select 66 storeid , 1 week_nbr, 15421 net_sales, 337 buns, 155 condiments  from dual union all
      select 66 storeid , 6 week_nbr, 15421 net_sales, 337 buns, 155 condiments  from dual union all
      select 66 storeid , 7 week_nbr, 15421 net_sales, 337 buns, 155 condiments  from dual union all
      select 67 storeid , 1 week_nbr, 28064 net_sales, 625 buns, 283 condiments  from dual union all
      select 67 storeid , 6 week_nbr, 28064 net_sales, 625 buns, 283 condiments  from dual union all
      select 67 storeid , 7 week_nbr, 28064 net_sales, 625 buns, 283 condiments  from dual union all
      select 68 storeid , 1 week_nbr, 22875 net_sales, 493 buns, 238 condiments  from dual union all
      select 68 storeid , 6 week_nbr, 22875 net_sales, 493 buns, 238 condiments  from dual union all
      select 68 storeid , 7 week_nbr, 22875 net_sales, 493 buns, 238 condiments  from dual union all
      select 70 storeid , 1 week_nbr, 26434 net_sales, 562 buns, 248 condiments  from dual union all
      select 70 storeid , 6 week_nbr, 26434 net_sales, 562 buns, 248 condiments  from dual union all
      select 70 storeid , 7 week_nbr, 26434 net_sales, 562 buns, 248 condiments  from dual union all
      select 71 storeid , 1 week_nbr, 14259 net_sales, 297 buns, 133 condiments  from dual union all
      select 71 storeid , 6 week_nbr, 14259 net_sales, 297 buns, 133 condiments  from dual union all
      select 71 storeid , 7 week_nbr, 14259 net_sales, 297 buns, 133 condiments  from dual union all
      select 82 storeid , 1 week_nbr, 24446 net_sales, 469 buns, 210 condiments  from dual union all
      select 82 storeid , 6 week_nbr, 24446 net_sales, 469 buns, 210 condiments  from dual union all
      select 82 storeid , 7 week_nbr, 24446 net_sales, 469 buns, 210 condiments  from dual union all
      select 83 storeid , 1 week_nbr, 13959 net_sales, 280 buns, 104 condiments  from dual union all
      select 83 storeid , 6 week_nbr, 13959 net_sales, 280 buns, 104 condiments  from dual union all
      select 83 storeid , 7 week_nbr, 13959 net_sales, 280 buns, 104 condiments  from dual union all
      select 181 storeid , 1 week_nbr, 13140 net_sales, 273 buns, 136 condiments  from dual union all
      select 181 storeid , 6 week_nbr, 13140 net_sales, 273 buns, 136 condiments  from dual union all
      select 181 storeid , 7 week_nbr, 13140 net_sales, 273 buns, 136 condiments  from dual union all
      select 221 storeid , 1 week_nbr, 27347 net_sales, 546 buns, 289 condiments  from dual union all
      select 221 storeid , 6 week_nbr, 27347 net_sales, 546 buns, 289 condiments  from dual union all
      select 221 storeid , 7 week_nbr, 27347 net_sales, 546 buns, 289 condiments  from dual union all
      select 222 storeid , 1 week_nbr, 16456 net_sales, 379 buns, 148 condiments  from dual union all
      select 222 storeid , 6 week_nbr, 16456 net_sales, 379 buns, 148 condiments  from dual union all
      select 222 storeid , 7 week_nbr, 16456 net_sales, 379 buns, 148 condiments  from dual union all
      select 223 storeid , 1 week_nbr, 20611 net_sales, 439 buns, 165 condiments  from dual union all
      select 223 storeid , 6 week_nbr, 20611 net_sales, 439 buns, 165 condiments  from dual union all
      select 223 storeid , 7 week_nbr, 20611 net_sales, 439 buns, 165 condiments  from dual union all
      select 224 storeid , 1 week_nbr, 21537 net_sales, 420 buns, 173 condiments  from dual union all
      select 224 storeid , 6 week_nbr, 21537 net_sales, 420 buns, 173 condiments  from dual union all
      select 224 storeid , 7 week_nbr, 21537 net_sales, 420 buns, 173 condiments  from dual union all
      select 260 storeid , 1 week_nbr, 19329 net_sales, 380 buns, 196 condiments  from dual union all
      select 260 storeid , 6 week_nbr, 19329 net_sales, 380 buns, 196 condiments  from dual union all
      select 260 storeid , 7 week_nbr, 19329 net_sales, 380 buns, 196 condiments  from dual union all
      select 280 storeid , 1 week_nbr, 20692 net_sales, 512 buns, 202 condiments  from dual union all
      select 280 storeid , 6 week_nbr, 20692 net_sales, 512 buns, 202 condiments  from dual union all
      select 280 storeid , 7 week_nbr, 20692 net_sales, 512 buns, 202 condiments  from dual union all
      select 294 storeid , 1 week_nbr, 26522 net_sales, 481 buns, 252 condiments  from dual union all
      select 294 storeid , 6 week_nbr, 26522 net_sales, 481 buns, 252 condiments  from dual union all
      select 294 storeid , 7 week_nbr, 26522 net_sales, 481 buns, 252 condiments  from dual union all
      select 362 storeid , 1 week_nbr, 20611 net_sales, 317 buns, 221 condiments  from dual union all
      select 362 storeid , 6 week_nbr, 20611 net_sales, 317 buns, 221 condiments  from dual union all
      select 362 storeid , 7 week_nbr, 20611 net_sales, 317 buns, 221 condiments  from dual union all
      select 501 storeid , 1 week_nbr, 28337 net_sales, 518 buns, 273 condiments  from dual union all
      select 501 storeid , 6 week_nbr, 28337 net_sales, 518 buns, 273 condiments  from dual union all
      select 501 storeid , 7 week_nbr, 28337 net_sales, 518 buns, 273 condiments  from dual union all
      select 521 storeid , 1 week_nbr, 26118 net_sales, 438 buns, 257 condiments  from dual union all
      select 521 storeid , 6 week_nbr, 26118 net_sales, 438 buns, 257 condiments  from dual union all
      select 521 storeid , 7 week_nbr, 26118 net_sales, 438 buns, 257 condiments  from dual union all
      select 524 storeid , 1 week_nbr, 31929 net_sales, 582 buns, 247 condiments  from dual union all
      select 524 storeid , 6 week_nbr, 31929 net_sales, 582 buns, 247 condiments  from dual union all
      select 524 storeid , 7 week_nbr, 31929 net_sales, 582 buns, 247 condiments  from dual)
Published by: user8875305 on January 26, 2010 19:13

Published by: user8875305 on January 26, 2010 20:25
Published without duplicates.

Hello

Is it?

select decode(gc,0,companyname, 'VanderbiltFoods') as companyname,
decode(gr,0,regionname,decode(gc,0,companyname, 'VanderbiltFoods')) as regionname,
decode(gd,0,districtname,decode(gr,0,regionname,decode(gc,0,companyname, 'VanderbiltFoods'))) as districtname,
decode(gs,0,storenbr,decode(gd,0,districtname,decode(gr,0,regionname,decode(gc,0,companyname, 'VanderbiltFoods')))) as storenbr,
decode(gs,0,storename,decode(gd,0,districtname,decode(gr,0,regionname,decode(gc,0,companyname, 'VanderbiltFoods')))) as storename,
net_sales,
buns,
condiments
from(
select     companyname,
grouping(companyname) gc
,        regionname
,grouping(regionname) gr
,          districtname
,grouping(districtname) gd,
        storenbr
,grouping(storenbr) gs,
max(storename) storename
,        sum(net_sales)         net_sales
,        sum(buns)              buns
,        sum(condiments)        condiments
from store_data  stdata
inner join sample_data   sampdata on sampdata.storeid  = stdata.storeid
group by   rollup(companyname, regionname, districtname, storenbr), week_nbr
order by   companyname nulls first,gc desc, regionname nulls first, gr desc, districtname nulls first, gd desc,storenbr nulls first, gs desc)

Support is you don't have a problem with the data, it is only just that group by creates a problem for you.

Concerning
Anurag

Tags: Database

Similar Questions

  • Why would I get a message "the application"Safari"is no longer open, and then he won't open?  Even a search on Fosfire allowed them to prevent me from using Safari?

    End of 2013 iMac 2.7 GHZ Intel Core 8 GB memory 2nx992F8J2 running OSX El Capitan 10.11.1 SN.   Why would I get a message "the application"Safari"is no longer open, and then he won't open?  I went online with Fire Fox and I continue to request now if I want Morel Fire Fox my primary.   Could they have blocked my Safari?  It is difficult to check on it when it won't even developed.

    I answer 'Lestoile.

    Who are "they".

  • Why does it fail to Lr and Ps download and install on my desktop? (Order # C5000162754)

    Why does it fail to Lr and Ps download and install on my desktop? (Order # C5000162754)

    Why does it fail to Lr and Ps download and install on my desktop? (Order # C5000162754)

  • Why Firefox keep save my last sites traveled as a group and then open this group when I start next time? It will not stop, and I get really mad

    Every time I close Firefox and I have a tab open, it records the tab as if I had recorded as a group and it opens the next time I start firefox again.

    You can check the startup parameter:

    • Tools > Options > general > startup: 'When Firefox starts': 'Show my home page' "show a blank page.
  • Why won't my iPhone 5s 9.3.5 update of the new iOS10?

    Why won't my iPhone 5s 9.3.5 update of the new iOS10?

    Hello Kevin Bouah,
    Thank you for using communities of Apple Support.

    I understand your message that you were not able to update your iPhone to iOS 10 5s. I know how exciting it is when a new version of the iPhone software will be released. If you tried to update your iPhone via Wi - Fi under settings > general > Software Update and cannot update your iPhone, I recommend that you use iTunes on a computer. Make sure that your computer software and iTunes are updated to the latest versions. The following steps will help you to upgrade your software to the iPhone via iTunes on your computer:

    Update the software on your iPhone, iPad or iPod touch iOS

    Update your device via iTunes

    If you are unable to update wireless on your iOS device, you can manually update using iTunes on a computer on which you trust. If your computer uses Personal Hotspot on the iOS device that you update, connect your computer to another Wi - Fi or Ethernet network before update you.

    1. Install the latest version of iTunes on your computer.
    2. Connect your device to your computer.
    3. Open iTunes, Select your device.
    4. Click summary, and then click Find updates.
    5. Click on download and update.
    6. If requested, enter your access code.

    Best regards

  • Device - Why won't my ipad connect/load from my pc?

    Why won't my ipad connect/load from my pc?

    Hello

    1. What is the brand and model of the iPad?

    2. what exactly happens when you connect to iPad?

    3. you get any error message?

    4. what operating system is installed on the computer?

    5. don't you make changes to the computer until the problem occurred?

    I suggest that you try to run the Fixit tool from the link below.

    Hardware devices do not work or are not detected in Windows:
    http://support.Microsoft.com/GP/hardware_device_problems

    Hope this information is useful.

  • Why won't install update 1.1

    Why won't install update 1.1

    Hi kdgrayson,

    Still having the problem you reported initially with the installation of the update? If you are updating for .net Framework 1.1, follow the steps in this document to solve the problem. If the problem is with another update, please provide the full name of the update and error messages only when the update fails.

  • Is there a phone # I can call to get real help? I never had trouble answering ads on Craigslist so far. Why won't he answer me?

    Is there a phone # I can call to get real help? I never had trouble answering ads on Craigslist so far. Why won't he answer me?

    It should be easier than that to send and receive e-mails via craigslist.

    Hello

    1. did you of recent changes to the system?

    2. what happens when you try to add?

    3. do you get an error message?

    4. what Web browser do you use?

    Please provide more information on the issue to get help.

    Also, I suggest to see the link below and contact the Support of Craigslist for help.

    http://stlouis.craigslist.org/

    https://forums.craigslist.org/?forumid=9

    http://www.craigslist.org/about/Help/FAQ

  • (Redirected) Why not split the order?

    I have a Dell coming 8 Pro and Dell Inspiron 14 ''. I ordered the Dell keyboard and folio combo and a dell for laptop carrying pouch. According to the website of command, expected delivery is one month! It is 5/9 and waits for 6/12. To continue the research, is the sheath that sits on the long time. The problem is that the keyboard combo is in the same number, but available now. I'm more interested in the keyboard combo. Why were they not these two separate orders? I also bought a screen protector of 3 m for the Conference on the same purchase ID and who is separated. So I have or the order can be changed? Unfortunately, it is as "in production" stage.

    Best of this post in the Customer Care Forum, here:

    http://en.community.Dell.com/support-forums/customercare/f/4674.aspx

    Bev.

  • unexpected results of the Group and the merger of processor

    Hi team,

    I use a processor of group and the merger in a process that includes "Store-num" and the "Cust-num" and mergers based on the most common value.

    I have few records store-Num = 42 and a few records store-num = 426. But for the same "Cust-num" for these two stores, store-num ' 426' we replace "42"

    My point is that these documents should not have grouped at all.

    No idea why this happen?

    Kind regards

    Ravi

    Yes, that's correct. Dates will be converted to a string representation of the date.

  • Why won't my animation?

    In Captivate 7, I have an animation with audio that I want to only appear when the user clicks the click box. I have the chronology paused until the user clicks and a tip action that says show the animation and play the audio. But when I saw, the audio playback, but not animation. I tried with the box click pause before the animation and the click one box break later, nothing works. I got it work but I changed the image and now it won't play properly. What I am doing wrong? Also, I can't go to the next slide now. Help!

    Why not just put continue in box click action? If the animation is visible in the output, it will play when the beginning of the timeline is reached, what will happen with continue. A tip action does any move the playback head, this is the reason why the animation is not playing. Considering that the audio will play. Add the "Continue" statement for your action.  You have even hide the animation at all, just start at the point of suspension of the click box.

  • Why won t the last reader to read the form?

    Why won t read this registration form WILL, ((( https://www.1010ez.med.va.gov/sec/vha/1010ez/Form/1010ez.pdf ))) site WILL continues to tell me that my read ADOBE is obsolete, and I the last installed?

    In fact, I have to disagree with Colin Holgate. The file seems file. It's a very clever and very complicated form.

    The problem is, and you are one (or two) countless who touched it: it is not enough to have installed the Adobe Reader. You must also USE it.

    This message is typical, if you use a browser that reads PDF files without using the Adobe Reader software. Most browsers now cela, then save the PDF file on your computer and open in Adobe Reader (Acrobat Reader) from there.

  • Could someone explain Radio button groups and how to identity, the individual borders in the name of the Group?

    I guess that there is a way to declare the name of the Group and then to identify the boxes of individual option within the group that way out put or "choice radio button: text that has been entered and is different from all the other within the same radio button group...»

    If you could point right to a tutorial that can help would be nice...

    Yes, myRadio is the name of the group. To get the correct number, which is determined by the order in which the individual radio buttons have been created (which can be different from their position, ordered on the page), you may need to use a bit of trial and error. The first is number 0 and you would increment from one to refer to the following.

  • Why won't photoshop elements download for me

    Why won't photoshop elements download for me

    clear your adobe.com cookies or

    Try another browser or

    Download from prodesigntools.com, if you follow the 7 steps: http://prodesigntools.com/tag/ddl

    and activate it with your serial number.

    If you have a problem dl'g, you don't follow all 7 steps.  the most common mistake is not meticulously follow the steps 1, 2, and/or 3 (which adds a cookie to your system, allowing you to download the correct version of the adobe.com site).

  • SQL grouping and summing impossible?

    I want to create a sql query to summarize some data, but I'm starting to think that it is impossible with sql only. The data I have is of the following form:
    TRAN_DT     TRAN_RS     DEBT     CRED
    10-Jan     701     100     0
    20-Jan     701     150     0
    21-Jan     701     250     0
    22-Jan     705     0     500
    23-Jan     571     100     0
    24-Jan     571     50     0
    25-Jan     701     50     0
    26-Jan     701     20     0
    27-Jan     705     0     300
    The data are classified by TRAN_DT and by TRAN_RS. THA grouping and summing data based on tran_rs, but only when it changes. If in the table above, I don't want to see all the first 3 records but only one value DEBT the sum of these 3 i.e. 100 + 150 + 250 = 500. If the table above after grouping would be similar to that below:
    TRAN_DT     TRAN_RS     DEBT     CRED
    21-Jan     701     500     0
    22-Jan     705     0     500
    24-Jan     571     150     0
    26-Jan     701     70     0
    27-Jan     705     0     300
    The TRAN_DT is the last value of the record sum. I undestand that the tran_dt are not selectable. What I've tried so far is the following query:
    select tran_dt,
             tran_rs,
             sum(debt)over(partition by tran_rs order by tran_dt rows unbounded preceding),
             sum(cred)over(partition by tran_rs order by tran_dt rows unbounded preceding) from that_table
    Is it still possible with only sql, any thoughts?

    The report that I am creating in BI Publisher.Maybe it is possible to group the data in the model and my question here?

    The Re: tutorial method Tabibitosan by Aketi Jyuuzou me would be very useful here:

    with sample_data as (select to_date('10/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 100 debt, 0 cred from dual union all
                         select to_date('20/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 150 debt, 0 cred from dual union all
                         select to_date('21/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 250 debt, 0 cred from dual union all
                         select to_date('22/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 500 cred from dual union all
                         select to_date('23/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 100 debt, 0 cred from dual union all
                         select to_date('24/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 50 debt, 0 cred from dual union all
                         select to_date('25/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 50 debt, 0 cred from dual union all
                         select to_date('26/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 20 debt, 0 cred from dual union all
                         select to_date('27/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 300 cred from dual),
         tabibitosan as (select tran_dt,
                                tran_rs,
                                debt,
                                cred,
                                dense_rank() over (order by tran_dt, tran_rs, rownum)
                                  - dense_rank() over (partition by tran_rs order by tran_dt, rownum) grp
                         from   sample_data)
    select max(tran_dt),
           tran_rs,
           sum(debt) debt,
           sum(cred) cred
    from   tabibitosan
    group by tran_rs,
             grp
    order by 1, 2;
    
    TRAN_DT       TRAN_RS       DEBT       CRED
    ---------- ---------- ---------- ----------
    21/01/2012        701        500          0
    22/01/2012        705          0        500
    24/01/2012        571        150          0
    26/01/2012        701         70          0
    27/01/2012        705          0        300
    

    ETA: I added the rownum in analytical functions to take account of the "encounters" where a tran_rs has multiple entries for the same tran_dt. If you have some other unique key to use (for example, the primary key of the table) then I would use instead.

    Published by: Boneist on February 17, 2012 11:41

Maybe you are looking for

  • My Satellite M50 does not start

    Hello All of a sudden my M50 does not start!He spends nothing; When I press the Start button, it flashes a few seconds and that s all! No noise, no BIOS! I already tried with the plug on the battery, but no change at all! I found a similar question o

  • Java isn't eenabled

    I just downloaded the new Firefox 4.0 and now my Java does not. I get a message to him. How can I do this? That's happened? It worked fine before downloading.

  • Satellite M60-162: the system hangs after connection of SD card

    I at all, I have a problem when I insert an SD card in my toshiba, the system freezes. I try to install the drivers downloaded from the Toshiba site but the problem remains. The operating system is windows xp pro sp2. Any ideas?

  • Even to day whenever I have logoff

    Whenever I have logoff, install the same 2 updates.  When I install from the toolbar, I get the following messages: "Update for Microsoft XML core services 4.0 service pack 2 KB 973688. and "Security update for Microsoft XL core services 4.0 service

  • 4630 all-in-one: offline?

    When I try to print it says I am offline and will not print. It stores all the work. My scan works as well as my wireless. How can I get the printer to say online or solve the problem, if this is not the case. Thank you.