dear all,
I have 2000 rows in my table and i want to have subtotal after every 20th row could anyone help me i do not know how to do it.
thanks in advance.
2 1603
First, you need a sequential number you can use to number the rows. If you don't have one, you can use the ROW_NUMBER() function or a ranking query.
After you have a sequential number, you can do math on that number to break it up into groups of 20. Then you can use the ROLLUP functionality of SQL Server to get subtotals on that new group field.
A simplified example is shown below - SELECT
-
FIELD1,
-
SUM(ROWNUM) AS SumOfRownum
-
-
FROM (
-
SELECT 1 AS ROWNUM, 'a' AS FIELD1 UNION ALL
-
SELECT 2, 'b' UNION ALL
-
SELECT 3, 'c' UNION ALL
-
SELECT 4, 'd' UNION ALL
-
SELECT 5, 'e' UNION ALL
-
SELECT 6, 'f' UNION ALL
-
SELECT 7, 'g' UNION ALL
-
SELECT 8, 'h' UNION ALL
-
SELECT 9, 'i' UNION ALL
-
SELECT 10, 'j'
-
) AS t
-
-
GROUP BY
-
ROLLUP(
-
(ROWNUM - 1) / 5 + 1,
-
FIELD1
-
)
- FIELD1 SumOfRownum
-
a 1
-
b 2
-
c 3
-
d 4
-
e 5
-
NULL 15
-
f 6
-
g 7
-
h 8
-
i 9
-
j 10
-
NULL 40
-
NULL 55
Be careful about this. Depending if you have an index or not and the type of index, your 20th row may varies.
Nevertheless Rabbit's code will work.
You may also try this: -
with x
-
as
-
(
-
select
-
rowid = row_number() over (order by UseAColumnThatYouCanUseToSort),
-
groupid = row_number() over (order by UseAColumnThatYouCanUseToSort)/20,
-
AnyNumericColumn
-
from YourTable
-
)
-
select
-
GroupId, sum(AnyNumericColumn)
-
from x
-
group by groupid
-
Happy Coding!!!
~ CK
Sign in to post your reply or Sign up for a free account.
Similar topics
by: someone |
last post by:
I am in a situation where I need to package some information from
Page1, submit it via POST to another server which will process the
information and then send the user to another page on my server...
|
by: Razzie |
last post by:
Hey all,
I need to connect to an SMTP server and get a notification when it gets a
new mail to send. I want to be able to 'read' that email (retrieve recipient
information for example).
Is that...
|
by: someone |
last post by:
I am in a situation where I need to package some information from
Page1, submit it via POST to another server which will process the
information and then send the user to another page on my server...
|
by: ViRi |
last post by:
My General Goal is to add e-mail functionality to my program without
using external (smtp) servers. I tried the following:
Lookup MX record: example: mx.home.nl (when looking up home.nl)
...
|
by: Tim |
last post by:
Hi all,
I have a problem with SMTPMail as I want to evaluate the respone of the
server, but unfortunately I could neither on the web nor in the MSDN
find an example of it.
There is no problem...
|
by: p3t3r |
last post by:
I am using .NET2 and have a number of aspx pages. On each page is a
LinkButton that performs a server.transfer() to another page.
If we use page names A,B,C,D,E as an example. I start on page A...
|
by: ardatun |
last post by:
Hi,
I add rows and cells to a table control runtime.
What I want to do is to have page breaks every 20th rows.
I use a page-break style like this:
<style>
P.pbr {page-break-before: always}...
|
by: Carey Carlan |
last post by:
Can the bright minds here point me to the java tools best suited to
interfacing a C++ library with the widest range of web servers?
Fundamental question from a Java newbie. I'm about to start a...
|
by: Seth Schrock |
last post by:
I'm trying to add a data source to my computer through the ODBC Data Source wizard. However, I keep getting an error that says
Connection Failed:
SQLState: '01000'
SQL Server Error: 2...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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: 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,...
| |