Solved

Query for non-duplicates in same table.

Posted on 2011-09-24
3
247 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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