---
title: "How to get Cumulative frequency in SQL Server"  
description: "How to get Cumulative frequency in SQL Server"  
author: "Anonymous User"  
published: 2012-09-10  
updated: 2026-05-15  
canonical: https://www.mindstick.com/forum/446/how-to-get-cumulative-frequency-in-sql-server  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# How to get Cumulative frequency in SQL Server

Hello Everyone, \
I've a small [requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge); Actually I've a [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) which has three [column name](https://www.mindstick.com/forum/369/how-can-i-add-a-column-name-to-my-grid) such as ID, Product_Name and Product_Quantity, these columns contains [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) like:\
ID Product_Name Product_Quantity\
1 Parle G 10\
2 [Hair Oil](https://yourviews.mindstick.com/view/87902/hair-oil-market-is-it-necessary) 20\
3 Soap 30\
4 Shampoo 49\
\
In this table I want to add another column name like Product_CumulativeValue which contains [cumulative](https://www.mindstick.com/forum/161441/what-is-cumulative-layout-shift-cls) value of Product_Quantity column, so can you tell me how to find out the cumulative value of Product_Quantity column. \
\
Please [resolve](https://www.mindstick.com/forum/159427/windows-app-dll-not-found-resolve-library-issue) my [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) as soon as possible.\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!\

## Replies

### Reply by Anonymous User

Hi AVADHESH PATEL,\
Hurrah!!! **This code resolve my problem**. Thank you very much Avadhesh. Keep it up.\

### Reply by AVADHESH PATEL

--Calculate Cumulative Frequency in SQL Server\
--Create DatabaseCREATE DATABASE [AldSalesPvtLtd]GO\
--Use DatabaseUSE [AldSalesPvtLtd]GO\
--Create TableCREATE TABLE [dbo].[Product]([ID] INT PRIMARY KEY,[Product_Name] VARCHAR(50) NOT NULL,[Product_Quantity] INT NOT NULL)GO\
--Insert Record into Student TableINSERT INTO [dbo].[Product]([ID],[Product_Name],[Product_Quantity])SELECT 1,'Parle G',10UNION ALLSELECT 2,'Hair Oil',20UNION ALL SELECT 3,'Soap',30UNION ALLSELECT 4,'Shampoo',40GO\
-- Display Record of Student TableSELECT [ID],[Product_Name],[Product_Quantity] FROM [dbo].[Product]GO\
-- Create Temporary TableCREATE TABLE #Product([ID] INT PRIMARY KEY,[Product_Name] VARCHAR(50) NOT NULL,[Product_Quantity] INT NOT NULL,[Product_CumulativeValue] INT NOT NULL --[Product_CumulativeValue] [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) Stored Calculated Cumulative Frequency result)GO\
-- Declare Variables and set valuesDECLARE @ID INT, @Product_Name VARCHAR(50), @Product_Quantity INT, @Product_CumulativeValue INT SET @Product_CumulativeValue = 0\
-- Create CorsorDECLARE CF_Cursor CURSORFORSELECT [ID],[Product_Name],[Product_Quantity]FROM [dbo].[Product]\
--Open CorsorOPEN CF_Cursor\
-- Select Record from cursor and stored into variablesFETCH NEXT FROM CF_Cursor INTO @ID, @Product_Name,@Product_Quantity\
--@@FETCH_STATUS Returns the status of the last cursor FETCH statement issued against any cursor currently opened by the connection.WHILE @@FETCH_STATUS = 0 -- 0 indicate FETCH statement was successful. BEGIN SET @Product_CumulativeValue = @Product_CumulativeValue + @Product_Quantity INSERT #Product VALUES (@ID,@Product_Name,@Product_Quantity,@Product_CumulativeValue) FETCH NEXT FROM CF_Cursor INTO @ID, @Product_Name,@Product_Quantity END\
-- Close CursorCLOSE CF_Cursor-- Deallocate CursorDEALLOCATE CF_Cursor\
-- Select * from Temporary Table #StudentSELECT * FROM #ProductGO


---

Original Source: https://www.mindstick.com/forum/446/how-to-get-cumulative-frequency-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
