473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to select all records one criteria, but not another?

I am trying to create a query where when one record within a household meets my criteria, I want all records to come back in the household. But I also need to exclude certain ptypes.

For example, I want all households that have a 'NOW' ptype, but does not include a 'CD' ptype. Using the sample data below, only one of these household would qualify, and I want all those records in my answer.

HHNUM PTYPE NIBOAC
200606892 CD 0
200606892 MMA 11913
200606892 NOW 76541
200606918 CRD 0
200606918 HEQ 10893
200606918 ODP 0
200606918 NOW 42884

Thanks.
Jan 28 '11 #1

✓ answered by Rabbit

You're not supposed to use PTYPE, you need to use an id field. Notice how I used two different fields in my example
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM DECTEST 
  3. WHERE 
  4.   HHNUM IN 
  5.   ( 
  6.   SELECT DISTINCT HHNUM 
  7.   FROM DECTEST 
  8.   WHERE PTYPE = 'NOW' 
  9.   ) 
  10. AND 
  11.   HHNUM NOT IN 
  12.   ( 
  13.   SELECT DISTINCT HHNUM 
  14.   FROM DECTEST 
  15.   WHERE PTYPE = 'CD ' 
  16.   )

7 2735
ck9663
2,878 Expert 2GB
What do you have so far?

~~ CK
Jan 28 '11 #2
So far I have this:

SELECT
b.HHNUM,COUNT(*) as NUMACCTS,SUM(b.NIBOAC) as TNIBOAC,
SUM(b.TOTBAL) as TOTBAL,b.BANK,b.CENT,b.[DEC],b.TIER,b.PROFIT
FROM DECTEST2 b
INNER JOIN (
SELECT DISTINCT
x.HHNUM,x.PTYPE,x.STYPE,x.NIBOAC,x.HHNIBOAC,x.TOTB AL,x.BANK,
x.BRANCH,x.CENT,x.[DEC],x.TIER,x.PROFIT
FROM DECTEST2 x
WHERE x.PTYPE IN ('DDA', 'MMA')) v
ON b.HHNUM = v.HHNUM
GROUP BY b.HHNUM,b.BANK,b.CENT,b.[DEC],b.TIER,b.PROFIT

When I try to add in the part about not pulling households with other ptypes, I get stuck.
Jan 28 '11 #3
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Table1
  3. WHERE
  4.      UniqueID IN
  5.      (
  6.      SELECT DISTINCT UniqueID
  7.      FROM Table1
  8.      WHERE SomeField = 'Hello'
  9.      )
  10. AND 
  11.      UniqueID NOT IN
  12.      (
  13.      SELECT DISTINCT UniqueID
  14.      FROM Table1
  15.      WHERE SomeField = 'World'
  16.      )
Jan 28 '11 #4
When I try this query, it only gives me the record = to the 'Hello' part, not all the records with the same HHNUM.
Jan 28 '11 #5
Rabbit
12,516 Expert Mod 8TB
Can you post your modified SQL?
Jan 28 '11 #6
SELECT *
FROM DECTEST
WHERE
PTYPE IN
(
SELECT DISTINCT PTYPE
FROM DECTEST
WHERE PTYPE = 'NOW'
)
AND
PTYPE NOT IN
(
SELECT DISTINCT PTYPE
FROM DECTEST
WHERE PTYPE = 'CD '
)

This is what I get:

HHNUM PTYPE NIBOAC
200606892 NOW 76541
200606918 NOW 42884

What I want is this:

HHNUM PTYPE NIBOAC
200606918 CRD 0
200606918 HEQ 10893
200606918 ODP 0
200606918 NOW 42884
Jan 28 '11 #7
Rabbit
12,516 Expert Mod 8TB
You're not supposed to use PTYPE, you need to use an id field. Notice how I used two different fields in my example
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM DECTEST 
  3. WHERE 
  4.   HHNUM IN 
  5.   ( 
  6.   SELECT DISTINCT HHNUM 
  7.   FROM DECTEST 
  8.   WHERE PTYPE = 'NOW' 
  9.   ) 
  10. AND 
  11.   HHNUM NOT IN 
  12.   ( 
  13.   SELECT DISTINCT HHNUM 
  14.   FROM DECTEST 
  15.   WHERE PTYPE = 'CD ' 
  16.   )
Jan 28 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Gill | last post by:
I can't help but think I'm re-inventing the wheel if I have to code my own interface! Isn't there some script, php code, or something (modifiable / customizable) available that lets me select...
2
by: Danny | last post by:
How to allow users to select a set of records and then let them change a field for all these records at once? I would like to do this in code on a form. I will have a form with tabular view of...
1
by: arthur-e | last post by:
How can you select records based on more than one combo box - I have a combobox that selects records based on name (I'm sure this has been asked a thousand times - web site answer/link could be...
1
by: Dan Sikorsky | last post by:
How do you select records from one dataset and create a second dataset? I need to pull out the master records from a dataset and create a master datagrid in which to embed a details datagrid (for...
6
by: Dale | last post by:
I'm not sure I'm even thinking about this the right way, but here goes: I have a table of users. Each one of these users may be associated with none, one, or many records in another table I call...
1
by: francophone77 | last post by:
What is the best way to setup a query to select records in specific months only. For instance if I want to compare sales in the month of May regardless of year. TIA
3
by: wvmbark | last post by:
First time poster... I just found this forum and it appears there's plenty of people here that could make short work of problem that's been driving me absolutely bonkers for months. Every day we...
5
by: trixb | last post by:
Hello all, Here is what I need to do and need help with: I have a table that is feeding a chart in a report in Access. If this table has more than 50 records, the graph gets messy, and this is...
2
by: Hamayun Khan | last post by:
Hi all I am using the following query to select records for table Select JobTitle,JobDesc,Scraped,logoimage,JobPostID,SchoolID,web,MemType,InstitutionName,PayScale,LEA,Contract as...
2
by: rkferguson | last post by:
Hi Folks, I have a table that captures information about documents that leave the office. Some of those documents may get a signature and then return to the office. Need away to query the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.