Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

t-sql query help

Posted on 2013-12-04
6
293 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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