SQL Query

Suppose I have a table with records below. How to write a TSQL such that if the records under a same contract# (eg. 0001) are invoiced, those records will not be displayed out

Contract#         MO              Qty          Invoiced
0001                   0001a         2                 Yes
0001                   0001b         1                 Yes
0002                    0002a         2                Yes
0002                   0002b         5                 No
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
what do you want to output then? only 0002 records?
Éric MoreauSenior .Net ConsultantCommented:
something like this maybe:

SELECT *
INTO #temp
FROM (
	SELECT '0001' AS Contract, '0001a' AS MO, 2 AS Qty, 'Yes' AS Invoiced
	UNION ALL 
	SELECT '0001', '0001b', 1, 'Yes'
	UNION ALL 
	SELECT '0002', '0002a', 2, 'Yes'
	UNION ALL 
	SELECT '0002', '0002b', 5, 'NO'
) AS A

SELECT * FROM #temp

SELECT * FROM #temp
WHERE Contract IN (SELECT Contract FROM #temp WHERE Invoiced = 'NO')

DROP TABLE #temp

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterSenior Software DeveloperCommented:
A basic subquery should achieve the result you're looking for and you shouldn't need to create a temp table to do it.

SELECT
  *
FROM
  [table]
WHERE
  [Contract#] IN
  (
    SELECT DISTINCT
      [Contract#]
    FROM
      [table]
    WHERE
      [Invoiced] = 'No'
  )

Open in new window

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Éric MoreauSenior .Net ConsultantCommented:
Russ, the only reason I have a temp table is to be able to test the query.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Please post the desired output for your example.
Russ SuterSenior Software DeveloperCommented:
Eric,

You're right. I see that now. You posted the same solution I did. Well done.
AXISHKAuthor Commented:
Tks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.