473,395 Members | 1,535 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,395 software developers and data experts.

How to get subtotal after every 20th row in sql server

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.
Apr 29 '15 #1
2 1602
Rabbit
12,516 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     FIELD1,
  3.     SUM(ROWNUM) AS SumOfRownum
  4.  
  5. FROM (
  6.     SELECT 1 AS ROWNUM, 'a' AS FIELD1 UNION ALL
  7.     SELECT 2, 'b' UNION ALL
  8.     SELECT 3, 'c' UNION ALL
  9.     SELECT 4, 'd' UNION ALL
  10.     SELECT 5, 'e' UNION ALL
  11.     SELECT 6, 'f' UNION ALL
  12.     SELECT 7, 'g' UNION ALL
  13.     SELECT 8, 'h' UNION ALL
  14.     SELECT 9, 'i' UNION ALL
  15.     SELECT 10, 'j'
  16.     ) AS t
  17.  
  18. GROUP BY
  19.     ROLLUP(
  20.         (ROWNUM - 1) / 5 + 1,
  21.         FIELD1
  22.     )
Expand|Select|Wrap|Line Numbers
  1. FIELD1    SumOfRownum
  2. a    1
  3. b    2
  4. c    3
  5. d    4
  6. e    5
  7. NULL    15
  8. f    6
  9. g    7
  10. h    8
  11. i    9
  12. j    10
  13. NULL    40
  14. NULL    55
Apr 29 '15 #2
ck9663
2,878 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. with x
  2. as
  3. (
  4. select 
  5.    rowid = row_number() over (order by UseAColumnThatYouCanUseToSort),
  6.    groupid = row_number() over (order by UseAColumnThatYouCanUseToSort)/20,
  7.    AnyNumericColumn
  8. from YourTable
  9. )
  10. select 
  11.    GroupId, sum(AnyNumericColumn)
  12. from x
  13. group by groupid
  14.  
Happy Coding!!!


~ CK
Apr 30 '15 #3

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

Similar topics

0
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...
7
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...
6
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...
1
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) ...
2
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...
8
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...
0
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}...
9
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...
2
Seth Schrock
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
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.