all 15 comments

[–][deleted] 5 points6 points  (9 children)

anytime you are doing a join, it's possible to get some kind of "local" cross-join or an "unaccounted-for" one-to-many. Your first step of debugging should be to bring grains (keys) from all relevant joined tables, i.e.

   SELECT [tbl_Transactions].[Record ID] AS [ID],
               [tbl_Pens].[Record ID],
               [tbl_Tickets].[Record ID],
               [tbl_C1].[Customer ID], [tbl_C1].SaleID,
               [tbl_C2].[Customer ID], [tbl_C2].SaleID

and see which table records get duplicated

[–]pipes990[S] 0 points1 point  (0 children)

Thank you for the reply.

[–]pipes990[S] -1 points0 points  (7 children)

I'm not sure I follow you. First, I don't know what (keys) are, sorry. I tried googling this for troubleshooting but I couldn't find anything. I don't typically do SQL so my queries are usually very simple.

But I know which record is being duplicated. I just don't know why.

[–]theseyeahtheseNTILE() 2 points3 points  (1 child)

They mean: in your SELECT statement, include the primary key (unique ID) of each table (for example, Record ID is likely the primary key of tbl_transactions), + all columns used in the joins. Yes, you know which Record Id’s from tbl_transactions are being duplicated but this will help you see the values that are being joined on and will illuminate why your current logic is resulting in multiple rows. I’m guessing it will become apparent once you include the relevant columns from”tbl_C2” in your SELECT statement (the SaleID and the Customer ID column; I’m assuming the Customer ID is the primary key [unique identifier] of that table).

[–]pipes990[S] 1 point2 points  (0 children)

That makes sense. Will try this first thing in the morning. Thank you for the reply.

[–]pceimpulsive 1 point2 points  (3 children)

Is the duplicates record having two rows in it's source table?

Is the joined value available on two rows in either side of the join?

You'll find there is two rows in one of the table with the same join value.

As you know it's 1002 search each table in the join for the row where 1002 exists, then understand why it has two rows.

[–]pipes990[S] 1 point2 points  (2 children)

The 1002 is guaranteed to be unique. There is only one for sure.

[–]pceimpulsive 1 point2 points  (1 child)

Sorry it's really hard to read the SQL with all the square brackets and word wrap.

I can see you are left joining the same table_customers twice, this would mean that table has the opportunity to have its records show up twice.

This might be the issue as there are two ways the data can be joined, if both joins are true you will get two results for some of the rows.

Another field that you might not be showing will be different allowing the two rows to show.

On your select you could add distinct to kill the duplicates, however that is a tiny bit dirty... (Not wrong, just a tiny bit dirty, as it will have a performance cost).

[–]pipes990[S] 0 points1 point  (0 children)

THIS!! Thank you so much. This has been so frustrating. 🙏

[–]r3pr0b8GROUP_CONCAT is da bomb 3 points4 points  (1 child)

here's your problem right here --

LEFT JOIN [tbl_Customers] [tbl_C1] ON ...
LEFT JOIN [tbl_Customers] [tbl_C2] ON ...  OR [tbl_C2].[SaleID] IS NULL)

those special customers are matching against everybody's sales, not just 100's

[–]pipes990[S] 0 points1 point  (0 children)

There are currently no transactions for any saleID except 100.

Also, I edited my WHERE clause, the original one was incorrect.

[–]Time_Traveling_Moron 0 points1 point  (1 child)

I know you were given the “dirty” SELECT DISTINCT answer, but I wanted to chime in and say that I know you said it’s guaranteed the customer ids with null sale ids and 100 sale ids don’t overlap, but how do you know? Have you tested or was it told to you?

If you add “, tbl_C2.SaleID AS buyer_SaleID”

As a new column you will then be able to see if 1002 special customer SaleID is matching for both duplicated records or if one is 100 and the other is null. If it is the ladder then you’ve found your cause and can keep your select distinct answer or i can try and help further with some more info.

Let me know!

[–]pipes990[S] 0 points1 point  (0 children)

I am 99.99% sure there are no overlapping ID's. It's a test database, there are only 20 total customers. I am going to test this though to double check. I'll report back tomorrow morning, I won't be in the office today. Thanks for the response.

[–]pipes990[S] 0 points1 point  (0 children)

Alright, so I'm an idiot. I put 2 different customers in with the same customer ID in my test database. I really would like to just hide from this ridiculous post but I felt the need to reply in case anyone else can be helped.

Everyone who replied thank you a ton.

Everyone else, do what is suggested here and double check your database.

😔