Accumulative separate accounts

I was on it for two days. Someone could lead me in the right direction? Given the following data set:

create table data_owner.test_data
(
item_number varchar2 (10 byte),
Store_Number varchar2 (10 byte),
Calendar_Year varchar2 (10 byte),
Calendar_Week varchar2 (10 byte),
whole units_sold
)

Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', ' 31 ', ' 2010', 51', 4)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', ' 16 ', ' 2010', 51', 2)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', ' 31 ', ' 2010', 52', 3)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', ' 27 ', ' 2010', 52', 1).
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111 ', '16', ' 2011', 1', 3)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111 ', '27', ' 2011', 2', 5)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111 ', '20', ' 2011', 2', 4)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', ' 27 ', ' 2010', 51', 3)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', ' 16 ', ' 2010', 52', 2)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', ' 20 ', ' 2010', 52', 1).
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222 ', '16', ' 2011', 1', 3)
Insert into test_data (item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222 ', '31', ' 2011', 2', 3)

Select * from test_data

item_number store_number calendar_year calendar_week units_sold
1111 31 2010 51 4
1111 16 2010 51 2
1111 31 2010 52 3
1111 27 2010 52 1
1111 16 2011 1 3
1111 27 2011 2 5
1111 20 2011 2 4
2222 27 2010 51 3
2222 16 2010 52 2
2222 20 2010 52 1
2222 16 2011 1 3
2222 31 2011 2 3

My desired outcome is a sum of units sold and a separate cumulative store account numbers grouped by year and week point. that is to say:

item_number calendar_year calendar_week store_count sum (units_sold)
1111 2010 51 2 6
1111 2010 52 3 4
1111 2011 1 3 3
1111 2011 2 4 9
2222 2010 51 1 3
2222 2010 52 3 3
2222 2011 1 3 3
2222 2011 2 4 3

I can't get the number of store on the right. I tried various methods of the count (distinct store_number) on the analytical function (...), but nothing works. Thank you.

Hello

Interesting problem!

When you use analytical functions, you cannot use ORDER BY and DISTINCT. Too bad; It would be of course convenient.

The most general solution is to use aggregate instead of analytical functions functions and do a self-join to match each line (l ' table', for 'later' in the query below) with each line of earleir ('table' e below) for the same item:

SELECT       l.item_number
,       l.calendar_year
,       l.calendar_week
,       COUNT (DISTINCT e.store_number)     AS store_count
,       SUM (l.units_sold)
     / COUNT (DISTINCT e.ROWID)          AS total_units_sold
FROM       test_data   e
JOIN       test_data   l      ON     e.item_number     = l.item_number
AND                               e.calendar_year || LPAD (e.calendar_week, 2)
                                <= l.calendar_year || LPAD (l.calendar_week, 2)
GROUP BY  l.item_number
,       l.calendar_year
,       l.calendar_week
ORDER BY  l.item_number
,       l.calendar_year
,       l.calendar_week
;

You might think to store a DATE (say, the date that begins the week) instead of the week and the year. This would simplify this query and probably many others, too. I realize that could complicate other issues, but I think that you will have a net gain of fiond.

Thanks for posting the CREATE TABLE and INSERT statements; This helps a lot!

Published by: Frank Kulash, November 18, 2011 12:48

This is an analytical solution. As you can see, it requires more code and more complicated code, but it can work better:

WITH     got_r_num   AS
(
     SELECT     item_number
     ,     calendar_year
     ,     calendar_week
     ,     units_sold
     ,     ROW_NUMBER () OVER ( PARTITION BY  item_number
                               ,                    store_number
                         ORDER BY        calendar_year
                         ,                calendar_week
                       )      AS r_num
     FROM    test_data
)
SELECT DISTINCT
     item_number
,     calendar_year
,     calendar_week
,     COUNT ( CASE
                    WHEN  r_num = 1
              THEN  1
                END
           )             OVER ( PARTITION BY  item_number
                                  ORDER BY      calendar_year
                      ,          calendar_week
                             )                    AS store_count
,       SUM (units_sold) OVER ( PARTITION BY  item_number
                                ,             calendar_year
                      ,             calendar_week
                         )                         AS  total_units_sold
FROM       got_r_num
ORDER BY  item_number
,            calendar_year
,       calendar_week
;

This approah will not work in all situations of windowing. It's not serious fo this work, but not if you want, for example, a number of separate stores for the past 6 weeks, so that the report covers more than 6 weeks.

Tags: Database

Similar Questions

  • Replace .mac account shared with iCloud separate accounts.

    Hello

    I'm looking for advice on how best to clean up the mess created by our continued use of our ancient .mac account.

    My wife and I have had an account .mac since 2005.  Our e-mail addresses are aliases that are associated with this account.  It's messy.

    My wife and I would like to have separate accounts .icloud but, if possible, do not want to give up e-mail addresses that we had for 11 years.  Is this possible?  We also want to make the transition without complicating our existing .mac account settings. For example, our children now have their own accounts of .icloud that are related to the original address of .mac as part of a family sharing.

    Thank you!

    Ben

    Using more an iDevice on the same computer

    This mainly applies to couples who add another device and don't want their electronic mail, messages, etc. being duplicated on both devices. To start reading: How to use several iPhone, iPad or iPod devices with one computer. You must create a login separate Apple and password for anyone who will use the new iDevice. See Apple - my Apple ID and frequently asked questions about the Apple ID. The easiest way is to do it on the computer using iTunes: iTunes - how to set up an Apple ID iTunes.

    On the computer, create a new user account for the person with the new iDevice. This is the user account always uses this person. He or she will no longer use the other user account. This way the person will have a separate iTunes library. Note When finished, do not 'share' apps under a single Apple ID. Each of you must buy your own applications under your own Apple ID. When you decide to divide like this you will have to buy new applications they want to use.

    First, transfer the new devices to a new account as well as all your data.  Save pictures of flow picture you want to keep for your camera (unless they are already in the camera roll) by opening your Photos app, tap Albums icon at the bottom. Now press the album My Photo Stream; Press Select; tap the photo you want to select. tap the share (box with the face up arrow) in the lower left corner icon; in the film, and then click Save.

    If you synchronize notes with iCloud, you want to keep, you must open each of your notes and email it to yourself. Later, you can copy and paste the text into the new notes created in your new account.

    Tap Settings > iCloud > remove account (removes it only from this device, not iCloud; the person who maintains the current account is not affected) provide the password to disable find my phone and choose to keep on my iDevice when prompted.  Sign in with another Apple ID to create your new account. Choose merge to download your data.

    Once you are on separate accounts, you can each go to icloud.com and remove data from the other person to your account.

    The above was created by the user randers4. Changes were made to improve the readability and the syntax.

  • producer iTunes tells me my account is attached to several suppliers of itunes and I have to create separate accounts to log on. What does that mean?

    When I am trying to connect to Itunes producer to present my book on ibooks, it tells me that my account is attached to several suppliers of itunes and I need to create separate accounts to log on. What does that mean? Can anyone help? Thank you!

    In the FAQ:

    http://www.Apple.com/iTunes/working-iTunes/sell-content/books/book-FAQ.htm l

  • My wife and I have iPhone 5 s, the two rings at the same time while we are at home when one of us Gets a call. They are on the same account, is there a way to stop this without establishing separate accounts?

    My wife and I have iPhone 5 s, the two rings at the same time while we are at home when one of us Gets a call. They are on the same account, is there a way to stop this without establishing separate accounts?

    settings - phone - call another device - off.

    One of you should get their own apple ID, you just go to have problems later.

  • separate account using the lookup table

    How can I get a separate count of column values by using another table?

    Let's say I want to get a separate account of all 'company_name' files in the 'emp' table that corespond (soccer match) with table of 'research', 'State' category.

    What I want is to find charges for all businesses that have a value of 'california' in the 'Status' column of the 'search' Table. I want the output to look like:

    Sears 17
    Pennys 22
    Marshalls 6
    Macys 9

    I want the result to show me the company names dynamically as I don't know what they are, just that they are part of the group 'State' in the lookup Table object. Does make sense?

    M

    If you want a counter for each value of rfs_category, but instead of rfs_category you want to display, it's translated value lookup_value_desc.

    This should do it for you:

    select lookup_value_desc, the_count
      from lookup
      join (select rfs_category, count(*) the_count
              from RFS group by rfs_category)
        on rfs_category = lookup_type;
    
  • BlackBerry smartphones separate accounts Email/text

    I have currently my curve put in place to get my gmail email and will soon add email to work.  I wish I had boxes of reception separated for text/pic messages and blackberry messenger with each e-mail account.  Currently, I get all messages (SMS, MMS, messenger and email) to the title of the message, and then only gmail is gmail Inbox.  Is it possible to separate them?

    Thank you

    Jeremy

    Yes, you can scroll on your message box, and then click your menu button. Click Hide and which will hide. Or you can click your menu button and click Add FOLDER and create a folder for all your miscellaneous items and then menu button on each item and click MOVE and move them to the new folder.

    have a great day to take advantage of your blackberry!

    If I have helped you, please click on BRAVO in my post.  If this solves your problem, please click RESOLVED!

  • My wife and I use an address e-mail, but with separate wallpaper and parameters of separate accounts. How do I do this in W8?

    I just bought two new Dell computers, which are set up side by side on a LAN, one for me and one for my wife.  When configuring the computer of my wife, she is not able to create a new user account because mine has already used the same email. I woke up, the menu wallpaper tiles and start my computer appeared on my wife until I started to put in place, and the changes made on his computer appeared on mine.

    I want to be able to create two accounts separate, each able to read email family, but with different images of wallpaper and screen settings, arrangements of different start menu and names of different accounts and passwords that are independent.  In fact, I want several user accounts on each computer in the privacy for files between users.  I had it on XP.  How he do on W8?

    Thank you for your attention.

    If you want to change your current account at the local level and then put your mouse in the top right Conner and go on settings and click on it need to change PC settings "found inside" click on it then go to users in the left menu and click on that and then pressing the switch to a local account This can be found under the user name

    Sean

    hope that this help please reply if you need more help then please answer if it worked for you answer please

    Thank you

  • My husband and I use a PC with two separate accounts, why he get a notice of failure to update for Mozilla but I don't?

    We share a PC running Windows XP. When he opened his account, it continues to go in the lower right corner that the updater is safe then a message box indicating "failed update make sure that another version is not running and restart" when I opened my account no such message not appear why? Thank you-

    Who has administrative rights on this PC?

    If you don't have administrator rights you have probably not 'rights' to update Firefox, therefore do not receive the message "update."

    Try opening the help > about Firefox and see if you get a message about the update. And while you're in this window see if he says Firefox 27.0.1 or just Firefox 27.0.

    Insofar as the message "Failure of the update to make sure another version is not running and restart", if your user account runs in the background and you have Firefox open, which could cause Firefox not to proceed with the update in your husband logon user account. Firefox needs all the Firefox user to be closed as part of the process of updating profiles. The user who launches the profile update automatically closes, but the process will crash if Firefox profiles running in the background. I often happens when I switch between 2 user accounts to logon in Windows XP.

  • separate accounts?

    My daughter invited me to join her family for the Apple's music circle. However, when I rent movies on iTunes, I want that they attributed to my own credit card, not hers--as I did in the past. Is this possible?

    Feldbert,

    In a sharing of the family group, only the organizer can have a credit card on file.

    Non-organisateur members can always pay for their own purchases, however.  You will need to buy and exchange your own account iTunes gift cards, so that the bank credit will be used to purchase instead of the organiser credit card.

  • Is it possible to have two icons of messaging windows list separate accounts on the same computer?

    For example, one for my personal e-mail and another for our home business - so that when I click on an icon only personal mail is opened and when I click the icon only business correspondence is opened and listed?

    Hello geoAZ,

    You can have multiple accounts on your Windows Mail in Windows Vista.
    http://Windows.Microsoft.com/en-us/Windows-Vista/add-or-remove-a-Windows-mail-account

    and

    http://Windows.Microsoft.com/en-us/Windows-Vista/Windows-mail-setting-up-an-account-from-start-to-finish

    I hope this helps.

    Thank you

  • Windows Mail - separate accounts

    I have a number of different pop3 e-mail accounts, and they work very well. I implemented the subfolders for each items in Inbox and sent accounts. When an e-mail is received, it goes directly into the file relating to the account (I created a rule in the rules of Message for that to happen). However, I set up a rule for sent items go in the subfolder relating to this account (that is to say, account 2 - box sent) but it does not work! They always go the MAIN elements sent and I must manually, drag and drop them in the correct folders. Can you help me? Why is this happening?

    Hello

    I've never used any so I'm hesitant to try to recommend some. Try this link to ask those who
    probably has experience with some people:

    Discussions in Windows Mail
    http://www.Microsoft.com/communities/newsgroups/en-us/default.aspx?DG=Microsoft.public.Windows.Vista.mail
    Rob - bicycle - Mark Twain said it is good.

  • Work with separate accounts of Adobe

    Hello

    I work for a company who bought CC which I use.

    I also have a personal account for Technical Communication Suite.

    Whenever I passes between applications for me to sign into this account.

    Is there a solution for this?

    Thank you

    No, unless you get two different machines, so that each of them is connected to another account.  You can have only one active account on one machine at a time.

  • I can define G.L. separate accounts for rebates that I create in Advanced Pricing?

    Hello!

    We are including to see the updated amount of an element in A.R. and G.L. By setting the parameter of OM system: Discount Show Bill Details. Yes, we can do it. We still have to be able to define different GL accounts on the different discount we give to a particular element. Here's our scenario: we normally give 2-3 discounts on a particular item.  So what I did was to set a discount for an item modifier. This modifier, I defined 2 lines that is having the same number of points, each line has its own discount as a percentage:

    30 percentage point

    50 percentage point

    Total discount is 80 percent.  Now, the question is, can I connect different GL accounts with these 2 different prices on the same point?

    Thank you.

    Jon

    We can fill AR_INTERFACE_DISTRIBUTIONS_ALL manually and assign the desired for each specific discount GL code_combination_id.

  • I can link separate accounts cc

    A colleague and I bought CC of Photoshop and InDesign CC independently of the other. We now also need Illustrator CC and it would be more profitable to buy the complete set of product of CC. Both accounts may be related?

    Not for two different people.  Licenses are per user.  You can see business plans, although I don't know if it ends with save you anything.

    Creative cloud plans

    https://creative.Adobe.com/plans

  • export data from half-out interval, with separate accounts

    I have in Oracle 10g database table T as below:
    with T as 
    (select to_date('14.07.2010 12:46:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 1 Cnt, 'N1' Name from dual UNION ALL
    select to_date('14.07.2010 12:46:26', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 1 Cnt, 'N1' Name from dual UNION ALL
    select to_date('14.07.2010 12:48:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 2 Cnt, 'N1' Name from dual UNION ALL
    select to_date('14.07.2010 12:46:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 3 Cnt, 'N2' Name from dual UNION ALL
    select to_date('14.07.2010 12:49:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 3 Cnt, 'N2' Name from dual UNION ALL
    select to_date('14.07.2010 13:00:26', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 10 Cnt, 'N1' Name from dual UNION ALL
    select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 10 Cnt, 'N1' Name from dual UNION ALL
    select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 30 Cnt, 'N2' Name from dual UNION ALL
    select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 31 Cnt, 'N2' Name from dual
    )
    select * from T;
    /*
    1     14.07.2010 12:46:24     1     N1
    2     14.07.2010 12:46:26     1     N1
    3     14.07.2010 12:48:24     2     N1
    4     14.07.2010 12:46:24     3     N2
    5     14.07.2010 12:49:24     3     N2
    6     14.07.2010 13:00:26     10     N1
    7     14.07.2010 13:00:25     10     N1
    8     14.07.2010 13:00:25     30     N2
    9     14.07.2010 13:00:25     31     N2
    */
    I want the query to this output:
    [Timegap]; [Name]; [DistinctCnt]
    12:30-12:59, N1, 1--we show only one record-instance here of Cnt=1
    12:30-12:59, N1, 2
    12:30-12:59, N2, 3--we show only one record-instance here of Cnt=3
    13:00-13:29, N1, 10--we show only one record-instance here of Cnt=10
    13:00-13:29, N2, 30
    13:00-13:29, N2, 31
    The output should do following:
    1. take all records where the "SnapshotDate" is located in the time interval "12:30 - 12:59". "." We have 5 records.
    2. now take distinct values of 'Name' of this interval. We have: N1, N2.
    3. take for each folders 'Name' with cnt value separate and distinct recordings of output. We have in the meantime 12:30-12:59 for N1 same two records with the same value of "Cnt", we take one of these records only and output. The other ecord for N1 with different cnt value is unique, it should also be issued. So, we released 3 rows for this time interval:
    12:30-12:59, N1, 1--we show only one record-instance here of Cnt=1
    12:30-12:59, N1, 2
    12:30-12:59, N2, 3--we show only one record-instance here of Cnt=3
    Those are records of 'name' "s with distinct value of the Cnt so to say, for that interval. For example the interval for simplicity "12:30 - 12:59; 13:00 - 13:29 ", but I lactually wil need more late several intervals, from" 00:00 - 00:29 "up to" 23:30 - 23 - 59 ", but I will do that later, lets now assume that we need these intervals of 2 only."

    How to write this query?

    Joints of 'disappeared', for here is an example:

    with    t as (select to_date('14.07.2010 12:46:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 1 Cnt, 'N1' Name from dual UNION ALL
                  select to_date('14.07.2010 12:46:26', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 1 Cnt, 'N1' Name from dual UNION ALL
                  select to_date('14.07.2010 12:48:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 2 Cnt, 'N1' Name from dual UNION ALL
                  select to_date('14.07.2010 12:46:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 3 Cnt, 'N2' Name from dual UNION ALL
                  select to_date('14.07.2010 12:49:24', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 3 Cnt, 'N2' Name from dual UNION ALL
                  select to_date('14.07.2010 13:00:26', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 10 Cnt, 'N1' Name from dual UNION ALL
                  select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 10 Cnt, 'N1' Name from dual UNION ALL
                  select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 30 Cnt, 'N2' Name from dual UNION ALL
                  select to_date('14.07.2010 13:00:25', 'DD.MM.YYYY HH24:MI:SS') SnapshotDate, 31 Cnt, 'N2' Name from dual),
    intervals as (select to_date('14.07.2010', 'dd/mm/yyyy') + (level - 1)*30/1440 time_period
                  from   dual
                  connect by level <= 24*2) -- generates one day's worth of time periods
    select distinct to_char(i.time_period, 'dd/mm/yyyy hh24:mi')||'-'||to_char(i.time_period + 29/1440, 'hh24:mi') timegap,
                    t.cnt,
                    t.name
    from   t,
           intervals i
    where  i.time_period = trunc(t.snapshotdate (+), 'hh') + trunc((t.snapshotdate (+) - trunc(t.snapshotdate (+), 'hh'))*1440/30)*30/1440
    order by timegap, name, cnt;
    
    TIMEGAP                       CNT NAME
    ---------------------- ---------- ----
    14/07/2010 00:00-00:29
    14/07/2010 00:30-00:59
    14/07/2010 01:00-01:29
    14/07/2010 01:30-01:59
    14/07/2010 02:00-02:29
    14/07/2010 02:30-02:59
    14/07/2010 03:00-03:29
    14/07/2010 03:30-03:59
    14/07/2010 04:00-04:29
    14/07/2010 04:30-04:59
    14/07/2010 05:00-05:29
    14/07/2010 05:30-05:59
    14/07/2010 06:00-06:29
    14/07/2010 06:30-06:59
    14/07/2010 07:00-07:29
    14/07/2010 07:30-07:59
    14/07/2010 08:00-08:29
    14/07/2010 08:30-08:59
    14/07/2010 09:00-09:29
    14/07/2010 09:30-09:59
    14/07/2010 10:00-10:29
    14/07/2010 10:30-10:59
    14/07/2010 11:00-11:29
    14/07/2010 11:30-11:59
    14/07/2010 12:00-12:29
    14/07/2010 12:30-12:59          1 N1
    14/07/2010 12:30-12:59          2 N1
    14/07/2010 12:30-12:59          3 N2
    14/07/2010 13:00-13:29         10 N1
    14/07/2010 13:00-13:29         30 N2
    14/07/2010 13:00-13:29         31 N2
    14/07/2010 13:30-13:59
    14/07/2010 14:00-14:29
    14/07/2010 14:30-14:59
    14/07/2010 15:00-15:29
    14/07/2010 15:30-15:59
    14/07/2010 16:00-16:29
    14/07/2010 16:30-16:59
    14/07/2010 17:00-17:29
    14/07/2010 17:30-17:59
    14/07/2010 18:00-18:29
    14/07/2010 18:30-18:59
    14/07/2010 19:00-19:29
    14/07/2010 19:30-19:59
    14/07/2010 20:00-20:29
    14/07/2010 20:30-20:59
    14/07/2010 21:00-21:29
    14/07/2010 21:30-21:59
    14/07/2010 22:00-22:29
    14/07/2010 22:30-22:59
    14/07/2010 23:00-23:29
    14/07/2010 23:30-23:59             
    

    You may want to nvl (cnt, 0) or nvl (to_char (cnt), '-') etc.

Maybe you are looking for