473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help With Query to Get Totals Sorted by Month & Year

164 Expert 100+
I am trying to figure out a query. I believe it is possible, but my SQL knowledge has been waning in the past year due to disuse. Anyone wish to help?

I have three tables.

Table: Fruits

Expand|Select|Wrap|Line Numbers
  1. FruitID  |  FruitName
  2. -----------------------
  3. 1           Orange
  4. 2           Bananas

Table: Customers

Expand|Select|Wrap|Line Numbers
  1. CustomerID  |  CustomerName
  2. ----------------------------
  3. 1              Bill
  4. 2              Jim

Table: CustomerOrders

Expand|Select|Wrap|Line Numbers
  1. OrderID  |  FruitType  |  QuantityBought  |  CustomerID  |  DateBought
  2. -------------------------------------------------------------------------
  3. 1            1             15                1             2010-04-02
  4. 1            1             90                1             2010-04-04
  5. 1            1             31                1             2010-06-06
  6. 1            1             26                1             2011-06-02
  7. 1            1             16                1             2011-07-04
  8. 1            1             40                1             2011-08-02
  9. 1            2             15                1             2010-04-02
  10. 1            2             90                1             2010-05-04
  11. 1            2             31                1             2010-06-06
  12. 1            2             26                1             2011-06-02
  13. 1            2             16                1             2011-07-04
  14. 1            2             40                1             2011-08-02


My goal is to get a query that would return a result like such:

Results:

Expand|Select|Wrap|Line Numbers
  1. FruitID  | Customer  | QuantityBought  | MonthBought  |  YearBought
  2. ------------------------------------------------------------------------
  3. 1          Bill             105                  4            2010
  4. 1          Bill             31                   6            2010

So far, I have come up with this, but it isn't right (else I wouldn't be here!).

Expand|Select|Wrap|Line Numbers
  1. SELECT FruitID, CustomerName, SUM(QuantityBought), MONTH(DateBought), YEAR(DateBought) From CustomerOrders, Customers WHERE Customers.CustomerID = CustomerOrders.CustomerID AND YEAR(DateBought) >= 2010
Apr 19 '11 #1
1 1349
gpl
152 100+
Try this, the group by will order the results, Im not sure how you want to order the results from your example
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. FruitID, 
  3. CustomerName, 
  4. SUM(QuantityBought), 
  5. MONTH(DateBought), 
  6. YEAR(DateBought) 
  7. From CustomerOrders inner join Customers on Customers.CustomerID = CustomerOrders.CustomerID 
  8. WHERE YEAR(DateBought) >= 2010
  9. GROUP BY 
  10. FruitID, 
  11. CustomerName, 
  12. YEAR(DateBought), 
  13. MONTH(DateBought) 
Apr 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Laguna | last post by:
Hi Gurus, I want to find the expiration date of stock options (3rd Friday of the month) for an any give month and year. I have tried a few tricks with the functions provided by the built-in...
2
by: Mary | last post by:
I am trying to develop a query which will determine the average costs using a rolling average of the past 12 months of data. In other words, if I entered the Ship Month of January and the Ship...
2
by: Catherine | last post by:
I am trying to create a report that will give me q total count by month to date, and then another by year to date. Example: Discharges By Type Month To Date Year To Date...
8
by: tfsmag | last post by:
i need to create a date range based on the current "shown" month on a calendar control to query a database and populate a datagrid based on that date range. how can i retrieve the "shown" month...
6
by: Burghew | last post by:
Hello, I generate invoices for my customers evry month. I want to keep a form which will allow the user to select the Month and Year through a combo and thus generate reports based on the month...
3
by: kiranvr | last post by:
Hi, I've to get the results from a single table for month and year-to-date by a single query. I've two columns monthstartdate and monthenddate. If it is only for month I can assign the values and...
3
by: Johan Mcgillicutty | last post by:
Could use some help on this one- I’m new to MS Access, and even newer to SQL. I’m building a database for clients of a treatment center, and need to keep track of when people are due for their...
2
by: =?Utf-8?B?Sm9lIFRhdmFyZXM=?= | last post by:
I am using the DateTime.Parse method to parse user entered dates. The dates can be just the year, Month-year and Month-day-year in a variety of formats in a variety of cultures. I immediately parse...
4
by: smugcool | last post by:
hi, i have a form in which a text field txtDate is there. Where user fills up a date. suppose sombody enters a date 23.oct.07 in this text field, i wanted to get only month and year...
2
by: bnashenas1984 | last post by:
Hi everyone I'm building an Ecommerce website with PHP and MYSQL. What I need to do is to categorize orders for each month or year to show a statistic table The DB table looks like this : ID :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.