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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:16:16+00:00 2026-06-19T00:16:16+00:00

I have a table like below (using SQL server 2008 R2 Enterprise): create table

  • 0

I have a table like below (using SQL server 2008 R2 Enterprise):

create table Message
(
ID int Not Null Primary Key,
Text nvarchar(100) not null,
Code nvarchar(50) null
)

Insert Into Message Values (1,'Hello Everybody','T6/45')
Insert Into Message Values (2,'Hello Everybody',Null)
Insert Into Message Values (3,'Hello Everybody','T6/45')
Insert Into Message Values (4,'Hello Everybody','T6/45')
Insert Into Message Values (5,'Hello Everybody','T6/70')
Insert Into Message Values (6,'Hello Everybody','T6/70')
Insert Into Message Values (7,'Hello','T6/70')
Insert Into Message Values (8,'Hello','T6/45')
Insert Into Message Values (9,'Hello Everybody',Null)
Insert Into Message Values (10,'Hello Everybody','T6/70')
Insert Into Message Values (11,'Hello',Null)

I need to get all the records which are having same Text but different Code. And Code will not be Null .

So expected output will be:

Insert Into Message Values (1,'Hello Everybody','T6/45')
Insert Into Message Values (5,'Hello Everybody','T6/70')
Insert Into Message Values (7,'Hello','T6/70')
Insert Into Message Values (8,'Hello','T6/45')

I tried below query, but it is returning several rows:

select m1.* 
from Message M1 
With (nolock)
JOIN Message M2 
With (Nolock) 
On m1.Text=m2.Text
where (m1.ID<>m2.ID 
  and m1.Code<>m2.Code 
  and m1.Code is not null)

How can I get expected result?

Also, this is a sample DB. I need to run the query on a table having arount 50 Million records. So any optimized query will be of great help.

  • 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-19T00:16:18+00:00Added an answer on June 19, 2026 at 12:16 am
    SELECT ID,TEXT,CODE
    FROM message
    WHERE ID IN 
    (
      SELECT MIN(ID) FROM message
     WHERE CODE IS NOT NULL GROUP BY TEXT,CODE
    )
    

    or

    SELECT 
       MIN(ID)
       ,TEXT
       ,CODE 
    FROM message
    WHERE CODE IS NOT NULL GROUP BY TEXT,CODE
    ORDER BY MIN(ID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like below (using SQL server 2008 R2): CREATE TABLE [dbo].[Data](
I am using SQL Server 2008. I have a table like below: ID Name
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
e.g. If I have a table like below:- create table Categories(CategoryId int primary key,
I'm using SQL Server 2008. I have a database table that looks like this
I am using sql server 2000 I have a table like below : lr_no
I am using sql server 2008. I have to insert records in temporary table
Let us say that I have a table structured like this(using SQL server): empID
I have a history table like below CREATE TABLE [dbo].[t_PettyCash_History] ( [TableName] [varchar] (500)
I have a table trigger like below: CREATE OR REPLACE TRIGGER PAT_BUR_DOB_TRG BEFORE UPDATE

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.