Group by Week and then by Month

Hello Forum Members,
Can you please help me on this:
City      Demand Demand_Date
Seattle    20     06/04/2014
Seattle    10     06/07/2014
Seattle    30     06/16/2014
Seattle    20     07/04/2014
Austin    10     06/07/2014
Austin    30     06/16/2014  
City       Demand           Demand_Week      Demand_by__Month  Demand_Month
Seattle     30              06/07/2014
Seattle     30              06/21/2014         60               
06/2014
Seattle     20              07/05/2014         20
Austin      10              06/07/2014
Austin      30              06/21/2014         40               
07/2014
Best,
Sqlquery9

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Now we have to guess because you are rude. 
Learn how to follow ISO-11179 data element naming conventions and formatting rules. Have no idea what this is. Temporal data should use ISO-8601 formats. You failed! 
Code should be in Standard SQL as much as possible and not local dialect. You did not even try! Are you always this rude to people that you want to do your job for you? 
This is minimal polite behavior on SQL forums. Here is my guess at cleaning up your mess. 
CREATE TABLE Cocaine_Demand
(city_name VARCHAR(15) NOT NULL,
 demand_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
 PRIMARY KEY (city_name, demand_date),
 demand_cnt INTEGER NOT NULL);
INSERT INTO Cocaine_Demand
VALUES
('Seattle', '2014-06-04', 20),
('Seattle', '2014-06-07', 10),
('Seattle', '2014-06-16', 30),
('Seattle', '2014-07-04', 20),
('Austin', '2014-06-07', 10),
('Austin', '2014-06-16', 30); 
Report Period Table
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
The report period table gives a name to a range of dates that is common to the entire enterprise. 
CREATE TABLE Something_Report_Periods
(something_report_name CHAR(10) NOT NULL PRIMARY KEY
   CHECK (something_report_name LIKE <pattern>),
 something_report_start_date DATE NOT NULL,
 something_report_end_date DATE NOT NULL,
  CONSTRAINT date_ordering
    CHECK (something_report_start_date <= something_report_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'
Weeks within Year: 
I would build a calendar table with a ISO-8601 week-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is a separator token, ww is (01-53) week number and d is (1-7) day of the week.
You input any calendar date, find the week-within-year column and return the dates that match on a LIKE predicate.
WHERE sale_day LIKE '2012W26-[67]'
There are several websites with calendars you can cut & paste, but you can start your search with: http://www.calendar-365.com/week-number.html 
Try to write this yourself. If you were too lazy to post DDL or DML, why should we do it for you? 
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • Using ios 7.0.4 can a group email be sent either from icloud or iPhone by a means other than selecting the individual Contact, ie; can the group be selected and then an email composed that will send the message to all group members.

    Using ios 7.0.4 can a group email be sent either from icloud or iPhone by a means other than selecting the individual Contact, ie; can the group be selected and then an email composed that will send the message to all group members.

    Hi Richard, 
    Thanks for the reply - I think I've sorted it though and there isn't actually an issue.
    The whole group wasn't receiving the NDR, only the group manager which I setup a few weeks prior. This is a new feature so it complies with certain RFC's, basically the group manager will recieive the NDR to let them know there is a problem.
    Something to do with mass mailing and unsolicited mail.
    Ta
    Ian

  • Hi I do not want iTunes to open up automatically when I turn on my macbook pro.  I tried going to System Preferences Users and Groups Login Items and then I took iTunes off the list but it still opens up automatically when I turn on my laptop.

    Hi I do not want iTunes to open up automatically when I turn on my macbook pro.  I tried going to System Preferences>Users and Groups>Login Items and then I took iTunes off the list but it still opens up automatically when I turn on my laptop. What should I do?

    Hi r,
    Make sure you close iTunes before shutdown.  And you're quite welcome.

  • If I start a one year contract for photoshop/lightroom, and then 6 months in, I want to upgrade to the full cloud, do I get out of the rest of the payments for the photoshop/lightroom, and just start paying a total cloud fee?

    If I start a one year contract for photoshop/lightroom, and then 6 months in, I want to upgrade to the full cloud, do I get out of the rest of the payments for the photoshop/lightroom, and just start paying a total cloud fee?

    Upgrade single to all Cloud http://forums.adobe.com/thread/1235382 may help

  • My macbook from 2008 which I bought used worked fine for 3 weeks and then suddenly it started restarting and now it restarts, goes to grey apple screen and a code saying something about a kernel panic appears and it restarts again and again. Help?

    My macbook from 2008 which I bought used worked fine for 3 weeks and then suddenly it started restarting and now it restarts, goes to grey apple screen and a code saying something about a kernel panic appears and it restarts again and again. Help?

    Restart the computer and immediately hold the Option key down.
    Select the Recovery partition and continue.
    On the next pane, select Disk Utility and continue.
    When DU opens, select your hard drive in the left column, click the First Aid tab, the click Repair Disk.
    When that is done, quit DU and restart in the usual way.

  • I have a new ipad air, and the notes app does not work right.  I write a note and it will keep it for several days, or weeks and then I go back to it later and it has disappeared.  This has happened several times.  Any ideas why?

    I have a new ipad air and am having trouble with the "notes" app.  When I write a note to keep, it will stay there for a few days or a week, and then it disappears.  This has happened several times.  Can anyone suggest why this is happening and how to fix it?

    Ok. I disabled hardware acceleration. The only extension is the McAfee Scriptscan for Firefox 15.1.0, which was already disabled. I then disabled all plugins (Acrobat, Google Update, Intel Identity Protection (2x), Java Deployment Toolkit, Java Platform, MS Office 2013, Quicktime, Shockwave, Silverlight), which were all up to date.
    Normal or safe mode still has same result. No startup, no option button and no customize.
    Thanks for your help. Got any other suggestions?

  • I need script which will filter a set of groups by name, and then give a CSV with direct members

    I need script which will filter a set of groups by name, and then give a CSV with direct members
    say it like,
    $groupitems = Get-ADGroup -SearchBase "DC=fincaint,DC=local" -Filter {Name -like "*AppUsers"}
    ForEach ($groupitem in $groupitems)
            IF ($groupitem.objectClass -eq 'user')
       Get-ADUser $groupitem | FT Name, Email, City, Title | Export-Csv c:\member.csv
          IF ($groupitem.objectClass -eq 'group')
       Get-ADgroup $groupitem | FT GroupName, GroupDN | Export-Csv c:\member.csv

    Hi Dhiravia,
    It's been a while. How is it going? If the suggestion provided by ThmsRynr doesn't help, for scripting questions, in order to get better help, it's recommended that we ask for advice in the following scripting forum.
    The Official Scripting Guys Forum
    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG
    Best regards,
    Frank Shen
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected]

  • TS1559 My wife's iPhone 4s worked fine. She syncd last week and then it couldn't find any wi-fi networks. Tried all the steps above except that after upgrading to ios 6, the wi-fi is toggled off and is greyed out, unable to turn on. NOT AN AT&T ISSUE

    Nothing posted on any of the threads has worked. I am reluctant to revert back to 5.1.1 which some say worked for them. Posting this solely so Apple will know there is one more continuing to have this issue and it is a major inconvenience.

    I have been having the same issue for the last two weeks. No real indication as to why it ever began other than I had turned off my router and Airport Extreme Base Station off for a weekend while I was away from my place.
    iPad, iPhone, iPod Touch, all have no issues at all with my Airport Extreme Base Station. Can stay connected throughout my living room, bathroom, bedroom, no issues whatsoever.
    My 5 month old MacBook Pro 2.66 GHz Core i7 Airport is sometimes perfect, able to connect, DHCP, and browse without issue. Then, for no reason, I won't be able to join my wireless network, or when I can join, I can't pick up an IP address. And then it will connect, pick up and IP, and then it won't browse. No real rhyme or reason to when this will happen, or when it works.
    I thought that it was an issue with my MacBook Pro, but then my sister visited with her brand new MacBook Pro 2.26 Intel Core 2 Duo, and while a month ago she was able to connect, DHCP, and browse without issue, this weekend she had all of the same issues I was having.
    My XBOX 360 and MacBook Pro have absolutely no issues with the wired connection. Only my WAN seems to have this issue.
    Did you ever find anything more to this?

  • Connection improved slightly for a week and then h...

    I get no more than 0.72MB download with 0.22MB upload.  For a week; with no router reset I acheived 1.1MB. I thought woohoo, but then the connection has dropped down again to 0.72 again.  Any ideas? I have reset my router again just to test. I can also hear line noise on our telephone. Would it be worth asking BT to reset my line since 21CN WBC is meant to be enabled at my exchange at the end of the month? I have tried every tweak going with no success e.g master socket, internet programs etc etc.
    Line state
    Connected
    Connection time
    0 days, 0:45:31
    Downstream
    928 Kbps
    Upstream
    288 Kbps
    ADSL settings
    VPI/VCI
    0/38
    Type
    PPPoA
    Modulation
    ITU-T G.992.1
    Latency type
    Interleaved
    Noise margin (Down/Up)
    7.1 dB / 6.0 dB
    Line attenuation (Down/Up)
    63.5 dB / 31.5 dB
    Output power (Down/Up)
    15.8 dBm / 12.3 dBm
    Loss of Framing (Local)
    0
    Loss of Signal (Local)
    0
    Loss of Power (Local)
    0
    FEC Errors (Down/Up)
    742 / 0
    CRC Errors (Down/Up)
    16 / 2147480000
    HEC Errors (Down/Up)
    nil / 0
    Error Seconds (Local)
    10

    when you say not much noise is that when you do the quiet line test?  if you do the quiet line test with a corded phone you should hear nothing apart from the woman's voice - no hiss, crackle or any other sound.  If you use a cordless phone then you may hear a dull hum in the background.
    So if you hear any noisethen that will affect your broadband.  you should dial 151 and report a phone fault NOT a broadband fault.
    removing the bellwire does help and information is hear  http://www.jarviser.co.uk/jarviser/bellwirenutshell.html
    If you like a post, or want to say thanks for a helpful answer, please click on the Ratings star on the left-hand side of the post.
    If someone answers your question correctly please let other members know by clicking on ’Mark as Accepted Solution’.

  • My iphone was stolen and then returned months later after I already got a new iphone the old iphone is disabled I sold it on eBay and don't have a computer to enable it will Genius Bar fix my phone and at what cost

    My iphone was returned to me and its disabled because I reported it stolen months ago. Will Genius Bar enable it so I can sell it on eBay seeing as I already got a new one?? I don't have a computer to connect to iTunes. Will they charge me?

    Genius bar appointments are free. To remove the passcode on a disabled iPhone requires forcing the phone into recovery mode & restoring it, using iTunes on a computer. Whether the genius bart will do this for you is a good question. I suspect if you can prove the phone is yours, there shouldn't be any issues. Only one way to find out, & that's to make an appointment.
    Good luck.

  • HT4600 i Get a solid amber light sometimes, bUT all works fine. I have to unplug the airport extreme to reset it and then it turns green and starys that way for a week or so and then turns back to amber. why is this and how do i fix it ?

    I gat a solid amber light once a week on my airport extreme, but all works fine. I have to unplug it to reset it and then the light turns back to green and it is good for another week and then it happens again. why is this and what can i do to fix it ?

    Solid amber on an AirPort Extreme is an indication that the internal power supply is starting to act up.
    If the AirPort Extreme is in warranty, it shouid be exchanged for another AirPort Extreme.
    If the AirPort Extreme is several years old, keep any eye on things. When/if you find that you are having to reset the device several times a week, it's time to start thinking about a replacement device, as the end is near.

  • IMac freezes in mid application and then can't shut down or reboot

    I have a iMac which is about 6 years old with 1 GB RAM, 60 GB hard drive and operating on OS 10.4.8 (Tiger). It has about 16 GB memory free as well.
    It was running quite nicely until about 2 months ago and then had probems with it running slower and slower while completing applications, and finally it started freezing in mid application with the spinning beachball of death. The computer has to be shut down by pulling the power. Sometimes I can restart it with the power button and . But this latest time, it did not even start from the C drive with the OS X disc in place.
    I tried the simple things like fixing permissions, and then I had tried re-loading Tiger both with archived files and also a clean install. Both times the machine worked beautifully for about a week and then would go through the same sequence, with slower processing time, and then the eventual terminal freeze up.
    Any suggestions to salvage this machine? I did take it in to an Apple authorized dealer, where they backed up my files and did the clean install. But I don't have a diagnosis. Is the hard drive corrupted? Anything short of replacing the hard drive (which I am not capable of doing myself) or buying a new computer? I loved this iMac, it has an amazing screen, and despite its age was still a very neat machine. It would be great if there is a solution out there.
    iMac   Mac OS X (10.4.8)  

    Welcome to Apple Discussions!
    There is no such thing as a Mac OS X clean install as my FAQ explains:
    http://discussions.apple.com/thread.jspa?messageID=607614
    However, your machine is right at the age, or a little beyond where the clock battery may begin to die. This can cause slowdowns and other issues.
    The hard drive could also be problematic, as some of these can die in less than a year, while others can last as long as a decade.
    The more you install Dashboard widgets, the slower your machine will get, and that can cause problems too. See my FAQ on other slowdowns if neither of these seems to help:
    http://www.macmaps.com/Macosxspeed.html

  • SharePoint 2010 View Group By Category and Sub-Category

    I have a document Library with Category and Sub-Category columns. In the view I am grouping by Category and then Sub-Category. However, most of the documents do not have a Sub-Category. My desired result would be that the document would appear underneath
    the Category. However, if no Sub-Category is assigned then SP creates a generic Sub-Category and puts the document there. It should look something like this:
    Category : Main Category
    Document-1
    Document-2
    Sub-Category : Sub Category
        -Document-3
        -Document-4
    Instead it is Creating a generic SubCategory:
    Category : Main Category
    Sub-Category : (1)
        -Document-1
        -Document-2
    Sub-Category : Sub Category
        -Document-3
        -Document-4

    Hi,
    Per my knowledge, when you set group by Category and then Sub-Category, it should looks like the following:
    Category : Main Category
    Sub-Category : (2)
        -Document-1
        -Document-2
    Sub-Category : Sub Category
        -Document-3
        -Document-4
    I think there is no OOTB method that could make it looks like the following:
    Category : Main Category
    Document-1
    Document-2
    Sub-Category : Sub Category
        -Document-3
        -Document-4
    Best Regards,
    Lisa Chen
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • How do I create groups in Mail and add contacts to that group?

    How do I create groups in Mail and then add contacts to that group?

    Look in Mail > Preferences > Composing tab > If you want all the email addresses to show, check "When sending to a group, show all member addresses".   If unchecked, just the groupname should appear.
    If it still doesn't work then review the email addresses for each of the group members looking for errors in format.
    Also, clean up the list in previous recipients in Mail > Window > Previous Recipients.

  • TS1702 i've an iphone 3.1.3. sometimes, i have problems opening all the apps i downloaded in the past 4 months. other apps would open but the recent ones will begin to boot and then close itself. once, it persisted for over a week. pls help as am frustrat

    i've an iphone 3.1.3. sometimes, i have problems opening all the apps i downloaded in the past 4 months. other apps would open but the recent ones will begin to boot and then close itself. once, it persisted for over a week. it staarted again this evening, pls help as am frustrated. NB- i checked for apps update but all my apps are up-to-date. CT

    Try the usual steps: restart, reset, restore.
    http://support.apple.com/kb/HT1430
    http://support.apple.com/kb/HT1414
    If restoring using a backup doesn't help, try restoring the iPhone to factory settings and try for a while with just the Apple-included apps, to make sure it's not an issue with a third-party app. If it still has problems, it may have a hardware problem and will need to be sent to Apple.
    Regards.

Maybe you are looking for

  • Windows 8.1 and Illustrator CC 2014- performance computer

    so i've exhausted myself searching...i actually think this will be the first time i've ever had to ask a question on the forums for ANY adobe product...i generally can find an answer; it's also generally my fault! slow movement of objects in illustra

  • Q4_How to manage software material and BOM in SAP_High-tech Industry PLM Implementation Issues

    Hello, We are implementing SAP PLM for a high-tech company which designs and manufactures mobile phones (ODM). Since we are new in PLM consulting and we are facing with some implementation difficulties, we are looking for some solutions here. Questio

  • Question about how put a icon custom in toolbar.

    I have developed a accion in acrobat X but i need put  a icon in toolbar for execute this accion, i have put an icon but this icon only shows the window modify an accion, somebody know how custom a icon for execute my accion?

  • Best Safari for Mac OS 10.4. ......

    - Some naivete involved here. More incidentally, contextually, .. I'd lost general access to my Finder, .. and so, with this, and with having had a re-installation done, of my main OS. - at the time, 10.3.9, and now, 10.4, ... .. at this point, I'm t

  • Check mark

    Is there a way to view, in the main library, just the checked songs? This would make it easier for putting new songs into playlists.