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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:27:03+00:00 2026-06-13T14:27:03+00:00

SELECT NameMonth,Year,Value from Table WHERE Name LIKE ‘%JERRY%’ AND Year = (SELECT MAX(Year) FROM

  • 0
SELECT "Name""Month","Year","Value" 
from Table
WHERE 
    "Name" LIKE '%JERRY%'
AND "Year" = 
    (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%')
AND "Month"= 
    (SELECT MAX("Month") FROM Table 
     where 
        "Name" LIKE '%JERRY%' 
    AND "Year"= (SELECT MAX("Year") FROM Table where "Name" LIKE '%JERRY%'))

Table –>

Name  | Year | Month | Value
-----------------------------
JERRY   2012    9        100           
JERRY   2012    9        120         
JERRY   2012    9        130           
JERRY   2012    8         20        
JERRY   2011    12        50           

So i want the first three rows as output. As for the latest month for the latest year i need all the values.
Can someone suggest a better cleaner query?

  • 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-13T14:27:04+00:00Added an answer on June 13, 2026 at 2:27 pm

    If there are other people in the table then you probably want to use a ranking function: Something like:

    
    DECLARE @sample TABLE (
          [Name] VARCHAR(255)
        , [Year] INT
        , [Month] INT
        , [Value] INT
        )
    
    INSERT @sample ([Name], [Year], [Month], [Value])
    VALUES ('JERRY', 2012, 9, 100)           
         , ('JERRY', 2012, 9, 120)
         , ('JERRY', 2012, 9, 130)
         , ('JERRY', 2012, 8, 20)
         , ('JERRY', 2011, 12, 50)
         , ('FRED', 2011, 12, 50)
         , ('FRED', 2011, 12, 120)
         , ('FRED', 2011, 7, 150)
    
    SELECT *
    FROM (
        SELECT *
             , RANK() OVER (PARTITION BY [Name] ORDER BY [Year] DESC, [Month] DESC) AS [rnk]
        FROM @sample
        )
        AS samp
    WHERE
        samp.[rnk] = 1
    

    Which gives results:

    Name                      Year        Month       Value       rnk
    ------------------------- ----------- ----------- ----------- ------
    FRED                      2011        12          50          1
    FRED                      2011        12          120         1
    JERRY                     2012        9           100         1
    JERRY                     2012        9           120         1
    JERRY                     2012        9           130         1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 2 select tags as, <td><g:select name=Year from=${2004..2014} optionValue=${Year} /></td> <td><g:select name=Month from=${['January','February','March','April','May','Jun','July','August','September','October','November','December']}
Consider this: <select name=month_selection> <optgroup label=2012> <option value=1>January</option> </optgroup> <optgroup label=2011> <option value=1>January</option> <option
I've got a dropdown that's code is as follows: <select name=month onchange=updateURL()> <option value=1>January</option>
<select name=day id=day> <option value=0 label=Day:>Day:</option> <option value=1 label=1>1</option> </select> <select name=month id=month> <option
SELECT * FROM items WHERE caption LIKE 'name%' AND type = 'private' OR owner
select devicename, name, value, timestamp from table1 gives me the following, DEVICENAME NAME VALUE
SELECT Id,Date,Name FROM people WHERE DATEPART(hh,Date) >= 7 AND DATEPART(hh,Date) <= 8 Order by
select tf.Id,tf.Name,tf.LName,tf.Rank,tf.Category where tf.Id not in (select fighter_id from tbl_player_fighter where league_id=91) and tf.Status
SELECT * FROM list WHERE list_item LIKE %_1375_% returns results it should not. For
SELECT l.name FROM locations as l WHERE l.location_id =',2,32,' the ,2,32, values we get

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.