Multi Table Query Access Comparing Data - sql

If I have three tables, one called Person, one called Owner and the other called Tenant. All three have SSN as one of the fields. What I want to do is compare the SSN from Person (that's the whole list) to see which ones do not show up in either OWner or Tenant so I can see which people in the database have never owned a unit or leased a unit. Then i would like to be able to delete these people out of the person table.
Thanks

One easy way to do this is using not in:
select p.*
from persons as p
where p.ssn not in (select ssn from owner) and
p.ssn not in (select ssn from tenant);

Related

SQL Query to return a table of specific matching values based on a criteria

I have 3 tables in PostgreSQL database:
person (id, first_name, last_name, age)
interest (id, title, person_id REFERENCES person)
location (id, city, state text NOT NULL, country, person_id REFERENCES person)
city can be null, but state and country cannot.
A person can have many interests but only one location. My challenge is to return a table of people who share the same interest and location.
All ID's are serialized and thus created automatically.
Let's say I have 4 people living in "TX", they each have two interests a piece, BUT only person 1 and 3 share a similar interest, lets say "Guns" (cause its Texas after all). I need to select all people from person table where the person's interest title (because the id is auto generated, two Guns interest would result in two different ID keys) equals that of another persons interest title AND the city or state is also equal.
I was looking at the answer to this question here Select Rows with matching columns from SQL Server and I feel like the logic is sort of similar to my question, the difference is he has two tables, to join together where I have three.
return a table of people who share the same interest and location.
I'll interpret this as "all rows from table person where another rows exists that shares at least one matching row in interest and a matching row in location. No particular order."
A simple solution with a window function in a subquery:
SELECT p.*
FROM (
SELECT person_id AS id, i.title, l.city, l.state, l.country
, count(*) OVER (PARTITION BY i.title, l.city, l.state, l.country) AS ct
FROM interest i
JOIN location l USING (person_id)
) x
JOIN person p USING (id)
WHERE x.ct > 1;
This treats NULL values as "equal". (You did not specify clearly.)
Depending on undisclosed cardinalities, there may be faster query styles. (Like reducing to duplicative interests and / or locations first.)
Asides 1:
It's almost always better to have a column birthday (or year_of_birth) than age, which starts to bit-rot immediately.
Asides 2:
A person can have [...] only one location.
You might at least add a UNIQUE constraint on location.person_id to enforce that. (If you cannot make it the PK or just append location columns to the person table.)

How to create a view that can identify each person's club out of many club?

I'm making the database of club's members and I found this problem.
Suppose:
There are people who want to join clubs.
Let's imagine that there're 10 club.
Each person choose to join from 1 to 5 clubs.
Each club's administrator have a data of all the person who join his club only.
(Example: if A join football club then football club's administrator will have his name but he won't know whether A join another club or not.)
I have a data of all the people who choose to join clubs.(retrieve from each club's administrator.)
If A joins 3 clubs that means I have 3 table that have A's name.
I have create a table that store the name of each club.
Questions
If there's not only A but it's 100 person like this, then how can I create one table that have a person's name and all the clubs they join?
Can I create a view table that have a format like this:
personID, personName, club1, club2, club3, club4, club5 (club# can be null.)
OR
personID, personName, allOfClubs
OR
Do I have to manually check on each person?
You don't store data like that in a database - that is Excel spreadsheet thinking.
You should have at least three tables, possibly four if a club can have more than one administrator.
Your tables should be People, Clubs, Memberships.
Memberships would be a junction table that you store one PersonId, one ClubID, and probably some other information Join date etc.
This way you have a person who is a member of as many clubs as you like.
First you need UNION all the club table:
SELECT 'football' as club, person FROM club1 UNION ALL
SELECT 'rugby' as club, person FROM club2 UNION ALL
....
SELECT 'tennis' as club, person FROM clubN
Then use PivotTable

Retrieve count of related records

I currently have two tables in my data source that I'm referencing in this instance.
Firstly, to explain the context, I have a windows form program in VB.NET (Visual Studio 2013).
In the first table Trainer, I have the following fields : ID, First Name, Surname, Contact, Class.
In the second table Member, I have the following fields : ID, First Name, Surname, Contact, Type, TrainerID.
I have enforced referential integrity between the two tables using Trainer.ID as PK and Member.TrainerID as FK with a 1:m relationship. I'm trying to retrieve the count of related records, to the specified ID of the trainer. I want to retrieve the count of related records. So, for example on the form I click Search and provide a trainer ID, I'd like to return the amount of customers he/she has belonging to them.
I need that, so that I can work out their salary based on commission + base amount.
I've looked around a lot, read up a lot but I just can't seem to get it. Any help whatsoever will be appreciated.
If you have the trainer id, can't you just do:
select count(*) as cnt
from member m
where m.trainerid = #TheTrainerId;
select count(m.id) as count_members
from trainer t
left join member m on m.trainerid = t.id
where t.surname = 'watson'

What is the normalized database form for these 3 tables?

So I have three tables and subsequently three models as well:
Company (table: companies)
Person (table: people)
Address (table: addresses)
How to connect these together, considering that each Person or Company can have multiple addressess, but since the addresses are in the same format I'd like to use one single 'addresses' table, so dealing/managin addresses can be done trough a single model?
Because this structure:
companies(id, name, ...)
people(id,name, ... )
addresses(id, foreign_id, foreign_model, ...)
Seems a bit out of place for me. basically this is the current sructure now. The 'foreign_model' field is a string which specifies which model the address belongs to (i.e. Company or Person).
All this under Cakephp, but I have many problems with this structure, what should I use instead?
how abt this:
companies(id, name, address_id,..)
people (id,name, address_id,... )
addresses(id,... )
Both companies and people point to the address table for their address.
Here you dont have to store any duplicate addresses if any
EDIT: If both companies and people can have many addresses
Then keep a mapping table
companies(id, name,..)
people (id,name, ... )
addresses(id,... )
Address_map(id,address_id,type)
Where type says whether its a company or a people
id --> either companyid or poepleid

sql query with "with and in clause"

i have a table which store user name, hobby and city .hobby field contain different hobby joined using "," operator eg swimming, basket, cricket. I want to search user name who match at least one hobby according to my search criteria.
You should not have multiple attributes in one column. That's one of the number one rules of 3nf database design. Now you have to figure out ways to parse this data. This issue only gets worse and worse each and every day. Seperate the hobbies as multiple rows in your database.
I agree with #JonH that there shouldn't be more than one piece of information in a column. It stops the row being truly atomic.
But you are where you are, and you can use the LIKE clause to return rows that match a substring within a column.
Something like:
select hobbycolumn from hobbytable where hobbycolumn like '%swimming%'
for example
To do this properly you need to restructure your tables if possible. For what you are looking for a possible way would be to have 3 tables. I'm not sure who the city belongs to, so I put it with the user.
1 for user with the following cols:
id
name
city
A table for for hobbies:
id
name
And a user_hobbies join table that allows each user to have multiple hobbies, and each hobby to have multiple users:
id
user_id (foreign key)
hobby_id (foreign key)
Then searching for a user with a certain hobby is:
SELECT user.id, user.name FROM user
INNER JOIN 'user_hobbies' on user_hobbies.user_id=user.id
INNER JOIN 'hobbies' on hobbies.id = user_hobbies.hobby_id
WHERE hobbies.name LIKE "query";