?
Solved

Query for non-duplicates in same table.

Posted on 2011-09-24
3
Medium Priority
?
251 Views
Last Modified: 2012-05-12
We have one table which stores witness forms for numerous legislative bills.  The same witnesses may fill out forms for multiple bills over a period of time.

We need a query that will find all the witnesses  who have filled out forms for a past bill and NOT yet filled out a form for a specified new bill.

Would it be best to create a temporary table from the first criteria and then use NOT EXISTS?  Would it be best to simply use GROUP BY?
Can JOIN be used which only one table?

We can assume that Last_Name, First_Name are accurate unique identifiers for witnesses.

Fields would include:

First_Name
Last_Name
DATE
Bill_Number

Data for Bill_Number would look like "SB 1", "SB 2", "HB5" etc.

Here's a non-functioning schematic of what I want to do:

SELECT First_Name, Last_Name, Bill_Number FROM `libertytools`.`testimony`
WHERE `Bill_Number` = "SB 2"

NOT EXISTS

SELECT First_Name, Last_Name, Bill_Number FROM `libertytools`.`testimony`
WHERE `Bill_Number` = "SB 8";

Thanks!
0
Comment
Question by:Ron1959
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 36593389
IF the names really were unique, a not exists with distinct would work

SELECT  DISTINCT p.First_Name, p.Last_Name
FROM     `libertytools`.`testimony` p
WHERE  p.Bill_Number = "SB 2"
AND      NOT EXISTS (
            SELECT 1
            FROM  `libertytools`.`testimony` n
            WHERE n.First_Name = p.First_Name
            AND      n.Last_Name = p.Last_Name
            AND     n.Bill_Number = "SB 8"
 )
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36593402
>> We have one table which stores witness forms for numerous legislative bills.  
>> The same witnesses may fill out forms for multiple bills over a period of time.

If you have control over the structure, it'd be better to store the unique witnesses and legislative bills in separate tables. Then store the relationships (ie witnesses for each bills) in a 3rd table

Witness:   WitnessID (Unique PK), FirstName, LastName, ....
LegislativeBill:   LegistlativeBillID (Unique PK), Bill_Number, ...
LegislativeBillWitnesses:  WitnessID , LegistlativeBillID, .....
0
 

Author Closing Comment

by:Ron1959
ID: 36593691
Works perfectly! Just what I was looking for.

If the witness enters their name differently from time to time, we will double up on them, but for this purpose it doesn't have to be perfect.

We would use two or three tables to store this data instead of one if the users were from a fairly static group, but witnesses will not be consistent.

Thank you!

- Ron
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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