Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:59:36+00:00 2026-05-14T22:59:36+00:00

Can anyone help me write this query more efficiently? I have a table that

  • 0

Can anyone help me write this query more efficiently?

I have a table that captures TCP traffic, and I’d like to update a column called RowNumForFlow which is simly the sequential number of the IP packet in that flow. The code below works fine, but it is slow.

declare @FlowID int
declare @LastRowNumInFlow int
declare @counter1 int
set @counter1 = 0

while (@counter1 < 1)
BEGIN
set @counter1 = @counter1 + 1   

-- 1)
select  top 1
        @FlowID = t.FlowID
from    Traffic t 
where   t.RowNumInFlow is null

if (@FlowID is null)
    break

-- 2)
set @LastRowNumInFlow = null

select  top 1
        @LastRowNumInFlow = RowNumInFlow
from    Traffic 
where   FlowID=@FlowID and RowNumInFlow is not null
order by ID desc

if @LastRowNumInFlow is null
    set @LastRowNumInFlow = 1
else
    set @LastRowNumInFlow = @LastRowNumInFlow + 1

update Traffic set RowNumInFlow = @LastRowNumInFlow
where ID = (select top 1 ID from Traffic where 
            flowid = @FlowID and RowNumInFlow is null)


END

Example table values after query has run:

    
ID  FlowID  RowNumInFlow
448923  44  1
448924  44  2
448988  44  3
448989  44  4
448990  44  5
448991  44  6
448992  44  7
448993  44  8
448995  44  9
448996  44  10
449065  44  11
449063  45  1
449170  45  2
449171  45  3
449172  45  4
449187  45  5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:59:36+00:00Added an answer on May 14, 2026 at 10:59 pm

    Something like this:

    update
        T
    set
        RowNumInFlow = @TheNumber
    FROM
       (
        SELECT
           ID, 
           ROW_NUMBER() OVER (PARTITION BY FlowID ORDER BY ID) AS TheNumber
        FROM
           Traffic
       ) T
    

    After comments:

    update
        T1
    set
        RowNumInFlow = TR.TheNumber
    FROM
       Traffic T1
       JOIN 
       (
        SELECT
           ID, 
           ROW_NUMBER() OVER (PARTITION BY FlowID ORDER BY ID) AS TheNumber
        FROM
           Traffic
       ) TR ON T1.ID = TR.ID
       WHERE
           T1.RowNumInFlow IS NULL --like this?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Untested: % Take the mean of adjacent pairs x_mean =… May 16, 2026 at 4:31 pm
  • Editorial Team
    Editorial Team added an answer ALTERNATIVES: Easy copy/paste of latest version (but install instructions may… May 16, 2026 at 4:31 pm
  • Editorial Team
    Editorial Team added an answer a php module, pal to run php code you need… May 16, 2026 at 4:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

can anyone help me write a linq query, its a bit confusing... Basically i
if i have a schema that goes somthing like this peoples(personNumber, etc) jobs(personNumber, jobNumber)
I have this small table of data. Dir LinkL LinkH East 19 27 East
I am trying to write a SQL Server query but have had no luck
Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query
I am trying to write a query expression to parse an XML tree, but
This is the SQL query I wrote in MS Access 07: IF EXISTS (Select
I am trying to use PHP preg_math parse a string that looks like: 6.15608128
UPDATE - No need to answer this now, I have solved below. Hi, I'm
EDIT: For the inner queries, there could be more than one match per inner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.