Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / July 2006

Tip: Looking for answers? Try searching our database.

Summing top 3 results

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
polisci grad - 06 Jul 2006 16:02 GMT
I have a table for truck sales in canada by make of truck (there is about 8
makes).  The records are for monthly sales.  I would like to make a table
that has date descending in the left most column (in order to show most
recent results first), and a sum of the top three makes for that month in
column 2 (the top sellers change from month to month).  

The data is stored in a large data table (named DTA, with the fields
DTE-date, KEY-items key, and VLE-value) and the descriptions are stored in an
items table (named ITEMS).  These tables are linked on the item "key".  
Therefore, a simple select query would look like this:

SELECT dta.dte, items.key FROM....WHERE items.key like 'CLASS8C' & '*';

The CLASS8C line restricts results to only those items pertaining to class 8
truck sales in canada.  The keys for the various makes look like this:  
CLASS8C_FRT, CLASS8C_PTRBLT, etc....

So, in brief, I want the query to display the sum of the top three makes for
each month - how would I do this???

Thanks,
MGFoster - 06 Jul 2006 21:08 GMT
Brother, your naming conventions are very bad.  You should be naming
things for what they are not what they do.

DTE - Date of what:  sales date, birthdate, holiday?
Key - This describes what the column does in the DB, not what the data
is, and it is a SQL reserved word.
VLE - What type of value:  sales amount, item cost, neck size...?

Then your Items.Key values violates the 1NF (First Normal From):  "Each
cell should be atomic" - there should be only 1 item in each column
value.  "CLASS8C_FRT" represents 2 items: the Class 8 truck and the
truck type (FRT, PTRBLT, etc.).

Your table design should look something like this:

CREATE TABLE TruckSales (
  sales_date DATETIME NOT NULL,
  truck_class CHAR(10) NOT NULL,
  truck_type VARCHAR(10) NOT NULL,
  sales_amount DECIMAL(8,2) NOT NULL,
  CONSTRAINT PK_TruckSales
    PRIMARY KEY (sales_date, truck_class, truck_type)
)

"I would like to make a table ...."  Actually, you want a View (query in
Access-speak).

You really need to read a book on database design.  I recommend
_Database Design for Mere Mortals_ by Hernandez.

Having said all that, here is a solution to your problem, using your
column names (untested):

SELECT Month(dte) As SalesMonth, [Key] As TruckType,
       Sum(vle) As TotalSales
FROM DTA As D
WHERE dte BETWEEN #1/1/2006# AND #12/31/2006#
  AND [key] IN
  (SELECT TOP 3 [key]
   FROM (SELECT [key], Sum(vle) As Sales
         FROM DTA
         WHERE [key] Like "CLASS8C*"
           AND Month(dte) = Month(D.dte)
         GROUP BY [key]
         ORDER BY Sum(vle) DESC
         ) As B
   )
GROUP BY Month(dte), [Key]

Don't change to Design view once this is in the SQL view, 'cuz Access
will try to put []s where the ()s are.  This will cause an error when
the query runs.  If that happens just change the []s around the 2nd
SELECT statements in the IN () predicate expression to ()s.  Be sure to
change the ]. to ) - without the period.

There is a possibility of a tie in the TOP 3, which means the result
will have more than 3 "TruckTypes" w/ 2 or more that have equal total
sales amounts.
Signature

MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

> I have a table for truck sales in canada by make of truck (there is about 8
> makes).  The records are for monthly sales.  I would like to make a table
[quoted text clipped - 15 lines]
> So, in brief, I want the query to display the sum of the top three makes for
> each month - how would I do this???
polisci grad - 06 Jul 2006 21:37 GMT
Thanks for the help pertinent to my question, i expect that it will work - i
didnt know you could select records from another query all within the same
query.

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 76 lines]
> > So, in brief, I want the query to display the sum of the top three makes for
> > each month - how would I do this???
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.