Solved

SQL Server Select on LT running Totals

Posted on 2016-08-16
6
52 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 45

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

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 40

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now