questions about group by weeks

Hello experts

I have data similar to what follows below

create the table t_one

(

sale number (30),

date of date_req,

req_desc varchar (1000)

)

insert into t_one values (30, to_date (March 7, 2014, ' DD/MM/YYYY'), 'boston')

insert into t_one values (to_date 40, (March 9, 2014, "MM/DD/YYYY '"), "New York")

insert into t_one values (10, to_date (March 14, 2014, ' DD/MM/YYYY'), 'boston')

insert into t_one values (to_date 20, (March 16, 2014, "MM/DD/YYYY '"), "New York")

required output

Week total_sales

Week 1-70

Week 2 30

Please note, the first week will start at 07/03/2014 in the system, that is why it is week 1 and a week is from (Friday - Sunday). That is because the execution is either on Friday or Saturday or Sunday. I have problems with this because I can't think of a good way to group my weeks together.

any help is appreciated.

Thank you

Hello

Here's one way:

WITH got_week_num AS

(

Req_desc sale, SELECT

, FLOOR ((date_req-TO_DATE (7 mars 2014 ", «MM/DD/YYYY»)))

/ 7

) + 1 AS week_num

FROM t_one

)

SELECT 'week ' | week_num WEEK

SUM (sales) AS total_sales

OF got_week_num

GROUP BY week_num

ORDER BY week_num

;

This will work with dates before March 7, 2014, also.  The week before week 1 (that is, the beginning of the week on February 28, 2014) week is 0; the week before this (from February 21) is week - 1 and so on.

Tags: Database

Similar Questions

  • Question about Group by

    Hello friends,

    I have a table called APPLICATIONS (INQUIRY_ID, CALLING_DATE, ASSIGN_TO_AGENT)

    I need to create a report to show the number of requests received by Agent as follows:
    Assign_to_agent , number_of_daily_inquiries, total_inquiries_number
    
    X               ,         4                 , 150
    Y               ,         8                 , 200
    ..........
    ....
    I used this query that does not work:
    select assign_to_agent   ,
     count (inquiry_id) , 
     (select count ( inquiry_id)   as "total_inquiries_number"
     from inquiries   where trunc ( calling_date,'DD' )= trunc ( sysdate, 'DD')  ) as "number_of_daily_inquiries"
    from inquiries  group by assign_to_agent
    I hope that my question was clear.

    Kind regards
    Water

    Published by: cold_water on 25 Sep 2012 10:23

    Hi cold_water,

    Maybe it is not necessary to query two times table. Something like

    select   i.assign_to_agent,
             count (
               case
                 when trunc ( i.calling_date, 'DD') = trunc ( sysdate, 'DD') then
                   i.inquiry_id
               end ) as "number_of_daily_inquiries",
             count (i.inquiry_id) as "total_inquiries_number"
    from     inquiries i
    group by i.assign_to_agent;
    

    Concerning
    Peter

  • Question about GROUP BY and double aggregation for example MAX (AVG (val))

    Good evening/morning,

    I am struggling with what is probably a simple problem.

    The objective of the exercise is to display the highest average earnings and his Department (with the EMP table).

    Easy to get the maximum average wage:
    select max(avg(sal)) as max_avg_sal
      from emp
     group by deptno;
    I could not figure out how to change this query to get the deptno associated AVG. max. Gave up on changing this query and came to this:
    select deptno,
           avg_sal as max_avg_sal
      from (
            select deptno,
                   avg(sal) as avg_sal
              from emp
             group by deptno
             --
             -- order causes 1st row to have the max(avg(sal))
             -- this will be exploited in the where of the outer query
             --
             order by avg_sal desc                                             
           )
     --
     -- get only the first row since that one has the values we want
     --
     where rownum <= 1;
    This works, but feels pretty disappointing compared to the simplicity of the first query (which I couldn't the deptno on.) That is the request more simple with that I could come.

    The QUESTION is:

    The query above is really the way simpler and easier to get the maximum average and its associated department number? If it isn't, I'm more interested in your simplest solution. :)

    Could someone to laugh at one of the alternatives that I came up with a "simpler": solution
    with x as
      (
       select deptno,
              avg(sal) as avg_sal
         from emp
        group by deptno
      ),
      max_avg_sal as
      (
       select max(avg_sal)                  as asmax
         from x
      ),
      deptno_max as
      (
       select deptno                        as dnmax
         from x where avg_sal = (select asmax from max_avg_sal)
      )
    select (select asmax from max_avg_sal) as max_avg_sal,
           (select dnmax from deptno_max)  as deptno_max
      from dual;
    The road to hell is simple, it is paved from selects < chuckle >,.

    Thank you for helping,

    John.

    Hello
    Try this

    SELECT MAX(AVG(sal)) AS max_avg_sal,
           MAX(DEPTNO) KEEP (DENSE_RANK FIRST ORDER BY AVG(SAL) DESC )  DEPTNO
    FROM scott.emp
    GROUP BY deptno
    

    Kind regards
    Anthony Alix

  • Question about grouping.

    Hello

    I have a problem with my data as I want, I have 2 tables: orders & Modifiedorders,.
    the Modifiedorders holds the orderid and some information on how the order is changed, sometimes orderid
    in the Modifiedorders table can repeat if the changed data are of different types, such as changing the quantity or the price...

    Now what I want is to make a report that displays data from the Orders table and when there is
    record (s) in the table modifiedorders that it should appear on the left, for example, but without repeating the orderid.

    What I tried creating a query of outerjoin between 2 tables and using the wizard I created a report with the style of left group and had Orderid as the grouping field, but again orderid. What should I do?

    Thanks in advance...

    What I tried creating a query of outerjoin between 2 tables and using the wizard I created a report with the style of left group and had Orderid as the grouping field, but again orderid. What should I do?

    You have made the right choice. Reuse the reports Wizard.

    What is your question?

  • question about the financial week

    Hello

    I worked on the logic of creating a sql code. The requirement is to give a calendar date, he must return the id of the week in the fiscal year. For example
    November 1, 2010, I give the code should return the 5 as the output (like the given date belongs to a fiscal 2011, which starts from October 2011). Clues please.
    Thanks in advance

    kumar09 wrote:

    I have to write a procedure to fill this table since a date has to date, b. (may be any date before 1990, but B must be January 1, 2020)

    Query below will do the job:

    with t as (
               select DATE '2010-01-01' dt from dual union all
               select DATE '2011-06-01' dt from dual
              )
    -- end of data sample
    select  dt,
            to_number(to_char(dt,'ddd')) day_in_year,
            to_number(to_char(dt,'mm')) month_in_year,
            to_number(to_char(dt,'yyyy')) year,
            to_number(to_char(dt,'q')) quarter,
            dt - add_months(trunc(add_months(dt,-9),'yy'),9) day_in_financial_year,
            trunc(months_between(dt,add_months(trunc(add_months(dt,-9),'yy'),9))) month_in_financial_year,
            mod(to_number(to_char(dt,'q')),4) + 1 quarter_in_financial_year,
            trunc((dt - add_months(trunc(add_months(dt,-9),'yy'),9)) / 7) + 1 week_in_financial_year
      from  t
    / 
    
    DT        DAY_IN_YEAR MONTH_IN_YEAR  YEAR  QUARTER DAY_IN_FINANCIAL_YEAR MONTH_IN_FINANCIAL_YEAR QUARTER_IN_FINANCIAL_YEAR WEEK_IN_FINANCIAL_YEAR
    --------- ----------- ------------- ----- -------- --------------------- ----------------------- ------------------------- ----------------------
    01-JAN-10           1             1  2010        1                    92                       3                         2                     14
    01-JUN-11         152             6  2011        2                   243                       8                         3                     35
    
    SQL> 
    

    SY.

  • question about group by function

    Hello

    I need to know why when the clause is not used, and the having clause is used in the function group, can any one will contact

    When the clause is used to filter the lines in service come pathological column.
    He selects and filters-out of the lines in the table listed in the from clause.

    for example if you have a table with matching employees and departments, and you want to see all employees of departments 10,20,30,40 and 50; you will use this condition in where clause

    select
        dept_id, emp_id
    from
        employee
    where
       dept_id in(10,20,30,40);
    

    Now let's say you want to check the number of employees in these departments

    select
        dept_id, count(emp_id)
    from
        employee
    where
       dept_id in(10,20,30,40)
    group by
       dept_id;
    

    now, if you need to select only the departments that have more than 1000 employees in them, your query will look like this:

    select
        dept_id, count(emp_id)
    from
        employee
    where
       dept_id in(10,20,30,40)
    group by
       dept_id
    having count(emp_id) > 1000;
    

    Here the condition where clause filters out all departments other than 10,20,30,40,50; the condition that must be applied for further filtering some departments on a per employee basis, cannot be applied in the where clause as it should be applied after + the function group worked on the rows of the table; After calculating the number of employees in each Department. Where the need for the clause which is evaluated after the group by the having clause.

    I hope this helps.

  • Question about GROUP BY and HAVING

    Good afternoon

    I have the following query, which returns the desired result (a set of students who follow the CS112 or CS114 but not both). I wanted to 'condense' it in a single SELECT statement (if that's possible - DDL of execution of the instruction is provided at the end of this post):
    --
    -- is this select distinct * and its associated where clause absolutely
    -- necessary to obtain the result ?
    --
    
    select distinct *
      from (
            select s.sno,
                   s.sname,
                   s.age,
                   sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end)
                     over (partition by s.sno) as takes_either_or_both
              from student s join take t
                               on (s.sno = t.sno)
           )
      where takes_either_or_both = 1
    ;
    The following text seemed reasonable, but unfortunately without success:
    /*
      Window functions not allowed here (in Having Clause)
    
    select max(s.sno),
           max(s.sname),
           max(s.age),
           sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) as takes_either_or_both
      from student s join take t
                       on (s.sno = t.sno)
     group by s.sno
    having sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) = 1
    ;
    
    */
    
    /*
    
    Invalid identifier in Having clause
    
    select s.sno,
           s.sname,
           s.age,
           sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) as takes_either_or_both
      from student s join take t
                       on (s.sno = t.sno)
     group by s.sno, s.sname, s.age
    having takes_either_or_both = 1
    ;
    I tried a document that completely defines the order in which the clauses are executed. I found bits and pieces here and there but not complete something. I realize that my race with problems like this is due to my lack of understanding of the sequence and scope of the clauses that make a statement. For this reason, I can't even say if it is possible to write the query above using a single select statement. Forgive my little frustration...

    Thank you for your help,

    John.

    DDL follows.
            /* drop any preexisting tables */
    
            drop table student;
            drop table courses;
            drop table take;
    
            /* table of students */
    
            create table student
            ( sno integer,
              sname varchar(10),
              age integer
            );
    
            /* table of courses */
    
            create table courses
            ( cno varchar(5),
              title varchar(10),
              credits integer
            );
    
            /* table of students and the courses they take */
    
            create table take
            ( sno integer,
              cno varchar(5)
            );
    
            insert into student values (1,'AARON',20);
            insert into student values (2,'CHUCK',21);
            insert into student values (3,'DOUG',20);
            insert into student values (4,'MAGGIE',19);
            insert into student values (5,'STEVE',22);
            insert into student values (6,'JING',18);
            insert into student values (7,'BRIAN',21);
            insert into student values (8,'KAY',20);
            insert into student values (9,'GILLIAN',20);
            insert into student values (10,'CHAD',21);
    
            insert into courses values ('CS112','PHYSICS',4);
            insert into courses values ('CS113','CALCULUS',4);
            insert into courses values ('CS114','HISTORY',4);
    
            insert into take values (1,'CS112');
            insert into take values (1,'CS113');
            insert into take values (1,'CS114');
            insert into take values (2,'CS112');
            insert into take values (3,'CS112');
            insert into take values (3,'CS114');
            insert into take values (4,'CS112');
            insert into take values (4,'CS113');
            insert into take values (5,'CS113');
            insert into take values (6,'CS113');
            insert into take values (6,'CS114');

    Hi, John,.

    Just use the SUM aggregate function.

    --
            select s.sno,
                   s.sname,
                   s.age,
                   sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end) as takes_either_or_both
              from student s join take t
                               on (s.sno = t.sno)
           GROUP BY  s.sno,
                    s.sname,
                  s.age
           HAVING  sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end)  = 1;
    

    Analytical functions are calculated after the WHERE - HAVING clause are applied. Use the results of an analytic function in a WHERE or HAVING clause, you need to calculate it in a subquery, and then you can use it in a WHERE - or the HAVING of a query clause Super.

  • Just a quick question about the appearance of the cluster

    Hello, this is Matthew, just a quick question about the appearance of the cluster

    Is there anyway to rearrange the order of the elements? I know cutting automatic resizing, I could move the items by hand. But if I use the cluster somewhere else, I have to move the items again...

    What I want is of the order of "Mx My Mz Ax Ay Az Temp voltage CS".

    Is there a quicker way to deal with?

    THz so much!

    You can right-click the border of the cluster and choose "arrange control cluster...". "The user interface is not as intuitive as it could be, but it will allow you to reorder items. If you use the cluster in several places, you should make the cluster in a type definition and replace all current uses of the latter with the type definition, so that they all match. They will also update automatically when you make changes to the type definition. Otherwise, you could have a situation where you have multiple groups with the same data types, but with elements in a different order, and your data will be getting a new denominated, when wire you the whole clusters. The data will be in the same order, but given that the item labels are in a different order, you can't get the data you want, when ungroup you by name.

  • Where should I post questions about Windows 8?

    I intend to install the Windows 8 consumer overview once it is published next week. What forum is used for questions about W8?

    Noel

    Thanks for all the suggestions. Just for the sake of the of the cleanliness, here's the answer:

    Overview of Windows 8 consumer

    Noel

  • A few Questions about the videos on my Sansa Fuze

    Hey all,.

    I'm pretty new with my sansa fuze (I only had for about a week), and I have 2 questions about 'videos' on the "rocket"...

    Any help in answering these questions would be greatly appreciated!

    So:

    First of all, on my sansa fuze, I see 2 folders under the section "VIDÉO", "Audio_TS" and "Video_TS". What are these folders and files ranging in what folder?

    Second, I tried to load this video (about 20 min. length) through the Sansa Media Converter on my sansa fuze. Now, when I try to convert my video to a Vob (a kind of file extension), the appropriate file type file (which I suppose is all .mp4), I get a message saying: "It cannot convert, because your Sansa player is not connected" (even though I know I plugged). Is there a way I can get the Sansa converter program around and just use a different software to modify the mjy .mp4 file, or what I need to use the Sansa Media Converter to do?

    Again, any help is appreciated...

    Thank you

    Rocket-4-Life says:

    Hey all,.

    I'm pretty new with my sansa fuze (I only had for about a week), and I have 2 questions about 'videos' on the "rocket"...

    Any help in answering these questions would be greatly appreciated!

    So:

    First of all, on my sansa fuze, I see 2 folders under the section "VIDÉO", "Audio_TS" and "Video_TS". What are these folders and files ranging in what folder?

    Second, I tried to load this video (about 20 min. length) through the Sansa Media Converter on my sansa fuze. Now, when I try to convert my video to a Vob (a kind of file extension), the appropriate file type file (which I suppose is all .mp4), I get a message saying: "It cannot convert, because your Sansa player is not connected" (even though I know I plugged). Is there a way I can get the Sansa converter program around and just use a different software to modify the mjy .mp4 file, or what I need to use the Sansa Media Converter to do?

    Again, any help is appreciated...

    Thank you

    OK two records don't matter to you. SMC (or any other program) place the files in there, so you don't need to worry. VOB files are not compatible with SMC, so when you rip the DVD, you must use a program like Any-Video-Converter to create an AVI file or a mpge (my recommendations). Rhapsody will also add videos to the "rocket" as well.

  • 2 questions about mcupdate.exe

    Use Vista Home Premium 32-bit with SP2 of Vista on HP desk top of page

    2 questions about mcupdate.exe

    1. what is? When I google it I get's things on McAfee. I have McAfee and never had McAfee. The information google seems to indicate that it could be a virus. However, I see the following

    -It seems to be in the ehome on my computer folder

    -It seems it works twice a day and every time it turns my internet connection log shows the connection to a microsoft ip address for 'download tv' or something like, think if it is not not a virus

    2 seems that it does not always work. I'll get these connections every day, twice a day for a few weeks or more, then get not every day.

    Currently I seem to not get any activity, just after the silver light updated 01/09/10. I do not use the TV feature on my computer, but I was wondering what it is and why it works sometimes

    Hello

    The link works here even if it could be glitch or malware, if you can access a Microsoft site. Try
    Yet once, and if no joy don't try to see if you can reach http://www.microsoft.com

    The 2nd link describes the file unless it has been replaced by malware here is information.
    Would that be a normal update.

  • Question about the charging light on the venue Pro 11 (5130)

    I'm curious... read these discussions in recent weeks... found this work with the Tablet for about 2.5 hours after... I was up to 75% of battery. Connect the tablet to install a software... noticed 75%, not charging. Stop... removed the battery and put it back... He then read 75%, in charge. OK... so what's fixed now... a question about the manual of the system. Said that when the battery is charging, a white light is supposed to be on... where? When that I start, I see a momentary white light at the top of the screen, by the front camera... but when it is plugged and the loading and off... white light no.... No a - I misread? Thank you

    Light (color is white when the load) is located next to the power switch (top, right). Mine re-load very fast.

    White light goes off (or does not) when the Tablet is fully charged (the light on the charger is always on when it is plugged into the outlet)...

  • Person to contact with questions about the presentation of the app world

    If I have a question about the app that I submitted, anyone knows how do I contact someone about this? I can't find anything on the site and respond to notification emails gets me nowhere.

    Hey miss breeder,.

    that will be tough. If you browse the App World his hell out there because of the new development forums and people have a lot of problems with it. I've read people were emailing RIM all week and no responses were received. But if you post a question here MShom (one of the moderators) usually responds in a timely. The support email is [email protected] I believe. Good luck!

  • Questions about vCSHB 6.5.1 (LAN or WAN and physical vs. virtual secondary server)

    Hello

    I have a few questions about vCSHB 6.5.1:

    1) there are two sets of installation guides: one for secondary physical server and a secondary server.

    What are the main differences between these two scenarios?

    (2) for vCSHB, there are two deployment options: LAN and WAN

    2 (a) which option is most common?

    2 (b) If an option (for example, LAN) works in an environment, can I assume that the other would work as well?
    And what a difference these two options?

    (3) is vCSHB backward compatible? VCSHB 6.5.1 works in an environment where an earlier version (for example vCSHB 6.4) works?

    (4) what version of vCSHB (e.g. vCSHB 6.5 or 6.4 and 6.3) is widely deployed today?

    Thank you.

    (1) there should be nominal differences - what is different is discussed the virtual network set up against the physical network implemented

    2 (a), in my experience LAN configuration is more common because it over a high availability solution oppesed to DR.

    2 (b) not necessarily, you should make sure that the appropriate ports are open

    (3) I think it is but you have to bring everything up to a compatible version as a best practice

    (4) I'd go with the latest version as it has been out for a few weeks-

  • The specific question about the placement of an image into text.

    Hello, I had a question about the placement of an image into text.

    I did the contours to create text to place the image inside. My text is 5 words and two lines inside the box.

    When I do create contours he gives me 2 transparent route separate layers for the top and the bottom line.

    How will I be able to do that in a single layer of compound to place image that expands to the two lines?

    First of all, there are two ways of describing the type. The default value creates anchored objects. You want a separate object, so hold down the Option/Alt key when you create outlines. Drag the new contours to separate the text block.

    It is useful to open the layers panel and view the contents of the layer. You have created a group which consists of two transparent traces. In the layers panel, expand the layer so you can see the structure. Shift-select the two transparent traces. (Make sure that the group is not selected.)

    Then choose object > tracks > make a compound path. This makes the two transparent tracks into one. Then, you can place or paste into the single compound path.

Maybe you are looking for

  • Terrible GPS Since his arrival in new carrier

    Hello I moved from AT & T to tmobile. I had an iphone 6 with at & t and now have an iphone6s Since his arrival, all my applications that rely on the GPS are almost unusable. Specifically, cards and my bike app following my tour. I thought GPS was not

  • Links make activated by touch elsewhere

    Everything is fine with Chrome and Android Browser in this example: http://jsfiddle.net/6512pu9b/6/ But in Firefox mobile, touch the lines that are not links in the following example causes one of the links to be activated. Is it possible to make mob

  • Toshiba 32PU200EJ - HDMI port does not signal

    My toshiba TV series 32PU200EJ unable to connect to the HDMI port,last week, it was OK, I use it just for the PS3 game.When I try to connect the HDMI to PS3 or laptop, it says "unsupported SIGNAL". ANYONE can help with this problem? Thanks :)

  • SCREEN AUTO IMAGE ROTATION ISSUE

    Recently, the auto-roate has stopped working. In settings, I checked that the feature "auto rotation" was checked. I even disabled and double-checked the feature without success. Then, when I turn the phone 90 degrees the content of the screen not to

  • A lost folders, files, images, etc.

    Something happened yesterday to my pc and I think my son has hit the wrong key and all was lost. Is anyway to get it if I had not saved him?