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

Home Posts Topics Members FAQ

How to simulate the subtotal in Select or group by query ?

215 New Member
I want to simulate the pivot table.
how to sum the subtotal of the father field, like this

ID - SubID - SumOfSubID -- TotalbyID
1 --- A ------- 11 ------------------------ 95
1 --- B ------- 13 ------------------------ 95
1 --- C ------- 71 ------------------------ 95
2 --- T ------- 33 ------------------------ 55
2 --- H ------- 22 ------------------------ 55

how to calculate the TotalbyID like this (ex: ID = 1 then TotalByID = 11+13+71) ?
At the moment I'm using stacked query, but how can I do this with subquery or any method faster ?
Oct 16 '15 #1
21 3123
Rabbit
12,516 Recognized Expert Moderator MVP
This is the first time I've heard the phrase stacked query. What is that?

One option to emulate subtotals is to union in an aggregate query that groups by your subtotal category.
Oct 16 '15 #2
NeoPa
32,557 Recognized Expert Moderator MVP
It seems to me that you can either use linked queries, as you are, or Domain Aggregate functions such as DSum(). I expect what you have is already the faster of the two anyway.
Oct 16 '15 #3
NeoPa
32,557 Recognized Expert Moderator MVP
Rabbit:
This is the first time I've heard the phrase stacked query. What is that?
I assumed the OP was referring to an aggregated subquery linked to the main data source on the common fields (In this case just [ID]).
Oct 16 '15 #4
hvsummer
215 New Member
@Rabbit: Stacked query is the query used inside another query, sound like traped query. let me see, UNION can combine 2 query and I don't have to play a query inside another ? ==

@NeoPa: Dsum() and his relative are whom I want avoid the most,
they slow me down too much haha
I'll try rabbit's suggestion, union, but I have never use UNION before lol, let see if this can be done.
Oct 16 '15 #5
NeoPa
32,557 Recognized Expert Moderator MVP
HVSummer:
@NeoPa: Dsum() and his relative are whom I want avoid the most
Very wise. Note I was not suggesting you try that as I assumed what you had was already better.

HVSummer:
I'll try rabbit's suggestion, union, but I have never use UNION before lol, let see if this can be done.
If you haven't played with UNION queries before then certainly go off and explore them. They can be very powerful and add flexibility.

In this case, I don't see where the benefit would come from above and beyond the use of subqueries (Subqueries in SQL), which is what I assume you started with.

Having said that, Rabbit is certainly extremely clever, and particularly so when it comes to SQL, so I expect there's something I've missed.
Oct 16 '15 #6
hvsummer
215 New Member
well, UNION must have the same select field's number, and it work like
append query, append bonus query into a query ==
if I use subquery, how to do it :-? (usually stacked query faster than subquery, but I'll try once, i won't let this simple task take away 2 slot queries from me ==)
my code uptill now (subquery method):
Expand|Select|Wrap|Line Numbers
  1. SELECT A.ID, A.SUBID, SUM(A.VOLUME) AS SUMOFSUBID, B.TOTALBYID
  2. FROM A INNER JOIN 
  3. (SELECT ID, SUM(A.VOLUME) AS TOTALBYID FROM A) AS B ON A.ID = B.ID
  4. ORDER BY A.ID;
  5.  
Oct 16 '15 #7
NeoPa
32,557 Recognized Expert Moderator MVP
I'd be looking at something pretty similar.
Expand|Select|Wrap|Line Numbers
  1. SELECT   [A].[ID]
  2.        , [A].[SUBID]
  3.        , SUM([A].[VOLUME]) AS [SUBIDVOLUME]
  4.        , [B].[IDVOLUME]
  5. FROM     [A]
  6.          INNER JOIN
  7.          (SELECT   [ID]
  8.                  , SUM([VOLUME]) AS [IDVOLUME]
  9.           FROM     [A]
  10.           GROUP BY [ID]) AS [B]
  11.   ON     [A].[ID]=[B].[ID]
  12. GROUP BY [A].[ID]
  13.        , [B].[ID]
  14.        , [A].[SUBID]
  15.        , [B].[IDVOLUME]
  16. ORDER BY [A].[ID]
  17.        , [A].[SUBID]
Oct 16 '15 #8
hvsummer
215 New Member
wow, It did work, Thank NeoPA, you showed me that I forgot "group by" in a group by query == shame on me haha.

Tested on simple DB. next, apply this model to complex one lol

btw, I'll try to study UNION method, uptonow I only see it as a Append query-to-query from what I can find on Google.

last problem if use UNION looklike this


really simulated subtotal, but no-use to next step ^^, still thank Rabbit.

Attached Images
File Type: png 1.PNG (5.2 KB, 1322 views)
Oct 16 '15 #9
zmbd
5,501 Recognized Expert Moderator Expert
On a side note, from good ole Allen Brown
http://allenbrowne.com/subquery-02.html

Note the differentiation of "stacked" queries vs. subquery.
In Access the "stacked query" is stored query that is referred to via a second query, third, etc...

The subquery we all know and love is where the SQL is created within the primary sql.

(AB has a related article here: http://allenbrowne.com/QueryPerfIssue.html )

I often start by building the individual queries for troubleshooting each piece, stacking as I go, and then combine for use in code... or just to tidy things up.

The issue I have with this is that stacking queries is a favorite SQL-Injection method used against RDMS (or any DBE) that is capable of running async multiple queries... or, well, so, I've been told...
Oct 16 '15 #10
NeoPa
32,557 Recognized Expert Moderator MVP
HVSummer:
Thanks NeoPa
You're very welcome. I'm glad I was able to nudge you a little in the right direction.
ZMBD:
In Access the "stacked query" is stored query that is referred to via a second query, third, etc...
I'm curious where that term comes/came from. I've never heard a QueryDef object referred to as a stacked query before.
Oct 17 '15 #11
Rabbit
12,516 Recognized Expert Moderator MVP
Sorry, the union wouldn't work. I misread the requirements. I thought the subtotals needed to be on their own row.
Oct 18 '15 #12
hvsummer
215 New Member
@Rabbit:
well, last time I want to simulate Subtotal,
but this time I found the way to do Subtotal, real subtotal with UNION
Expand|Select|Wrap|Line Numbers
  1. SELECT ID, SubID, Sum(Volume) as MTD
  2. FROM Table1
  3. GROUP BY ID, SubID
  4. ORDER BY ID, SubID
  5. UNION ALL
  6. SELECT ID, "SubTotal "&Group as SubID, Sum(Volume) as MTD
  7. FROM Table1
  8. GROUP BY ID
  9. ORDER BY ID, SubID;
  10.  
Funny that, Idea just pop up when I answer another request lol
Nov 23 '15 #13
Rabbit
12,516 Recognized Expert Moderator MVP
hvsummer, that's what my suggestion was in post #2. Except your sample results didn't match what your query would create, which is why I withdrew my suggestion and NeoPa made his suggestion.

But also, this kind of stuff is handled better at the report level because it allows for better formatting options.
Nov 23 '15 #14
hvsummer
215 New Member
@Rabbit: better formating is great, but user may want to calculate on the subtotal sothat, query sometime still usefull
Nov 24 '15 #15
Rabbit
12,516 Recognized Expert Moderator MVP
The reporting option also calculates subtotals, I find that 99% of the time, the reporting option is the preferred method.
Nov 24 '15 #16
hvsummer
215 New Member
yes, unlucky I'm the rest 1%..
coz I calculate everything in access, can do this ezier in excel, but I don't want setup too much steps. I need subtotal or grantotal as a part of fomular sothat reporting option does not help, I have to generate result (fomular relative to subtotal like Volum-weighted) with query first.

Sometime people have to do that too, so I think we should findout their need to fullfill it rather than give "our best direction" every question.
Nov 24 '15 #17
Rabbit
12,516 Recognized Expert Moderator MVP
The "best direction" should always take into account the user's need. If they need it in query form and have a valid reason, then I would give them the 1% solution. If they are unclear or don't need it calculated within the query, then the 99% solution is the answer I would give them.
Nov 24 '15 #18
NeoPa
32,557 Recognized Expert Moderator MVP
HVSummer:
yes, unlucky I'm the rest 1%.
Just to be clear, it's the use case rather than the user that determines what is appropriate for any given scenario. It's possible for a designer to provide a solution that doesn't match the requirements very well. There are always many ways to provide a solution.

Nevertheless, there are sometimes cases when the 1% is needed. That will be clear because the 99% won't work for some reason. It won't be connected in any way with who the developer is. If your choice is to go with the 1% regardless of the use case then that simply means you're not doing it the most appropriate way.

We will always suggest, to the best of our ability, the most appropriate way. We see that as our responsibility. What you do from there is your own lookout. We see that as yours.
Nov 25 '15 #19
hvsummer
215 New Member
yea, I know what is the right direction for most of people, and you guy help me much when resolved my funny problem.

the reason i don't use report or form or anything else other than query is that I'm using Access as back-end, excel as front-end, I pull all data from access and show up in excel.

and the real reason, I'm lazy to calculate with excel :p
just want to calculate everything within query then pull out the result. It's hard, but it's worth for me.
Nov 25 '15 #20
NeoPa
32,557 Recognized Expert Moderator MVP
Indeed. Depending on various parameters of what you need the spreadsheet for and how you're using it, that could make it one of the 1%. Essentially that's for you to decide anyway - not if it's a 1% but rather what you want to do.

As long as we've given the advice we're happy with then it's fine to explore other options.
Nov 26 '15 #21
Rabbit
12,516 Recognized Expert Moderator MVP
Well, excel can calculate it for you if you feed the results of the query into a pivot table
Nov 26 '15 #22

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

Similar topics

0
2929
by: cooldv | last post by:
i have an *access database + ASP newsletter* that is working fine and it sends newsletter to ALL the email addresses in the DB. i want to send this newsletter to only a select group (like people...
1
2603
by: sks | last post by:
Hi, I have a table which logs for sake of argument page hits: id, timestamp, ipaddress Now, I'm running a query to get an activity report and I want to group together all the hits from a...
5
6622
by: Terri | last post by:
The following query will give me the top ten states with the most callers. SELECT TOP 10 Count(Callers.CallerID) AS CountOfCallerID, Callers.State FROM Callers GROUP BY Callers.State ORDER BY...
3
354
by: | last post by:
Is a SELECT TOP query allowed in filtering rows from a datatable? If it is not, is there any other easy way to filter a given number of rows from a datatable? Thanks a lot for your help.
3
3069
lwwhite
by: lwwhite | last post by:
I've got a a datasheet subform that gets its data from a SELECT DISTINCT query. The data is not editable. When I remove the DISTINCT qualifier from the query, the data is editable, but of course I...
5
3959
by: mebrabham | last post by:
Hello, I am trying to create a running subtotal in a query and then graph the running subtotal for each city group. The data looks like this in the table (for illustration): My City My...
2
1927
by: murch.alexander | last post by:
Hi all, I have a query that uses a subquery to subtotal one of its fields. This part is working fine. The twist is that when I encounter certain values, I want to reset the subtotal to zero and...
2
8729
by: pedalpete | last post by:
I seem to have a few duplicates entries in a very large database. My database has two identical tables. One is written to on a regular basis (every few seconds) and the other is used for queries,...
4
1521
by: fran7 | last post by:
Hi, I wonder if anyone has any pointers to this. I have a page querying cities from my database. fields to query in database country australia uk uk
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
7165
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,...
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
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.