Solved

Query for non-duplicates in same table.

Posted on 2011-09-24
3
244 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
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 125 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now