Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server Select on LT running Totals

Posted on 2016-08-16
6
76 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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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