Solved

total column in query

Posted on 2014-12-14
5
407 Views
Last Modified: 2014-12-15
hi
i have ms access query
i know now we can select summary of the columns within the query
but is there any way to have conditional summary of those columns ?
for example
i have in my table
id
store_no
store_type
sales_amount

if i want to add 4 rows at the end of the query
1st row to have summary of columns for store_type= 1
2nd row to have summary of comuns for store_type= 2
3rd row to have summary of comuns for store_type= 3
4th row to have summary of comuns for store_type= 4

note :
i dont need this summary in ms access report  , i need it in ms access query
the purpose is , i'm formatting one excell sheet in same order of the ms access output query
i want to copy the query output , paste it in the formatted excell sheet ( including the summary by types)
instead of formating the cells in excell after exporting it
thanx
0
Comment
Question by:NiceMan331
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40498768
This can be done by adding a sort column to your query output and using union queries for the summary lines, but please post your actual query SQL statement.
0
 

Author Comment

by:NiceMan331
ID: 40498780
here is my sql statement

SELECT Basic_Date.Store, Basic_Date.B_Type, Basic_Date.City, Basic_Date.Address, Basic_Date.Op_Date, Basic_Date.Loc_Rent, Basic_Date.Land_Rent, Basic_Date.Owner, Basic_Date.Proj_Sales, Basic_Date.[1stDaySales], Basic_Date.[2ndDaySales], 

Basic_Date.CDbl(DSum("sales_amt","Yesterday_Sales","[store]= " & [store])) AS Yesterday_Sales, CDbl(DSum("sales_amt","Sales_trans","[store]= " & [store])) AS Total_Sales, CLng(DCount("sales_amt","Accepted_Trans","[store]= " & [store])) AS Days_Count, (Total_Sales/Days_Count) AS Daily_Avg, (nz([loc_rent],0)+nz([land_rent],0)+(nz([rent_percnt],0)*Daily_Avg*365))/365 AS Daily_Rent, CDbl(nz(DSum("asst_value","daily_depr","[rest] =" & [store]),0)) AS T_F_Asst, CDbl(nz(DSum("D_DEPR","daily_depr","[rest] =" & [store]),0)) AS Daily_Depr, Basic_Date.Rent_Percnt, [daily_depr]+[daily_rent] AS D_Rent_Dep, [d_rent_dep]/[daily_avg] AS Rent_Per_Sls
FROM Basic_Date
WHERE (((Basic_Date.IsOpen)=Yes))
ORDER BY Basic_Date.Store;

Open in new window


i need summry for those items
Basic_Date.CDbl(DSum("sales_amt","Yesterday_Sales","[store]= " & [store])) AS Yesterday_Sales, CDbl(DSum("sales_amt","Sales_trans","[store]= " & [store])) AS Total_Sales, CLng(DCount("sales_amt","Accepted_Trans","[store]= " & [store])) AS Days_Count, (Total_Sales/Days_Count) AS Daily_Avg, (nz([loc_rent],0)+nz([land_rent],0)+(nz([rent_percnt],0)*Daily_Avg*365))/365 AS Daily_Rent, CDbl(nz(DSum("asst_value","daily_depr","[rest] =" & [store]),0)) AS T_F_Asst, CDbl(nz(DSum("D_DEPR","daily_depr","[rest] =" & [store]),0)) AS Daily_Depr, Basic_Date.Rent_Percnt, [daily_depr]+[daily_rent] AS D_Rent_Dep, [d_rent_dep]/[daily_avg] AS Rent_Per_Sls

Open in new window


group by Basic_Date.B_Type

thanx
0
 
LVL 18

Accepted Solution

by:
Simon earned 300 total points
ID: 40498820
Sorry, the query is far more extensive than your initial example. I can't write it all out.

The basis of the method I would have used is:

Prepended your query output with a column with string value "Data"
union
"StoreType1" + a grouped version of your query with where clause to restrict to store-type=1
union
"StoreType2" + a grouped version of your query with where clause to restrict to store-type=2
union
"StoreType3" + a grouped version of your query with where clause to restrict to store-type=3

However, your query is rather too complex for me to handle without Access and the table in front of me, and uses domain aggregate functions which would make it perform horribly if called repeatedly in subqueries. I'd have to suggest that for your current query, you'd be far better off adding the summary lines in Excel. Sorry.
0
 

Author Comment

by:NiceMan331
ID: 40498834
ok thanx
any other suggestions from any experts
0
 
LVL 41

Assisted Solution

by:pcelba
pcelba earned 200 total points
ID: 40498851
You don't need so many unions...
Given table:
id  (numeric)
store_no  (numeric)
store_type
sales_amount
you just have to query details and add totals grouped by store_type:
SELECT id, store_no, store_type, sales_amount
  FROM YourTable
 WHERE <SomeFilterCondition>
UNION ALL
SELECT 0, 0, store_type, SUM(sales_amount)
  FROM YourTable
 WHERE <SomeFilterCondition>
 GROUP BY store_type

Open in new window

If you need conditional SUM() then simply put IIF() inside the SUM(). If you need certain output order enclose above query into parentheses and add sorting column.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now