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 7650257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:12:17+00:00 2026-05-31T11:12:17+00:00

I’m having trouble getting a query to work. What I’m trying to do is

  • 0

I’m having trouble getting a query to work. What I’m trying to do is sum a counter by user id but with conditions.

Currently my query gives the following output.

User ID  EndDate    Date       Index
 123      5/1/12   1/1/12       -1
 123      5/1/12   1/25/12       1
 123      5/1/12   2/13/12      -1
 456      4/1/12   1/18/12      -1
 456      4/1/12   2/15/12      -1
 456      4/1/12   2/18/12       1

What I want to do with this list is sum the Index by User Id but with a catch. The Index must be summed in date order, also the min value of the index is -1 and max is 1, so the values can be -1, 0, 1 only. So with user 123, the process would be -1 then you add 1 then you add -1 for a final sum of -1. But for user 456 you start with -1 then you have -1 again but the sum must remain -1 then you have 1, so the final sum is 0. Below is what I’m been trying to do but I can’t figure it out. I would really appreciate some help.

DECLARE @Period char(6)
DECLARE @StatusCount int
    SET @Period = '201201'
    SET @StatusCount = 0

SELECT     Q1.UserID, Q1.End_Date,
            Sum(Case 
                When Index = -1 Then Case When @IndexCount >=0 Then @IndexCount - 1 Else @IndexCount + 0 End
                When Index = 1 Then Case When @IndexCount <=0 Then @IndexCount + 1 Else @IndexCount + 0 End
            END) as FinalIndex

FROM 
(
    (SELECT    UserID,  End_Date, Enter_Dt, 1 as Index
    FROM        UserTable 
    WHERE       (Code in ('A', 'B') and PRD = @Period)
    GROUP BY    UserID,  End_Date, Enter_Dt)

         UNION

    (SELECT     UserID,  End_Date, Enter_Dt, -1 as Index
    FROM         UserTable 
    WHERE     (Code in ('C', 'D') and PRD = @Period)
    GROUP BY    UserID,  End_Date, Enter_Dt)
) as Q1

GROUP BY Q1.UserID, Q1.End_Date
ORDER BY Q1.UserID ASC, Q1.End_Date ASC

I think my main problem is I can’t figure out how to accumulate the Index properly. I can’t get IndexCount to remember the the previous value and then start again from 0 with the next User ID

The result I get with this query is

User ID    EndDate       Index
 123       5/01/12       -1
 456       4/01/12       -1

Which is just summing the Index

  • 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-31T11:12:18+00:00Added an answer on May 31, 2026 at 11:12 am

    I’ll illustrate a running total by userID solution here, leaving out the other details of your query for clarity. Basically, add a IndexRT column populate it with a running total that resets for each new userID.

    EDIT: constrain running total to -1,0,1

    create table #temp(userID int, [Index] int, IndexRT int) 
    insert into #temp (userID,[Index]) values (123,-1) , (123,1) , (123,-1) , (456,-1) , (456,-1) , (456,1)  
    declare @rt int; set @rt=0;
    
    with a as (
        select select TOP 100 percent *
        ,r=ROW_NUMBER()over(partition by userID order by userID) 
        from #temp order by userID,ROW_NUMBER()over(partition by userID order by userID) 
    ) 
    update a set @rt = [IndexRT] = case when case when r=1 then [Index] else @rt + [Index] end between -1 and 1
    then case when r=1 then [Index] else @rt + [Index] end
    else @rt
    end
    
    select * from #temp;
    go 
    drop table #temp;
    

    Result:

    userID      Index       IndexRT
    ----------- ----------- -----------
    123         -1          -1
    123         1           0
    123         -1          -1
    456         -1          -1
    456         -1          -1
    456         1           0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need to clean up various Word 'smart' characters in user input, including but
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.