简体   繁体   English

访问SQL条件子句多个条件/记录在同一列

[英]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. 我在尝试编写一些用于MS Access的SQL时遇到问题,该SQL将按照我希望的方式解析一些数据。 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. 尽管我承认关于SQL的知识是新的(或者仍然学习超过五年前的大学课程),但我想我可以做的事情是可以实现的。

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. 原始数据集以不到72,000条记录开始。

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. 目的或本案中我想要达到的目的是,找到某人出庭不止一项指控的所有记录,而一项指控的定义为有0分的结果是有罪的,而其他指控是有罪的发现异常或已解散。

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. 然后,我创建了三个小查询,每个小查询都被要求从主查询中生成数据的子集,一个用于OUTCOME = GT,点数= 0,另一个用于结果= NG,点数> 2,最后一个结果= DI,点数> 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. 在make table查询中调用了每个查询,该查询将记录集从原始的72k减少到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. 最后,总数下降到不足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. 目前,我还没有弄清楚如何清除两个值均为0的APP值。就我所知,我应该采用完全不同的方法来处理。

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. 提取单个记录的位置,以及所有涉及的费用的结果为DI / 0的APP。

The current attempts I have tried using a straight forward WHERE clause only evaluate the records single value. 我尝试使用直接WHERE子句进行的当前尝试仅评估记录的单个值。 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? 如果我将值写入单独的临时表,然后仅对两个表中都出现的APP值进行并集,那可能会更容易?

Any help or guidance would be greatly appreciated! 任何帮助或指导将不胜感激!

My MS-Access experience is rather close to zero, but I believe this syntax will work. 我的MS-Access经验几乎为零,但是我相信这种语法会起作用。 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. 我不确定这两个值均为0时的情况是什么意思,但您更有可能添加另一个WHERE子句以将其过滤掉。

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. 我想我要查找的数据集将始终具有以下内容:一个以上的APP,同时包含一个包含0个PTS的GT记录和一个(或多个记录)可以大于0的点值的记录DI或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 我从2开始的原因是,我知道没有任何包含1的值,但我想很可能将它们包括在内

First get APPs that meet the 0 PT and GT requirements: 首先获得满足0 PT和GT要求的APP:

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: 重复使用上面的方法以获得点值大于0且DI或NG的APP:

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: 接下来,使用两个子查询将上述逻辑组合到一个查询中,以便仅提取同时满足两个要求的APP数据:

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. 这将导致仅具有GT,0 PT费用和其他费用的DI或NG具有1个或更大PT的APP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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