473,498 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Advanced Query Outputting to Excel

10 New Member
Hi,

I'm using the following code to find the total sales of each product to each customer, for a user choosen month.

Expand|Select|Wrap|Line Numbers
  1. SELECT HistoryHeader.DelAddress01 as Shop, HistoryLines.Description, Sum(HistoryLines.DiscountAmount) AS TotalQty
  2. FROM CustomerMaster INNER JOIN (HistoryHeader INNER JOIN HistoryLines ON HistoryHeader.DocumentNumber=HistoryLines.DocumentNumber) ON CustomerMaster.CustomerCode=HistoryHeader.CustomerCode
  3. WHERE datepart("m",[HistoryLines.DDate])=[Enter month number Jan=1,feb=2,etc] And Year([HistoryLines.DDate])=Year(Date())
  4. GROUP BY HistoryHeader.DelAddress01,HistoryLines.Description;
  5.  
Now this works fine, however, what i really need is to be able to run this query 12 times each time changing the month so i can look at the past 12 months. And feed the results into excel and into a standard report. So i can end up with a table that will enable me to compare one month with another.


ie like the following table

Customer, product, JanTotals, FebTotals, .....DecTotals

I have no idea where to start this, or even it can be automated. Any help would be greatly appreciated.

Alex
Apr 6 '11 #1
3 1998
Rabbit
12,516 Recognized Expert Moderator MVP
Rather than filtering by month, why not group by it instead? Then you just need to export one query and it will be split up by month.
Apr 6 '11 #2
AlexMwells
10 New Member
Hi Rabbit,

Thank you very much for your response. Grouping by month was a very good idea. For anyone who stores their accounting data in Pastel Partner 2009 the following code should work.

Expand|Select|Wrap|Line Numbers
  1. SELECT datepart("m",[HistoryLines.DDate]) as Month,CustomerMaster.CustomerCode, HistoryHeader.DelAddress01, HistoryHeader.DelAddress02, HistoryLines.Description, Sum(HistoryLines.Qty) as TotalQty, Sum(HistoryLines.DiscountAmount) AS TotalSale
  2. FROM CustomerMaster INNER JOIN (HistoryHeader INNER JOIN HistoryLines ON HistoryHeader.DocumentNumber=HistoryLines.DocumentNumber) ON CustomerMaster.CustomerCode=HistoryHeader.CustomerCode
  3. WHERE Year([HistoryLines.DDate])=Year(Date())
  4. GROUP BY datepart("m",[HistoryLines.DDate]), CustomerMaster.CustomerCode, HistoryHeader.DelAddress01,HistoryHeader.DelAddress02, HistoryLines.Description;
  5.  
Thanks Again

Alex
Apr 7 '11 #3
AlexMwells
10 New Member
Thanks Daniel,

I am in the process of downloading it. It will take 1hr 8min. Thats living in Africa for you.

Alex
Apr 7 '11 #4

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

Similar topics

1
1785
by: GERALD SHAW | last post by:
ACC97... I have a database which is connected to an excel spread sheet using MS query.. Using Access, I am used to setting up within a queries criteria, the following is Null Or this...
3
3084
by: kscheu via AccessMonster.com | last post by:
I am using OutputTo to export a query to excel, prompt the user to save as and then open the excel file. See below: DoCmd.OutputTo acOutputQuery, "q701ExportFields", acFormatXLS, , True 'Opend...
19
12720
by: wreckingcru | last post by:
I'm trying to output a SQL query that is constructed thru my VB.net GUI into an excel file. Here is the code I'm using: 'Sqlstmt is the SQL query statement 'Conn is the SQL Connection object...
5
31883
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
3
7966
by: austin1539 | last post by:
I am trying to run an Access query from Excel. The query, called "ProdActs1" works in Access and is run from using information from a table called "Queries". When a button is clicked in Excel, data...
3
3179
jamjar
by: jamjar | last post by:
Trying to Export the results of a Query to Excel without having the user have to leave the Switchboard. Tried DoCmd.OpenQuery "myQuery" DoCmd.RunCommand acCmdExport DoCmd.Close...
3
6339
by: Remaniak | last post by:
Hi All, I use the code below in my form to filter data. But I also need to export the data to excel. So I'd like to create a temporary query based on the strWhere and export that query to excel....
1
10464
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
2
2068
by: Comandur | last post by:
Hi, I am trying to export an access query to excel. I have made use of transferspreadsheet command to achive this. However i have hardcoded the path and the filename in the VBA code. I am not sure...
3
2937
by: JimCarlson | last post by:
I've created a macro in Access 2003 to output a query into Excel. I have scheduled a Windows XP task to run daily to run the macro. I want the current values in the query to overwrite the current...
0
7002
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
7205
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...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.