Solved

t-sql query help

Posted on 2013-12-04
6
290 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help - 12 59
email the result out from a T-SQL queries 29 62
T SQL Update Table from another table 5 42
tempdb latch contention 12 47
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

18 Experts available now in Live!

Get 1:1 Help Now