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.
You're not supposed to use PTYPE, you need to use an id field. Notice how I used two different fields in my example - SELECT *
-
FROM DECTEST
-
WHERE
-
HHNUM IN
-
(
-
SELECT DISTINCT HHNUM
-
FROM DECTEST
-
WHERE PTYPE = 'NOW'
-
)
-
AND
-
HHNUM NOT IN
-
(
-
SELECT DISTINCT HHNUM
-
FROM DECTEST
-
WHERE PTYPE = 'CD '
-
)
7 2735
What do you have so far?
~~ CK
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.
- SELECT *
-
FROM Table1
-
WHERE
-
UniqueID IN
-
(
-
SELECT DISTINCT UniqueID
-
FROM Table1
-
WHERE SomeField = 'Hello'
-
)
-
AND
-
UniqueID NOT IN
-
(
-
SELECT DISTINCT UniqueID
-
FROM Table1
-
WHERE SomeField = 'World'
-
)
When I try this query, it only gives me the record = to the 'Hello' part, not all the records with the same HHNUM.
Can you post your modified SQL?
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
You're not supposed to use PTYPE, you need to use an id field. Notice how I used two different fields in my example - SELECT *
-
FROM DECTEST
-
WHERE
-
HHNUM IN
-
(
-
SELECT DISTINCT HHNUM
-
FROM DECTEST
-
WHERE PTYPE = 'NOW'
-
)
-
AND
-
HHNUM NOT IN
-
(
-
SELECT DISTINCT HHNUM
-
FROM DECTEST
-
WHERE PTYPE = 'CD '
-
)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| | |