简体   繁体   中英

Access SQL Conditional Clause Multiple Conditions/Records Same Column

I've run into a problem trying to write some SQL for MS Access that will parse some of the data down in the way I was hoping. While I'm admittedly new (or still learning beyond the bounds of a college class I took five years ago) with regards to SQL, I'd like to think what I'm trying to do can be accomplished.

The scenario: While I might not have started this all of that efficient to start with, I began with two tables. The original dataset started with a little under 72k records.

The goal, or what I was seeking to accomplish in the case was to find all of the records where someone appeared for more than one Charge, and the outcome for a charge that is defined as having 0 points is guilty, however the other charges are found either NG or Dismissed.

Here is what I have so far. That data I started with came from two tables, one which contained the appearances, one that had the codes and points.

An example for the appearances would be similar to the following:

APP    DATE      CHARGE    OUTCOME  
===================================
1      1/1/2014   137      GT        
2      1/5/2014   122      GT        
2      1/5/2014   123      DI       
3      1/6/2014   257      DI
3      1/6/2014   257      DI
4      1/6/2014   137      NG
4      1/6/2014   123      DI
4      1/6/2014   122      GT

I had a second table which linked the charge, to the potential number of points associated with the charge, fairly simple formatting there

CHARGE    POINT
===============
122       0
123       2
137       2
257       0

So I created a few basic queries, since I'm largely filling in the blanks based on a bulk data file that was provided to me. The first one pulled joined the first two tables together, so I was left with a table that looked something along the lines of:

APP    DATE      CHARGE    OUTCOME  POINTS
==========================================
1      1/1/2014   137      GT       2      
2      1/5/2014   122      GT       0
2      1/5/2014   123      DI       2
3      1/6/2014   257      DI       0
3      1/6/2014   257      DI       0
4      1/6/2014   137      NG       2
4      1/6/2014   123      DI       2
4      1/6/2014   122      GT       0

I then created three small queries that were each asked to produce a subset of data from the main query, one for OUTCOME=GT, Points=0, another for Outcome=NG, Points>2, the last Outcome=DI, Points>2. Each one of the queries was called in a make table query that brought the record set down from the original 72k to 39k. Applying yet another query with

Select * from record_set Where RecordID IN
(Select RecordID from record_Set
GROUP BY RecordID
HAVING COUNT (*) > 1))

on the end brought the total down to just under 21k.

My problem is this: The dataset I would like to return needs to further filter the results. Currently I haven't figured out how to weed out the APP values where both values are 0. For all I know I should have approached this a completely different way.

EDIT: What I would be looking for is the dataset that returns to look something like this:

APP    DATE      CHARGE    OUTCOME  POINTS
==========================================      
2      1/5/2014   122      GT       0
2      1/5/2014   123      DI       2
4      1/6/2014   137      NG       2
4      1/6/2014   123      DI       2
4      1/6/2014   122      GT       0

Where the single record was pulled, and the APP where the OUTCOME was DI/0 for all the Charges involved.

The current attempts I have tried using a straight forward WHERE clause only evaluate the records single value. I guess I'm looking for a way to evaluate both. Maybe that would be easier if I wrote the value to separate temp tables and then did a union only for the APP values that appeared in both tables?

Any help or guidance would be greatly appreciated!

My MS-Access experience is rather close to zero, but I believe this syntax will work. At least it might spark some ideas.

SELECT APP, DATE, CHARGE, OUTCOME, POINTS
FROM APP A
INNER JOIN CHARGE C on C.Charge = A.Charge
WHERE (Outcome = 'GT' and Points = '0')
OR (Outcome = 'NG' and Points > '2')
OR (Outcome = 'DI' and Points > '2')

I'm not sure what you mean by cases when both values are 0, but more than likely you can add another WHERE clause to filter those out as well.

EDIT:

I guess the data set I am seeking will always have the following: More than one APP, while containing one record with 0 PTS that was a GT, and one (or more records) that can be a point value greater than 0 that is either DI or NG. The reason I started at 2 is that I know there aren't any values that contain a 1, but I guess to be thorough they could be included

First get APPs that meet the 0 PT and GT requirements:

SELECT APP
FROM APP A
INNER JOIN CHARGE C on C.Charge = A.Charge
WHERE Outcome = 'GT'
AND Points = '0'

Reuse above to get APPs that have point values greater than 0 and DI or NG:

SELECT APP
FROM APP A
INNER JOIN CHARGE C on C.Charge = A.Charge
WHERE (Outcome = 'DI' AND Points > '0')
OR (Outcome = 'NG' AND Points > '0')

Next, combine the above logic into one query, using two subqueries, in order to only pull APP data that meets both requirements:

SELECT APP, DATE, A.CHARGE, OUTCOME, POINTS
FROM APP A
INNER JOIN CHARGE C on C.Charge = A.Charge
WHERE APP IN (
   SELECT APP
   FROM APP A
   INNER JOIN CHARGE C on C.Charge = A.Charge
   WHERE Outcome = 'GT'
   AND Points = '0'
)
AND APP IN (
   SELECT APP
   FROM APP A
   INNER JOIN CHARGE C on C.Charge = A.Charge
   WHERE (Outcome = 'NG' AND Points > '0')
   OR (Outcome = 'DI' AND Points > '0')
)

This will result in only APPs that have a GT, 0 PT charge, AND another charge that is DI or NG with 1 or greater PTs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM