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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:53:28+00:00 2026-05-17T15:53:28+00:00

Hallo Friends, I need a query to throw an error message on comparison with

  • 0

Hallo Friends,

I need a query to throw an error message on comparison with table temp_invoice.

It should compare the field [Task resp#] , against the [PB-ID] field in PB_Responsbility table for customer = 3D

temp_invoice format

Concession Number   [Task resp#]

   TH-123                 12345
   TH-014                 98065
   TH-123                 41027
   TH-567                 12345
   TH-111                 99765
   TH-567                 41027             
   TH-228                 73095             
   TH-001                 09129             
   TH-779                 41027         
   TH-333                 01029     
   TH-722                 03489  

This should check against the database table for which the customer is 3D.

TABLE PB_Responsbility

[PB-ID]                      Customer
  09129                        3D
  98065                        AB
  12345                        DC
  41027                        ZH
  99765                        3D
  73095                        UZ

If the customer is not 3D OR if [Task resp#] is not found in table PB_Responsbility,
it should throw a message, by selecting distinct [Task resp#]
from the excel sheet.

Here the required output is, “Incorrect [Task resp#]:- 12345,98065,41027,73095,01029,03489.

I have written the query so,

DECLARE @pb_id_msg VARCHAR(MAX)
SET @pb_id_msg = ''

SELECT @pb_id_msg =  @pb_id_msg + ' ,'
+ CONVERT(VARCHAR(10),[Task resp#]) FROM temp_invoice WHERE [Task resp#]  NOT IN 
    (SELECT [PB-ID] FROM PB_Responsbility WHERE Customer = '3D')
   GROUP BY CONVERT(VARCHAR(10),[Task resp#]).

This is producing the result as required. But if i modify the query with DISTINCT

SELECT DISTINCT @pb_id_msg =  @pb_id_msg + ' ,'
+ CONVERT(VARCHAR(10),[Task resp#]) FROM temp_invoice WHERE [Task resp#]  NOT IN 
    (SELECT [PB-ID] FROM PB_Responsbility WHERE Customer = '3D')

This results in only 1 value. Please explain me, why the second select query with distinct is not working??

  • 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-05-17T15:53:29+00:00Added an answer on May 17, 2026 at 3:53 pm

    Looks like SQL Server interprets distinct to mean you’d only like to assign a value to the variable once.

    You can fix that by moving the distinct to a subquery:

    SELECT  @pb_id_msg =  @pb_id_msg + ' ,' + CONVERT(VARCHAR(10),[Task resp#]) 
    FROM    (
            SELECT  DISTINCT [Task resp#]
            FROM    temp_invoice 
            WHERE   [Task resp#]  NOT IN 
                    (
                    SELECT  [PB-ID] 
                    FROM    PB_Responsbility 
                    WHERE   Customer = '3D'
                    )
            ) as SubQueryAlias
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo Friends, I need a button with text on it in 2 lines. The
Hallo, i am using WSS 3.0 and i need to display certain entries of
Hallo, i need to write some javascript that gets the contents of a userfield
Hello friends i need a book/ tutorials for rails without using scoffold. All the
hello friends following is my query where displaylist is object if stringBuilder which have
Hello Friends i have the following query which defeat the very purpose. A day
Hallo i am currently playing around with castle projects ActiveRecord and the remoting facility.
I.e.: echo H#97llo | MagicPerlCommand Stdout: Hallo were MagicPerlCommand is something like perl -pnle
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello I am compiling a program with make but I get the error of

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.