Select Query producing duplicate results of the same records (Access 2010) - sql

I had a select query, which I designed in the SQL view of the query design tool. With some of my results I found duplicates of the same records. As in there were not multiples in the table, only the query (Same Primary Key). Here is the original query.
SELECT t1.*
FROM Inventory AS t1 INNER JOIN Inventory AS t2 ON
t1.Part_ID = t2.Part_ID WHERE (t1.Inventory_ID<>t2.Inventory_ID);
I aimed to query Inventory for records with the same Part_ID (FK) but different Inventory_ID(PK). There is a composite key between part_ID (FK) and location_ID (FK), if that makes any difference.
I have since changed this query to:
SELECT DISTINCT t1.*
FROM Inventory AS t1 INNER JOIN Inventory AS t2 ON t1.Part_ID = t2.Part_ID
WHERE (t1.Inventory_ID<>t2.Inventory_ID);
This removes the duplicate records, however, I don't believe that my original query should produce replicate data results. I am worried that this suggests that there is something wrong with my tables?
My table looks like the following:
Thanks

The thing is that you might have multiple occurences of part_ID on the INNER JOIN side of your select. So if a part with the same part_ID and a different inventory_ID exists in 2 other locations, you will get duplicates.
To check that, you could do a test on a few duplicates, or rewrite your original query with a GROUP BY instruction on the INNER JOIN side of the query.

Related

How to return no duplicates for records that have many-to-many values

I am trying to find the correct join construction to join together the relevant customer info from the Rentals table with the Accidents table. I often run into this issue where my joining fields aren't unique but not sure what else to join on. The accidents table only has about 1500 records but when I join it to pull in more customer data, I get like 35k records. I can do some joins but frequently have joins like this at work and I feel like a dummy because I am not sure how to troubleshoot...
SELECT a.*,
r.market,
r.date_of_birth,
r.is_blocked,
r.rate_type
FROM `accidents` a
LEFT JOIN `rentals` r -- I also tried an INNER JOIN
USING (customer_id) -- Other fields I tried to match on: Full Name
ORDER BY accident_dt DESC

How to join 4 tables in SQL?

I just started using SQL and I need some help. I have 4 tables in a database. All four are connected with each other. I need to find the amount of unique transactions but can't seem to find it.
Transactions
transaction_id pk
name
Partyinvolved
transaction.id pk
partyinvolved.id
type (buyer, seller)
PartyCompany
partyinvolved.id
Partycompany.id
Companies
PartyCompany.id pk
sector
pk = primary key
The transaction is unique if the conditions are met.
I only need a certain sector out of Companies, this is condition1. Condition2 is a condition inside table Partyinvolved but we first need to execute condition1. I know the conditions but do not know where to put them.
SELECT *
FROM group
INNER JOIN groupB ON groupB.group_id = group.id
INNER JOIN companies ON companies.id = groupB.company_id
WHERE condition1 AND condition2 ;
I want to output the amount of unique transactions with the name.
It is a bit unclear what you are asking as your table definitions look like your hinting at column meanings more than names such as partycompany.id you are probably meaning the column that stores the relationship to PartyCompany column Id......
Anyway, If I follow that logic and I look at your questions about wanting to know where to limit the recordsets during the join. You could do it in Where clause because you are using an Inner Join and it wont mess you your results, but the same would not be true if you were to use an outer join. Plus for optimization it is typically best to add the limiter to the ON condition of the join.
I am also a bit lost as to what exactly you want e.g. a count of transactions or the actual transactions associated with a particular sector for instance. Anyway, either should be able to be derived from a basic query structure like:
SELECT
t.*
FROM
Companies co
INNER JOIN PartyCompancy pco
ON co.PartyCompanyId = pco.PartyCompanyId
INNER JOIN PartyInvolved pinv
ON pco.PartyInvolvedId = pinv.PartyInvolvedId
AND pinv.[type] = 'buyer'
INNER JOIN Transactions t
ON ping.TransactionId = t.TransactionId
WHERE
co.sector = 'some sector'

Joining three tables clarification

This isn't a question I need a direct answer to, I Just need to know how, from a philosophical point of view, how many tables I would need to join.
Say I have 3 tables :
TableA :
Account_Number | Name | Address
TableB :
Account_Number | Occupation | Salary
TableC :
Account_Number | Model | Make
These tables all have a column in common. If I was to join these tables together to get a the columns in each table that has a matching account number, i would do an inner join to return only records that are matching.
in the query, I would have something like :
SELECT T1.Name, T1.Address, T2.Occupation, T2.Salary, T3.Model, T3.Make
FROM TableA T1
INNER JOIN TableB T2 ON T1.Account_Number=T2.Account_Number
INNER JOIN TableC T3 ON T2.Account_Number=T3.Account_Number /Do I need to join T3 to T1 or is this handled by having T1 and T2 joined together?
WHERE ??.AccountNumber=1234123412341234 /Would any Table work in the WHERE clause here since we are all looking for the same account_number in each table?
SELECT T1.Name, T1.Address, T2.Occupation, T2.Salary, T3.Model, T3.Make
FROM TableA T1
INNER JOIN TableB T2 ON T1.Account_Number=T2.Account_Number
INNER JOIN TableC T3 ON T2.Account_Number=T3.Account_Number /Do I need to join T3 to T1 or is this handled by having T1 and T2 joined together?
WHERE T1.AccountNumber=1234123412341234
Inner join will return only records which are present in the table you are joining ,. So in your select your use TableA so in where clause you should use T1.AccountNumber
For more information please look this link http://www.sql-join.com/
There are a few things to consider with respect to your data model. For example
o What is the relationship between these tables? Is there one? Is the fact that there is an Account_Number column just a coincidence
o Assuming there is a relationship, what is the cardinality? For example, for any given row in Table A, how many corresponding rows are there in Table B; 0, 1 or more than one. Similarly from Table B to table A; and similarly between Tables B and C.
o. Depending on the meaning of these relationships, you could find yourself in a situation called a "fan trap". This is a situation where you have a fanning out of relationships:
B
/ \
/ \
A C
It easier to describe with an Example.
Lets say:
A is a table of Sales Reps
B is a table of Branches
C is a table of Accounts
So each Sales Rep works in one Branch (cardinality=1), and each Branch has 1 or more Sales Rep ( cardinality=Many )
Each Branch handles many Accounts, and each Account is held at one Branch
This is a perfectly valid data model
However, from this data model, if you want to find out which sales rep handles which account, you cannot. This is the so called Fan trap. In this particular example, you would probably solve this be having a relationship directly between Sales Rep and Accounts; even though it may look redundant.
So I know this is a long answer to your question, but it elaborates on the short answer, which is "It depends" :)
BobC is right concerning semantics, it depends.
Syntactically, your example is fully correct and just how I would do it too. Joining applies to just 2 objects/result sets and both have to be in the condition when you don't want a cross join. Furthermore it doesn't matter which of the identically account# columns from the first join you use in the second join.
As for the WHERE condition, if only one of the tables got an Index or Partionkey, use that one. Else, use the table on which the WHERE statement gives you the fewest rows.
Joining often benefits from starting with the smallest tables/result sets. Although Oracle will optimize your statement respecting the gathered statistics anyway.

SQL query with loop

I am having trouble with writing a SQL query in Access with foreign keys. There are two tables, 'Customers'(ID, Name, Surname) and 'Orders'(ID, Customer, Date, Volume). ID fields are primary, and Orders.Customer is a foreign key linked to Customers.ID, so a customer can have many orders or none.
My goal is to do a search on customers based on many criteria, one of which being if customers have at least an order which volume is superior to a certain quantity. I tried joins with SELECT DISTINCT but it still gives me duplicate results, plus I had to create an empty order for every customer without orders if the query didn't use the above condition.
Does anyone have an idea about that? Maybe some special instruction on foreign keys or 2 separate queries?
Based on the information you give, i only can give you hints on what I think you're doing/understanding wrong :
SELECT DISTINCT does select you a unique record, not a unique value, so if your statement selects all fields (*), distinct won't help you much there.
My guess is you had to create an empty order for each customer because you used INNER JOIN, try LEFT OUTER JOIN instead
For example :
SELECT DISTINCT Customers.*
FROM Customers
LEFT OUTER JOIN Orders
ON (Orders.Customer = Customers.id)
WHERE Volume > put_your_value

SQL like clause is not returning any results

I have following query, but it doesn't return any results for where clauses, even when there is row with that kind of name what is queried. If I remove where clause, then all records in Company table which have OfficeLocation table are returned. What is wrong in my query?
SELECT c.*
FROM [MyDb].[dbo].[Company] AS c
INNER JOIN [MyDb].[dbo].[CompanyOfficeLocation] AS col ON c.Id = col.CompanyId
INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON ol.Id = col.OfficeLocationId
WHERE ol.Name like '%Actual Name In This Table%';
Table structure :
Company
Id
etc ...
CompanyOfficeLocation
CompanyId
OfficeLocationId
OfficeLocation
Id
etc ...
Two things for a record to show up given your query:
The OfficeLocation you specified (given the ol.Name value) must have an Id value that is used by a record in the CompanyOfficeLocation table in its OfficeLocationId.
The CompanyOfficeLocation record that you got in #1 must have a CompanyId that exists in the Company table.
If any of those two criteria are not met, then no records will show up in your query result. The INNER JOIN is essentially an 'AND' clause. If a record could not be related to at least one INNER JOINed table, then that record will not show up at all.
If you want a record to show up despite not having any related records in the joined tables, you may want to consider using OUTER JOINs. A RIGHT JOIN in your case to be exact.
I do not find any mistake however I'd suggest you switch the columns after ON when joining to maintain standards.
Instead of - INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON ol.Id = col.OfficeLocationId
Do - INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON col.OfficeLocationId = ol.Id