Solved

t-sql query help

Posted on 2013-12-04
6
289 Views
Last Modified: 2013-12-06
Hi,

I know this is a small issue but I am trying to figure this out for a while now. Please help me resolve the below issue.

CREATE TABLE TABLE1
(
ACCT_ID INT,
ACCT_TYPE VARCHAR(20)
)

INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'A')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'B')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'C')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'D')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'E')

INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (2,'A')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (2,'B')

SELECT * FROM TABLE1

SELECT DISTINCT ACCT_ID FROM TABLE1 WHERE ACCT_TYPE IN ('A','B') AND ACCT_TYPE NOT IN ('C','D','E')

Open in new window


From the above TABLE1, I want only the ACCT_ID's which have 'A' , 'B'. I want to ignore the other IDs.

From the above table I should get the result as '2'. But I am getting both '1' and '2'

Please help me modifying my query,

Thanks in advance!!!
0
Comment
Question by:ravichand-sql
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39697413
query is correct

SELECT DISTINCT ACCT_ID FROM TABLE1 WHERE ACCT_TYPE IN ('A','B') AND ACCT_TY

you want want only the ACCT_ID's which have 'A' , 'B'. right.

then see below
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'A')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (1,'B')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (2,'A')
INSERT INTO TABLE1 (ACCT_ID,ACCT_TYPE) VALUES (2,'B')

as per above you are inserting 1 and 2 from A and B
so result is correct.
0
 

Author Comment

by:ravichand-sql
ID: 39697419
Thanks for the reply :)

Yes, But I dont want to get 1 in my result set as it has values other than 'A' and 'B'. My result set should only have 2
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39697431
ok means you do not want the acc_id that has other valuse other than A and B is it so ?

then try this
Select ACCT_ID FROM TABLE1
where ACCT_ID not in (

SELECT ACCT_ID  FROM TABLE1 WHERE ACCT_TYPE IN ('C','D' , 'E') )
and  ACCT_TYPE = 'A' and  ACCT_TYPE = 'B'
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:ravichand-sql
ID: 39697926
Hello Pratima,

The above query of yours is giving me no results. Please help me modify it.

Thanks in advance!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39698261
>I want only the ACCT_ID's which have 'A' , 'B'
I take it this really means 'I want only the ACCT_ID's where the ACCT_TYPE value is 'A' or 'B'?
SELECT DISTINCT ACCT_ID
FROM TABLE1
WHERE ACCT_TYPE IN ('A', 'B')
ORDER BY ACCT_ID

Open in new window

>From the above table I should get the result as '2'. But I am getting both '1' and '2'
Looking at your sample data there are ACCT_TYPE=A or B for both 1 and 2, so eyeball your sample data and explain to us the logic for returning only 2.
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39698740
Solution 1, using a correlated sub-query:
SELECT DISTINCT
	a.ACCT_ID
FROM	TABLE1	a
WHERE	a.ACCT_TYPE IN ('A','B')
AND	NOT EXISTS
	(	SELECT	*
		FROM	TABLE1
		WHERE	ACCT_ID = a.ACCT_ID
		AND	ACCT_TYPE IN ('C','D','E')
	)

Open in new window

Solution 2, preferred solution:
SELECT DISTINCT
	a.ACCT_ID
FROM	TABLE1	a
LEFT
JOIN	TABLE1	b	ON	a.ACCT_ID = b.ACCT_ID
			AND	b.ACCT_TYPE IN ('C','D','E')
WHERE	a.ACCT_TYPE IN ('A','B')
AND	b.ACCT_ID IS NULL

Open in new window

Solution 3, in-clause:
SELECT DISTINCT
	ACCT_ID
FROM	TABLE1
WHERE	ACCT_TYPE IN ('A','B')
AND	ACCT_ID NOT IN
	(	SELECT	ACCT_ID
		FROM	TABLE1
		WHERE	ACCT_TYPE IN ('C','D','E')
	)

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now