473,614 Members | 2,076 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 2003
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
1799
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 allows me the option to either pick
3
3109
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 Excel Automatically Now I want to add a row to the opened spreadsheet at the top and add a title. How do I accomplish this? I am new to excel formatting, but I created a macro and the code to do this is: Rows("1:1").Select Selection.Insert...
19
12746
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 cmd = New SqlCommand(Sqlstmt, Conn) datareader = cmd.ExecuteReader()
5
31908
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
7981
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 currently in the table is deleted and new data is sent based on cell value on the Excel worksheet. So far, I can delete the old records and add new data. I just cannot seem to return results from the query "ProdActs1". Also, is it possible to specify...
3
3190
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 acQuery but the query stays open waiting for export parameters (filename, file location, file type) to be filled out be the user. And I'm not getting the query to close. Actually, it behaves quite strangely when I try to step through it (in that it...
3
6345
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. Could anyone modify the code for me? cheers Michiel
1
10486
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 trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
2
2075
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 as to how i can make the user enter his ownfile name and select the location where the file is to be written. I am attaching below my code: Private Sub Report_Click() On Error GoTo Err_Report_Click Dim db As DAO.Database
3
2945
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 spreadsheet and I want this to happen automatically without user receiving a prompt that the dataset already exists and do they want to replace it. Can I delete the file in my macro prior to outputing the query? If so how? Or is there a way I can...
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8579
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7093
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5540
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2568
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 we have to send another system
1
1747
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.