Solved

SQL Server Select on LT running Totals

Posted on 2016-08-16
6
79 Views
Last Modified: 2016-08-17
In the sql below I get a count  by CampaignID

What I would like to do is set an update and set vendorServer = 1 on all rows where the running total < 125,000

SQL Server version is 2012


SELECT CampaignID, CurrentCount, vendorServer
FROM   dbo.Campaigns
WHERE  schedStatus = 'ACTIVE' AND stat = 0 

Open in new window

0
Comment
Question by:lrbrister
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41757935
Define 'running total' in the context of the above T-SQL statement, preferably with some sample data.
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41757941
CurrentCount is the column that has the running total?
If so, then you can try this:
UPDATE Campaigns
SET vendorServer=1
WHERE CurrentCount < 125000

Open in new window

NOTE: Test it before in a development environment before running the code in Production.
0
 

Author Comment

by:lrbrister
ID: 41757959
Here ...
SP1
And this
SP2
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Accepted Solution

by:
Vitor Montalvão earned 350 total points
ID: 41758031
Try this solution:
UPDATE Campaigns
SET SendGrid=1
FROM Campaigns T
INNER JOIN (SELECT TOP 100 PERCENT c.campagainId, c.CurrentCount, c.SendGrid, SUM(c2.CurrentCount) RunningTotal
		FROM Campaigns c,
			Campaigns c2 
		WHERE c2.campagainId <= c.campagainId
		GROUP BY c.campagainId, c.CurrentCount, c.SendGrid
		HAVING SUM(c2.CurrentCount)<125000
		ORDER BY c.campagainId) T2
	ON T.campagainId = T2.campagainId

Open in new window

0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 150 total points
ID: 41758286
try this.
;with cte as (select *,sum(CurrentCount) over (order by CampaignId) RunningTotal from Campaigns)
update cte set SendGrid = case when RunningTotal< 125000 then 1 else 0 end

Open in new window


Here is the example
declare @Campaigns table(CampaignId int, CurrentCount int, SendGrid bit)
insert @Campaigns values (96988, 21618, 0),(96989, 28477, 0),(96990, 24, 0),(96991, 58000, 0),(96992, 11255, 0),(96993, 35035, 0)
;with cte as (select *,sum(CurrentCount) over (order by CampaignId) RunningTotal from @Campaigns)
update cte set SendGrid = case when RunningTotal< 125000 then 1 else 0 end

select * from @Campaigns

/*
CampaignId	CurrentCount	SendGrid
96988	21618	1
96989	28477	1
96990	24	1
96991	58000	1
96992	11255	1
96993	35035	0
*/

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 41759535
Very nice guys... both work
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question