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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:14:29+00:00 2026-06-13T04:14:29+00:00

I m trying to achieve flag setting for the condition in my table below

  • 0

I m trying to achieve flag setting for the condition in my table below

p_id      mon_year      e_id     flag  
----      ---------     -----    -----
 1          2011/11       20       0
 1          2011/11       21       1
 1          2012/01       22       1
 1          2012/02       23       0
 1          2012/02       24       0
 1          2012/02       25       1
 2          2011/11       28       0
 2          2011/11       29       1
 2          2012/01       30       1

grouping by p_id,e_id and mon_year, the flag is set for the last value in the month.

I m confused how can i achieve this

I tried to achieved this by using row_number and partition to seperate out the value. By still looking for to achieved

Output by using row_number query , i have got is as below:

Grouping by

p_id      mon_year      e_id     row
----      ---------     -----    -----
 1          2011/11       20       1
 1          2011/11       21       2
 1          2012/01       22       1
 1          2012/02       23       1
 1          2012/02       24       2
 1          2012/02       25       3
 2          2011/11       28       1
 2          2011/11       29       2
 2          2012/01       30       1

Max of this value would set the flag column. But i m really bugged how to achieve it. Any help would be useful.

Thanks !!

  • 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-13T04:14:30+00:00Added an answer on June 13, 2026 at 4:14 am

    I think this is what you’re going for. . . The output exactly matches your example:

    declare @t table (p_id int, [year] int, [month] int,  [day] int)
    insert @t select 1, 2011, 11, 20
        union select 1, 2011, 11, 21
        union select 1, 2012, 01, 22
        union select 1, 2012, 02, 23
        union select 1, 2012, 02, 24
        union select 1, 2012, 02, 25
        union select 2, 2011, 11, 28
        union select 2, 2011, 11, 29
        union select 2, 2012, 01, 30
    
    select p_id, [year], [month], [day] 
    , case when r=1 then 1 else 0 end flag
    from
    (
        select p_id, [year], [month], [day]
        , row_number() over (partition by p_id, [year], [month] order by [day] desc) r
        from @t
    ) x
    order by  p_id, [year], [month], [day]
    

    Output:

    p_id    year    month   day     flag
    1       2011    11      20      0
    1       2011    11      21      1
    1       2012    1       22      1
    1       2012    2       23      0
    1       2012    2       24      0
    1       2012    2       25      1
    2       2011    11      28      0
    2       2011    11      29      1
    2       2012    1       30      1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to achieve something but i dont really know how I have set
I am new to SQL and trying to achieve something. I have a table
Trying to achieve any moving effect while appending an element from one to another
What I'm trying to achieve is show count of lines starting with/ending with needle
What i'm trying to achieve is playing a guitar chord from my python application.
What I'm trying to achieve is: Let inflector slug Ignore a special character in
I am trying to achieve an effect of overlapping the main window boundary with
What I am trying to achieve is editing code in vim then running make
I am trying to achieve something like this. The Expandable List consists of the
I am trying to achieve something in MySQL but even with all the answers

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.