473,394 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Folding subtotals into query?

Is is possible, via some clever sql coding ( perhaps with PL/pgsql)
to get subtotals to appear in a selection, ie

If I have a query: select * from checks order by category
I would like the have the subtotals appear (possibly in
an unused column for each "category" when the category
"breaks".

Basically I would like to meld the query:
select category, sum(amount) from checks group by category order by
category

Into the of the first select.

Thanks,

Jerry
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
4 3473
On Apr 18, 2004, at 2:41 PM, Jerry LeVan wrote:
Is is possible, via some clever sql coding ( perhaps with PL/pgsql)
to get subtotals to appear in a selection, ie

If I have a query: select * from checks order by category
I would like the have the subtotals appear (possibly in
an unused column for each "category" when the category
"breaks".

Basically I would like to meld the query:
select category, sum(amount) from checks group by category order by
category


I think you want to do something like this:

SELECT *, (SELECT sum(amount) FROM checks AS x WHERE x.category =
checks.category GROUP BY x.category) AS total
FROM checks
ORDER BY category;

This will give you a column named "total" for every row in checks. The
value will be the sum(amount) for the corresponding category. You'll
likely want an index on checks.category to get any level of tolerable
performance out of the query.

eric
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
Wow, much faster

Jerry

On Apr 18, 2004, at 4:20 PM, Eric Ridge wrote:
On Apr 18, 2004, at 4:07 PM, Jerry LeVan wrote:
That does the job, for 3200 checks it does chug for a while, too bad
it can't remember the intermediate results :)


hmm... Can do this via a left join too. Much faster:

SELECT checks.*, x.sum
FROM checks
LEFT JOIN (SELECT category, sum(amount) AS sum FROM checks GROUP
BY category) AS x ON x.category = checks.category
ORDER BY category, sum

eric

--Jerry

On Apr 18, 2004, at 3:10 PM, Eric Ridge wrote:
On Apr 18, 2004, at 2:41 PM, Jerry LeVan wrote:

Is is possible, via some clever sql coding ( perhaps with PL/pgsql)
to get subtotals to appear in a selection, ie

If I have a query: select * from checks order by category
I would like the have the subtotals appear (possibly in
an unused column for each "category" when the category
"breaks".

Basically I would like to meld the query:
select category, sum(amount) from checks group by category order by
category

I think you want to do something like this:

SELECT *, (SELECT sum(amount) FROM checks AS x WHERE x.category =
checks.category GROUP BY x.category) AS total
FROM checks
ORDER BY category;

This will give you a column named "total" for every row in checks.
The value will be the sum(amount) for the corresponding category.
You'll likely want an index on checks.category to get any level of
tolerable performance out of the query.

eric

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
That does the job, for 3200 checks it does chug for a while, too bad
it can't remember the intermediate results :)

--Jerry

On Apr 18, 2004, at 3:10 PM, Eric Ridge wrote:
On Apr 18, 2004, at 2:41 PM, Jerry LeVan wrote:
Is is possible, via some clever sql coding ( perhaps with PL/pgsql)
to get subtotals to appear in a selection, ie

If I have a query: select * from checks order by category
I would like the have the subtotals appear (possibly in
an unused column for each "category" when the category
"breaks".

Basically I would like to meld the query:
select category, sum(amount) from checks group by category order by
category


I think you want to do something like this:

SELECT *, (SELECT sum(amount) FROM checks AS x WHERE x.category =
checks.category GROUP BY x.category) AS total
FROM checks
ORDER BY category;

This will give you a column named "total" for every row in checks.
The value will be the sum(amount) for the corresponding category.
You'll likely want an index on checks.category to get any level of
tolerable performance out of the query.

eric

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
On Apr 18, 2004, at 4:07 PM, Jerry LeVan wrote:
That does the job, for 3200 checks it does chug for a while, too bad
it can't remember the intermediate results :)
hmm... Can do this via a left join too. Much faster:

SELECT checks.*, x.sum
FROM checks
LEFT JOIN (SELECT category, sum(amount) AS sum FROM checks GROUP BY
category) AS x ON x.category = checks.category
ORDER BY category, sum

eric

--Jerry

On Apr 18, 2004, at 3:10 PM, Eric Ridge wrote:
On Apr 18, 2004, at 2:41 PM, Jerry LeVan wrote:
Is is possible, via some clever sql coding ( perhaps with PL/pgsql)
to get subtotals to appear in a selection, ie

If I have a query: select * from checks order by category
I would like the have the subtotals appear (possibly in
an unused column for each "category" when the category
"breaks".

Basically I would like to meld the query:
select category, sum(amount) from checks group by category order by
category


I think you want to do something like this:

SELECT *, (SELECT sum(amount) FROM checks AS x WHERE x.category =
checks.category GROUP BY x.category) AS total
FROM checks
ORDER BY category;

This will give you a column named "total" for every row in checks.
The value will be the sum(amount) for the corresponding category.
You'll likely want an index on checks.category to get any level of
tolerable performance out of the query.

eric

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nuff Said | last post by:
Question: Is there a way to unfold a Python class *without* unfolding all its methods etc. so that you can get a quick overview of the class? Details: I normally open a file either with...
2
by: Scott | last post by:
How can you show SUBTOTALS in the same column as the data, NOT in a separate column? I need to SUBTOTAL in the same column as one of the the data columns, like the subtotals 700 and 800 under...
3
by: John Baker | last post by:
Hi: I have a situation where I have a series of time records (one or more per day) for a number of projects. Each record has an identification for the activity conducted during the period...
2
by: Richard Grene | last post by:
I am creating an Excel spreadsheet using a query function in my vb code. This works fine. I can format the columns but dont know how to create subtotals and a grand total. Thanks, Richard
3
by: Greg | last post by:
I have a database that tracks the vacation time of each employee of the company that I work for. Everything works great and totals all of the vacation time that has been used. Could someone give me...
14
by: John Salerno | last post by:
Specifically, I'm using UltraEdit and perhaps there's no way perfect way to implement code folding with it, given how it uses its syntax highlighting file to do so (i.e., you have to specify an...
0
by: Paul Ilacqua | last post by:
I'm using VB .NET and I'm trying to "roll my own" report subtotals. I'm building a string from a datareader top pass to a reporting class and I want to break and subtotal QTY at each change in...
2
by: =?Utf-8?B?SHV0dHk=?= | last post by:
I have searched the net but have been unable to find a solution to subtotaling in a gridview. I have a gridview that is bounded to sqldatasource where I would like to subtotal rows throughout the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.