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

  • SEARCH
  • Home
  • 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 8844651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:32:27+00:00 2026-06-14T11:32:27+00:00

Let’s say I have a table table1 with the following structure: id date v1

  • 0

Let’s say I have a table table1 with the following structure:

  id date v1   v2  v3  v4 ... vn
  ------------------------------
  1   03   Y    N  89  77 ... x
  1   04   N    N  9   7  ... i
  1   05   N    Y  6   90 ... j
  1   06   N    Y  9   34 ... i
  1   07   N    Y  0   88 ... i
  2   03   N    N  9   77 ... f
  2   04   Y    Y  90  7  ... y
  2   05   Y    N  6   90 ... v
  2   06   N    Y  9   34 ... i
  2   07   N    N  10  88 ... i

As you might see, the table has five rows for each id. I’d like to create two new columns:

-summarystory:= This variable is computed for those rows having the date between 05 and 07 and is the sum of the variable v3 for the last three rows.

Let me explain this better: the first two rows (date 03 and 04) must have NULL values, but the row having date=05 is the sum of the last three v3 values, i.e, 89+9+6=104. Likewise, the row having date=06 must be equal to 9+6+9=24. This have to be done for each id and for each date.

This is the desired result:

  id  date v3  summarystory
  -------------------------
  1    03  89     NULL
  1    04  9      NULL
  1    05  6      104
  1    06  9      24
  1    07  0      15
  2    03  9      NULL
  2    04  90     NULL
  2    05  6      105
  2    06  9      105
  2    07  10     25
  • VcountYN:= the number of Y for each row (based only on variables v1 and v2). So. for instance, for the first row it would be VcountYN=1. This variable must be computed for all the rows.

Any help is much appreciated.

  • 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-06-14T11:32:28+00:00Added an answer on June 14, 2026 at 11:32 am

    Here’s how to do the computations. Turning it into the new table is left as an exercise:

    -- SQL 2012 version
    Select
      t.id,
      t.[date],
      Case When [Date] Between 5 And 7 Then 
        Sum(v3) over (
          partition by 
            id 
           order by 
             [date]
           rows between 
             2 preceding and current row
        ) Else Null End,
      Case When v1 = 'Y' Then 1 Else 0 End +
        Case When v2 = 'Y' Then 1 Else 0 End
    From
      table1 t;
    
    -- SQL 2005 version
    Select
      t1.id,
      t1.[date],
      Case When t1.[date] Between 5 And 7 Then t1.v3 + IsNull(t2.v3, 0) + IsNull(t3.v3, 0) Else Null End,
      Case When t1.v1 = 'Y' Then 1 Else 0 End +
        Case When t1.v2 = 'Y' Then 1 Else 0 End
    From
      table1 t1
        Left Outer Join
      table1 t2
        On t1.id = t2.id and t1.[date] = t2.[date] + 1
        Left Outer Join
      table1 t3
        On t2.id = t3.id and t2.[date] = t3.[date] + 1
    

    http://sqlfiddle.com/#!6/a1c45/2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a table with a Color column. Color can have various
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say that I have a date in R and it's formatted as follows.
let's say I have the following string: string s = A B C D
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have a due date and a reminder timespan . How do
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012

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.