GROUP BY - Is there a way to have some sort of for-each statement?

Hi there,
This discussion is a branch from https://forums.oracle.com/thread/2614679
I data mart I created for a chain of theatres. The fact table contain information about ticket sales, and I have a some dimensions including DimClient and DimTime.
Here is an example of each table:
FactTicketPurchase
TICKETPURCHASEID
CLIENTID
PRODUCTIONID
THEATREID
TIMEID
TROWID
SUMTOTALAMOUNT
60006
2527
66
21
942
40
7
60007
2527
72
21
988
36
6
60008
2527
74
21
1001
40
6
60009
2527
76
21
1015
37
6
60010
2527
79
21
1037
39
6
DDL for FactTicketPurchase
CREATE TABLE FactTicketPurchase(
TicketPurchaseID NUMBER(10) PRIMARY KEY,
ClientID NUMBER(5) CONSTRAINT fk_client REFERENCES DimClient,
-- ProductionID NUMBER(5) CONSTRAINT fk_prod REFERENCES DimProduction,
-- TheatreID NUMBER(5) CONSTRAINT fk_theatre REFERENCES DimTheatre,
TimeID NUMBER(6) CONSTRAINT fk_time REFERENCES DimTime,
-- TRowID NUMBER(5) CONSTRAINT fk_trow REFERENCES DimTRow,
SumTotalAmount NUMBER(22) NOT NULL);
DimClient
CLIENTID
CLIENT#
NAME
TOWN
COUNTY
2503
1
LEE M1
West Bridgford
Nottingham
2504
2
HELEN W2
Hyson Green
Nottingham
2505
3
LEE M3
Lenton Abbey
Nottingham
2506
4
LORA W4
Beeston
Nottingham
2507
5
SCOTT M5
Radford
Nottingham
2508
6
MINA W6
Hyson Green
Nottingham
    ..cff.
DDL for DimClient
CREATE TABLE DimClient(
ClientID NUMBER(5) PRIMARY KEY,
Name VARCHAR2(30) NOT NULL);
DimTime
TIMEID
FULLDATE
YEAR
SEASON
MONTH
MONTHDAY
WEEK
WEEKDAY
817
02-MAR-10
2010
Spring
3
2
9
3
818
03-MAR-10
2010
Spring
3
3
9
4
819
04-MAR-10
2010
Spring
3
4
9
5
820
05-MAR-10
2010
Spring
3
5
9
6
821
06-MAR-10
2010
Spring
3
6
9
7
822
07-MAR-10
2010
Spring
3
7
9
1
DDL for DimTime
CREATE TABLE DimTime(
TimeID NUMBER(6) PRIMARY KEY,
Year NUMBER(4) NOT NULL,
Season VARCHAR2(20));
I have the following analysis request to perform on this data mart:
Top 5 clients by value of ticket sale for each season
For this requirement I came up with the following query:
SELECT * FROM
(SELECT FacTIC.ClientID, DimCLI.Name, SUM(SumtotalAmount) SumTotalAmount, DimTIM.Season
FROM FactTicketPurchase FacTIC, DimClient DimCLI, DimTime DimTIM
WHERE FacTIC.ClientID = DimCLI.ClientID
AND FacTIC.TimeID = DimTIM.TimeID
AND Season = 'Spring'  AND Year = 2010
GROUP BY Season, FacTIC.ClientID, DimCLI.Name
ORDER BY Season ASC, SumTotalAmount DESC)
WHERE rownum <=5;
As you can see, in line 06 of the above query, I am explicitly specifying the season for the query to return.
However what I would like to do is just one query that could autocratically go through the seasons and years available in the time dimension in a fashion similar to a FOR-EACH statement. This way, if we get more years added to the time dimension, we wouldn't have to amend the query.
Is this possible?
Regards,
P.

I think I fixed it!
The trick was to look into the r_num value. As soon as I added it to my query I started to see how r_num was being calculated and I realised that I had to add Season to my partition, right after Year.
SELECT Year, Season, TotalAmount, Name
FROM (
   SELECT   DimCLI.Name
   ,        DimTIM.Year
   ,        DIMTIM.Season
   ,        SUM(FacTIC.SumTotalAmount) TotalAmount
   ,        RANK() OVER (PARTITION BY Year, Season
                         ORDER BY SUM(FacTIC.SumTotalAmount) DESC
                        ) AS r_num
   FROM     FactTicketPurchase FacTIC
   ,        DimClient DimCLI
  ,         DimTime DimTIM
   WHERE    FacTIC.ClientID = DimCLI.ClientID
   AND      FacTIC.TimeID = DimTIM.TimeID
   GROUP BY DimTIM.Year
   ,        DimTIM.Season
   ,        DimCLI.Name
WHERE r_num <= 5 -- Need to amend this line on my data sample to show 2 rows.
ORDER BY Year, Season, TotalAmount DESC;
Looking at my data sample, I got the following:
YEAR
SEASON
TOTALAMOUNT
CLIENTID
2010
Autumn
29
2504
2010
Autumn
26
2503
2010
Spring
25
2503
2010
Spring
14
2506
2010
Summer
26
2506
2010
Summer
26
2504
2010
Winter
28
2503
2010
Winter
26
2506
2011
Autumn
23
2506
2011
Autumn
14
2503
2011
Spring
25
2505
2011
Spring
13
2503
2011
Summer
21
2505
2011
Summer
14
2503
2011
Winter
19
2505
Now, looking at my real data, (considering the top 5 rows, not the top 2), I got:
YEAR
SEASON
TOTALAMOUNT
NAME
2010
Autumn
141
BUSH M225
2010
Autumn
140
DIANA W66
2010
Autumn
136
HANA W232
2010
Autumn
120
DIANA W220
2010
Autumn
120
WILSON M459
2010
Spring
137
DAVID M469
2010
Spring
125
ALEX M125
2010
Spring
124
PETER M269
2010
Spring
115
ZHOU M463
2010
Spring
114
TANIA W304
2010
Summer
138
JANE W404
2010
Summer
105
MINA W8
2010
Summer
97
DAVID M275
2010
Summer
96
CLINTON M483
2010
Summer
93
ANNA W288
2011
Spring
12
LUISE W20
2011
Spring
7
ANNA W432
2011
Spring
7
LEE M409
2011
Spring
7
CHRIS W274
2011
Spring
7
HELEN W136
2011
Spring
7
LILY W114
2011
Spring
7
LUISE W348
2011
Spring
7
LIU M107
2011
Spring
7
VICTORY W194
2011
Spring
7
DIANA W240
2011
Spring
7
HELEN W120
2011
Spring
7
LILY W296
2011
Spring
7
MATTHEW M389
2011
Spring
7
PACO M343
2011
Spring
7
YANG M411
2011
Spring
7
ERIC M101
2011
Spring
7
ALEX M181
2011
Spring
7
SMITH M289
2011
Spring
7
DIANA W360
2011
Spring
7
MATTHEW M63
2011
Spring
7
SALLY W170
2011
Spring
7
JENNY W258
2011
Spring
7

Similar Messages

  • Is there a way to have some sort of stereo mix audio input for mac?

    Is there a way to have some sort of stereo mix audio input for mac? also so i can have a voice over?

    polyglotinc wrote:
    Actually, after scouring the web, I see that RogueAmoeba also has a free utility called LineIn which simply sends the audio in to the audio out and doesn't interfere with other programs like iTunes!
    On the other hand, after downloading and trying it, there is a noticeable latency between what goes in and what comes out.
    The latency is in the computer, not the software. Most of the latency is in the A/D and D/A conversion.
    You need to connect the analog input to the analog output directly to get zero latency.
    Check this page:
    http://forums.macrumors.com/showthread.php?t=368834

  • My family shares one apple id. Is there any way to make an apple id for each member of my family and have it all connected to the same credit card and have the same previous downloads open to download by signing in to each members personal apple id?

    My family shares one apple id. Is there any way to make an apple id for each member of my family and have it all connected to the same credit card and have the same previous downloads open to download by signing in to each members personal apple id?

    Hi cindy,
    If you all have individual Apple IDs, then you would be able to dowload music from a shared iTunes library on a computer, but you would not be able to sign on each individual ID on an iOS device in order to download music purchased under the original Apple ID without a 90-day waiting period between sign-ons.
    Here's what I mean:
    If you were to sign on a new Apple ID on, say, an iPhone, and then were to purchase and download some music from iTunes, then you would have to wait 90 days before you could sign on the old Apple ID to that iPhone in order to download previously purchased material (and vice versa).
    However, if all of the music purchased by that origjnal Apple ID is in an iTunes library on a shared computer, then you can hook up the device and selectively sync any music in that library to the device. You can also sign on any of the Apple IDs to that shared iTunes library and purchase music to be added to the library.
    You would need to authorize the computer for each of the new Apple IDs.
    Each of the Apple IDs would also be able to download that music purchased either by syncing to the iTunes library on the computer or by downloading it OTA on the device.
    So, each Apple ID can download music, movies, etc. purchased with that Apple ID OTA to the device. Also each Apple ID can sync any music, movies, etc. that are in the iTunes library on the computer regardless of who purchased them.
    The only other thing you would need to remember is that if an App needed to be updated, then the original Apple ID that was used to purchase that App would come up, and the password that goes with that Apple ID would be required to do the update.
    Hope that helps,
    GB

  • Can anyone help or answer this question for me. Is there a way to have a private album for pictures sent to my phone? Don't exactly want kids ( niece and nephew ) to play on my phone and see private pictures of my girlfriend

    Can anyone help or answer this question for me. Is there a way to have a private album for pictures sent to my phone? Don't exactly want kids ( niece and nephew ) to play on my phone and see private pictures of my girlfriend

    What about this?
    https://itunes.apple.com/ru/app/best-secret-folder/id488030828?l=en&mt=8

  • Is there a way to have two different iTunes for two different iPhones on one Windows User account?

    Using Windows 7, iphone 4.
    I've read as many threads on these forums and as many kb's as I could find, but I sttill can't figure it out. I have 2 iPhones. Let's call them, "wife" and "hubby". I want to have two separate iTunes, one for each of the phones. Hubby is the Windoows User. I don't want to set up a different Windows User for wife.
    Hubby was the original iPhone. When I plug it in, iTunes recognized it has Hubby's iPhone. Then I unplug Hubby and close iTunes. I plug in Wife but iTunes stll comes up with Hubby under Devices and syncs Hubby's iTunes to Wife's iphone.
    I've tried using the shift key when I open iTunes to set up two separate .itl files to no avail.
    Is there a way to have two different iTunes for two different iPhones on one Windows User account?
    Thanks!

    What happens if you rename the iPhone "Wife" when it shows up as "Hubby", then choose a different set of apps/playlists to sync with? iTunes is supposed to be able to manage multiple devices, each with their own selection of content. Perhaps you "restored" "Wife" with a backup from "Hubby" when you first set things up which is now causing confusion.
    To create a new library press and hold down the shift key as you click the icon to start iTunes and keep holding shift until asked to choose or create a library. You need to use the same technique to switch back.
    tt2

  • I installed iCloud on to a pc.  The shorts cuts did not include notes.  Is there a way to have a short cut for notes or do I need to logon to icloud every time to use notes on my pc?

    I  installed iCloud on my pc and the short cuts did not include notes.  Is there a way to create a short cut for notes or do I have to logon to iCloud every time I use notes on my pc?

    Open the Mac App Store on your MacBook Pro
    Hold the Option key and click on “Purchases”
    Hold the Option key and click on “OS X Lion” from the purchased app list
    “Installed’ should now say “Install” which allows you to download OS X Lion.
    This assumes you are using the same Apple ID on both computers.
    Lion does not get installed on an iPad. It runs iOS.

  • Is there a way around having to enter password for each iOs app

    I'm getting back into experiment/deploy iOS apps from the App store using ZMM.
    Is there a way around the user having to enter the password for each app in the same session.
    I guess what I am saying is that once the apple password is entered for the first app when the user turns the ipad on, they seem to have to enter it for each app in quick succession eg 20 apps, then 20 passwords. Is there a setting or way around that? It seems kind of silly or is it that I'm dumb
    thanks

    Originally Posted by Thomas Roberts
    I'm getting back into experiment/deploy iOS apps from the App store using ZMM.
    Is there a way around the user having to enter the password for each app in the same session.
    I guess what I am saying is that once the apple password is entered for the first app when the user turns the ipad on, they seem to have to enter it for each app in quick succession eg 20 apps, then 20 passwords. Is there a setting or way around that? It seems kind of silly or is it that I'm dumb
    thanks
    There is not AFAIK, if you find one then please let us know But I do recal that if you enter the password the first time then cancel the rest of the popups they should get installed anyway but I'm not 100% sure, my memory may fail at times
    Thomas

  • Is there a way to have a homepage set for an "App Tab"?

    Lets say I'm using facebook on an App Tab and I click on a link and it takes me away from facebook (or maybe it just takes me away from the home facebook page that I had originally set as an App Tab). When I restart firefox the page that shows up is the last page I visited on that tab. Is there any way to set a "homepage" for each "App Tab" so that whenever I restart firefox the original page I had intended to be there is actually there?

    Hi,
    Within a country, you can have different screens as per the different employee group, subgroup, personnel area, subarea etc. You can use further decision fields after MOLGA. Feature P0002 has PME04 structure and it offers more than just MOLGA as the decision field:
    BUKRS     Company Code
    WERKS     Personnel Area
    BTRTL     Personnel Subarea
    PERSG     Employee Group
    PERSK     Employee Subgroup
    ITBLD     Infotype Screen Control
    MOLGA     Country Grouping
    TCLAS     Transaction Class for Data Retention
    With that said, if you just use MOLGA, you can only have one screen. For more than one, further divide MOLGA as per other fields mentioned above.
    Hope this helps.
    Donnie

  • Is there a way to have a search form for the CRM database?

    I know there is a search form provided for the Web Apps. I can't seem to find how to create a search form for the CRM database. I have created a membership site, and also extended the CRM database with additional fields. I would like the users to be able to search on selected fields in the CRM database including the extended database fields. For example, I have a field called membership type in the extended database. I would like to be able to search on that in a search form. Is that possible?
    Thanks

    Sidney,
    Your input is very helpful to put me on the track to find a workaround. As I tried using Web App approach, I get into a road block that hopefully you can help throw some light on.
    This my scenerio: I created a Web App for holding the profile fields (called it Profile). I created a Page to display the Customer record (Names, usernames, email, etc, and I would also want to display the fields from the Profile Web App. I am at lost how to do that. Seems like I can only insert the web item list in that page but I actually want to display the Detall Layout instead of the List layout. To display the Detail Layout, I need to first manually choose the item before it will direct me to the Detail layout. I am trying to find a way to do this automatically based on the fact I have only one Profile web app item for each customer.
    The question is whether there is a way to pull up a specific Profile web app item based on the customer's id or something that link the two together, using javascript or whatever method. Is there a tag to gives me the customer record id? If I can get that id, I could insert that id into one of the fields in the Profile Web App during creation of profile. Perhaps I can then pull up the profile item based on this id.
    Thanks for all the help
    Steve

  • Is there a way to have 2 threads communicate w/each other?

    I'm working on a program which requires 2 threads to communicate with each other. I was wondering how I can do this? How can I find out an id to reference a thread?

    I am not sure that there is .. not directly anyway. You can creat an intermediate class and pass copies of its objects to your threads. And then your threads can use it as a means of communication between them. For example:
    class A {
    // you have some private and public methods here.. which you can use to communicate between threads
    class B extends Thread {
    private A aVar;
    public B (A a) {
    aVar = a;
    //you have your run method here..
    class C extends Thread {
    private A aVar;
    public C (A a) {
    aVar = a;
    //you have your run method here..
    class D {
    public static void main(String [] args) {
    A a = new ();
    B b = new B (a);
    C c = new C(a);
    b.start();
    c.start();
    }

  • Can i use TC to backup a pc using the internal storage?Is there a way to partition some of it for FAT 32?

    is there a way to partition TC so I can back a pc on my network without having to get a usb hard drive?

    You cannot partition a TC.. well not without pulling it apart.. not recommended unless you have a fit of the crazies.. like moi.
    You do not need to FAT32 anything.. indeed you cannot.. the internal drive of the TC is HFS+ and is always going to be HFS+.
    But it is shared with windows over the network as if it was FAT32.. so you can indeed backup your pc to it.
    Simply mount (sorry Linux/Apple term).. simply access the TC drive, by typing its address in Windows Explorer.
    \\TCname or \\TCIPaddress where you obviously replace with the real network name.. or the real IP address.. so by default it would be \\10.0.1.1
    If PC cannot access it make sure you have airport utility for windows loaded.. and you have PC setup in home location not work or public.. no luck turn off the firewall.

  • Tax Accounts - Is it necessary to have separate tax acct. for each state

    I am new at this:
    Is it necessary to have separate AR/AP/ account for each tax jurisdiction like state county etc?\
    Or do the sales tax reports for payments in SAP produce outstanding taxes per jurisdiction.
    I would prefer only one Sales Tax account and hope to have SAP produce the taxes due per jurisdiction.
    Mike

    Thanks Suda.
    So I only need to setup two tax accounts:
    1.  One tax account for tracking the AR taxes
    2.  One tax account for tracking the AP taxes
    So, to report the taxes to the different tax jurisdictions, SAP should be able to break then down individually for us to pay them.
    I am new at this ... so please be patient.
    Mike

  • Is there a way to have a audible alert for when a mms fails to send?

    Pretty much the thread title. I want to know if there is a way for a better alert then just a little red "!" next to the text and on the mms app button. To many times I have tried to send a text then turn the screen off or slip the phone back into my pocket only to get a message awhile later from the person I was trying to text asking why I never replied then I see that lame excuse for a failed message alert. This is getting really old. Also why doesnt it auto-retry to send a failed message?

    No such functionality currently exists.
    Feedback can be submitted here.

  • Is there a way to have two iCloud accounts for one Apple Id

    I want to keeo sharing my apps with my wife but Dont want our safari info and contacts and calenders to sync together

    You're confusing icloud and itunes store accounts.  Individual users should have their own icloud accounts (using their Apple ID) but multiple users can share the same ID for logging into the itunes store account.

  • Is there a way to change pen tool width for each anchor point?

    I'm new to illistrator, and I've been looking around to see if I can find something that will show how I can take an ancor point and change the width of it. How can I do this? Thanks.

    There's a new width tool in CS5 which will allow you to vary the width of the stroke of a path.

Maybe you are looking for