Getting duplicates even with Select Distinct - sql

I'm trying to write a query in which I should get the company information; however, I should be getting only 2 record and I'm getting 6 records.
Below is my query.
SELECT distinct a.FOLIO
,a.MAIN_ADDRESS1
,a.MAIN_ADDRESS2
,b.COMPANY_NAME
,b.FIRST_NAME
,b.LAST_NAME
,a.OPEN
,a.CLOSE
,c.CC
,c.CNAME
FROM vw_CODE_CASE AS a
INNER JOIN vw_CODE_CASE_VIOLATOR_CONTACTS AS b ON b.CMCODECASEID=a.CMCODECASEID
INNER JOIN vw_CODE_CASE_WORK_FLOW AS c ON c.CMCODECASEID=a.CMCODECASEID
Is it possible that is because of the amount of inner joins?
Thank you for your help.

Without sample data it would be difficult to identify that due to which column the distinct data are not coming from your query.
But you can do one thing to identify the cause, You can try remove one by one column and check the data from your query. At the point when you get your expected data, the last removed column will be the cause behind your problem.
Hope this helps.

Related

SQL INNER JOIN duplicate columns

I am trying to return some columns from 2 tables which share an ID column using the following browser database query system, which reads from the tables shown on this webpage. I believe the way to do this is by using INNER JOIN (e.g. see this guide).
SELECT sami_dr2.DR2Sample.CATID,
sami_dr2.DR2Sample.Mstar,
sami_dr2.StellarKinematics.PA_STELKIN
FROM sami_dr2.DR2Sample INNER JOIN sami_dr2.StellarKinematics
ON sami_dr2.DR2Sample.CATID = sami_dr2.StellarKinematics.CATID;
However, when I run this query I get the error message:
sql: Duplicate columns are not supported. Try using an alias for those columns within the SELECT clause e.g., SELECT t1.CATAID, t2.CATAID becomes SELECT t1.CATAID as t1_CATAID, t2.CATAID as t2_CATAID
But as far as I'm aware the whole point of using the INNER JOIN is to remove duplications as I'm not returning sami_dr2.StellarKinematics.CATID in my output table, only sami_dr2.DR2Sample.CATID.
I've also found that using SELECT sami_dr2.DR2Sample.CATID as ID_1, sami_dr2.StellarKinematics.CATID as ID_2 in the selction doesn't fix the problem either.
Any help on this would be greatly appreciated!

Newbie to SQL I have run the the inner join query but result comes up with columns only

I have run this query in adventureworks but the result is run successfully but i only get the columns instead of the data with columns how so?
select
a.BusinessEntityID,b.bonus,b.SalesLastYear
from
[Sales].[SalesPersonQuotaHistory] a
inner join
[Sales].[SalesPerson] b
on
a.SalesQuota = b.SalesQuota
My best guess is that instead of joining the tables on SalesQuota, you should be joining them on something else - An ID field, typically.
I don't have Adventureworks here, but judging from the names of the tables and the columns that you've provided, I would assume that there's a SalesPersonID field of some sort that actually connects a Salesperson's quota history to the Salesperson him/herself.
I would expect that you're looking for something closer to this:
SELECT
a.BusinessEntityID
,b.bonus
,b.SalesLastYear
FROM [Sales].[SalesPersonQuotaHistory] a
INNER JOIN [Sales].[SalesPerson] b
ON a.SalesPersonID = b.SalesPersonID
General Knowledge:
INNER JOIN means "Show me only entries (rows) that have a matching value on both sides of the condition." (i.e. The value in Table A matches the value in Table B).
So ON a.SalesQuota = b.SalesQuota means "Only where the value of SalesQuota in Table A matches the value of SalesQuota in Table B."
I'm not sure what the purpose of this query could be, since it is entirely possible that two salespeople have the same values in both tables, and then you would get duplicate rows (because the values of SalesQuota would match in both cases), or that the values wouldn't match at all, and then you wouldn't get any rows - I suspect that is what's happening to you.
Consider the conditions of what you're trying to join. Are you really trying to join quota amounts, or are you trying to retrieve quota information for specific salespeople? The answer should help guide your JOIN conditions.

Duplicate results on inner join

I've written the below query but I'm getting multiple duplicate rows in the results, please can anyone see where I'm going wrong?
use Customers
select customer_details.Customer_ID,
customer_details.customer_name,
metering_point_details.MPAN_ID,
Agents.DA_DC_Charge
from Customer_Details
left join Metering_Point_Details
on customer_details.customer_id = Metering_Point_Details.Customer_ID
left join agents
on customer_details.Customer_ID = agents.customer_id
order by customer_id
It doesn't really matter, but you're not using an INNER JOIN. Regardless, your unexpected rows indicate that your JOIN criteria is not specific enough to return your expected output. You can use SELECT DISTINCT if your results are fully duplicative, and if you'd like to see why you're getting those duplicates you can just use SELECT * to see the full detail between the multiple rows that are returned using your JOIN criteria, which should help you either make your criteria more specific or show you that you've got duplicated records in one of the tables you're using in your JOIN.
With sample data we can dissect the problem more, but odds are you won't need it once you see why the rows are duplicated.

Getting way more results than expected in SQL left join query

My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)

Access 2010 - Why does a query produce less results than the base table itself

I have created a query in Access 2010 that maps a number in the right table into the left table. when i run the query i get less results than the total number of records in the left table and i wonder why? it seems that some records are left out. Does somebody have a clue on what i am doing wrong?
edit: i cant post pictures so i´ll draw this here in a simplified way:
-table 1....-........-table 2...-..........-table 3... -
-number 1-.<->.-number 1-.........-xxxxxxxx...-
-xxxxxxxx...-.......-number 2-..<->..-number 2-
Query:
SELECT [2007].[Analyse (Nummer)],
[2007].[Analyse (Name)],
[2007].Faktura, BKPF2007.Referenz,
BSET.St
FROM BSET
INNER JOIN (2007 INNER JOIN BKPF2007 ON [2007].[Faktura] = BKPF2007.[Belegnr])
ON BSET.[Belegnr] = BKPF2007.[Referenz]
GROUP BY [2007].[Analyse (Nummer)],
[2007].[Analyse (Name)],
[2007].Faktura,
BKPF2007.Art,
BKPF2007.Referenz,
BSET.St;
That's one of the functions of JOIN - it does not show records that don't match. There are a few things I can suggest without seeing specifics.
Remove one of the joins, so your query only shows Table 1 and Table 2. Make sure you get the expected results before moving on to joining Table 3
Replace your joins with LEFT JOINS which show all records even if there is no match in the right-hand table. Even if this doesn't give you the solution you're hoping for, it may help you diagnose the problem.
This is the reason for your results being filtered:
INNER JOIN
You will need to make this a LEFT JOIN to keep all results where results do not exist on the right tables(s).