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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:48:06+00:00 2026-06-09T16:48:06+00:00

I have simple SQL Server query: declare @table table (id int, name nvarchar(5), deleted

  • 0

I have simple SQL Server query:

declare @table table (id int, name nvarchar(5), deleted bit)

insert into @table(id, name, deleted) values(1, 'A1', 0)
insert into @table(id, name, deleted) values(2, 'A1', 0)
insert into @table(id, name, deleted) values(3, 'A1', 0)
insert into @table(id, name, deleted) values(4, 'A1', 1)
insert into @table(id, name, deleted) values(5, 'A2', 0)
insert into @table(id, name, deleted) values(6, 'A2', 0)

select
    max(id) as id,
    name
from @table
where deleted = 0
group by name

it returns to rows

id  |name
--------------
3   |A1
6   |A2

but should return only one

id  |name
-------------- 
6   |A2

since last (or max) id of A1 is deleted.

How to fix my query.

Thanks a lot.

  • 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-09T16:48:08+00:00Added an answer on June 9, 2026 at 4:48 pm

    Since you could have a third set like this:

    insert into @table(id, name, deleted) values(7, 'A3', 1)
    insert into @table(id, name, deleted) values(8, 'A3', 0)
    

    I assume you want the following returned as well, since the highest id for that name was not marked as deleted:

    id    name
    ----  ----
    8     A3
    

    Then this query should do it:

    ;WITH x AS 
    (
      SELECT id, name, deleted, 
        rn = ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC)
      FROM @table
    )
    SELECT id = MAX(id), name 
    FROM x
    WHERE NOT EXISTS
    (
      SELECT 1 FROM x AS x2 
      WHERE name = x.name 
      AND deleted = 1 AND rn = 1
    )
    GROUP BY name;
    

    If you only want rows returned where no row for a particular name was ever deleted, then it’s slightly simpler:

    SELECT id = MAX(id), name
    FROM @table AS t
    WHERE NOT EXISTS 
    (
      SELECT 1 FROM @table 
      WHERE name = t.name AND deleted = 1
    )
    GROUP BY name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem with SQL SERVER charindex function. DECLARE @VAR1 varchar SET
I have a simple temp-table defined in SQL Server 2008 R2 representing a parent-child
I use SQL Server 2005. I have a simple logging table that my web
I have (what I think) is a simple SQL Server spatial query: Grab all
I've got a simple query running against SQL Server 2005 SELECT * FROM Table
I have a simple SQL query (SQL Server 2005) where I'm selecting from a
I use SQL Server 2005 and I have a query like this : INSERT
Let's say I have the following table in SQL Server 2008: ProfileID int //identity;
I am using SQL Server Compact Edition. I have a simple table: CREATE TABLE
With our product we have a simple backup tool for the sql server database.

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.