Joined CTEs with a where clause returns too many rows [closed]
I am getting a very strange issue using SQL. I am running this in SQL Server Management Studio. I have built two CTEs trying to find dates where membership starts and membership ends, with the end goal of being able to show the date ranges where members are active. The code that builds the CTEs is fairly extensive and I am leaving it out to start, but if it turns out to be needed I can add it. starting_points and ending_points are the names of the CTEs.
I run this query
SELECT *
FROM starting_points starts
JOIN ending_points ends
ON ends.MEMBER = starts.MEMBER
AND ends.rowNum = starts.rowNum
WHERE ends.MEMBER = 1
This is the result
| MEMBER | begin_date | rowNum | MEMBER | end_date | rowNum |
|---|---|---|---|---|---|
| 1 | 20201116 | 1 | 1 | 20201118 | 1 |
| 1 | 20201120 | 2 | 1 | 20210316 | 2 |
| 1 | 20210402 | 3 | 1 | 99991231 | 3 |
When I run this code
SELECT *
FROM starting_points starts
JOIN ending_points ends
ON ends.MEMBER = starts.MEMBER
AND ends.rowNum = starts.rowNum
ORDER BY ends.MEMBER
I get this result
| MEMBER | begin_date | rowNum | MEMBER | end_date | rowNum |
|---|---|---|---|---|---|
| 1 | 20201116 | 1 | 1 | 20201118 | 1 |
| 1 | 20201120 | 2 | 1 | 99991231 | 2 |
| 2 | 20201113 | 1 | 2 | 99991231 | 1 |
| 3 | 20211209 | 1 | 3 | 99991231 | 1 |
I am very confused as to why this is happening. As far as I know, the where clause should only filter down to return rows where the condition is met, but in this case it is adding a new row and changing the value in one of the columns in row 2. For clarity, the correct return is the one that uses the where clause and returns three rows for member #1.
As far as I can tell this is only happening on one member. I have spot checked about 20 other members and have not found this happening on any of them.
Thank you in advance for the help.
Table of Contents
You may also like
Popular Posts
Choosing Healthy Bully XL Puppies: Signs of a Reputable Breeder and Strong Bloodlines
Understanding Dog and Cat Euthanasia: Compassion, Process, and Modern Considerations
Fresh, Raw, or Kibble? Comparing the Most Popular Dog Food Types
¿Estás Cometiendo Estos 5 Errores con tu Mascota sin Saberlo?
From Wax to Worry: How to Tell If Your Cat’s Ear Discharge Signals Infection
Fresh Breath and Healthy Teeth: The Complete Guide to Dog Oral Hygiene
The Role of Personal Protection Dogs in Modern Society
How Dogs Enrich Our Lives and We Enrich Theirs in Return
Are Goldendoodles Good Beginner Dogs?
Exploring the Three Fundamental Types of Anti-Barking Collars
Dog collars vs. Harnesses: Which Is Right For Your Pup?
How to Use Treats When Training a Dog
The History Of Dog Collars
The Best Puppy Food For Goldendoodles
People Prefer Using The Best Harnesses For Their Beloved Pets
The Best GPS Collars to Buy For Your Dog: 3 Key Points to Consider When Choosing One
How Can You Socialise a Dog with Other Dogs?
How To Train Your German Shepherd Puppy
Games And Exercises That Will Make Dog Training Fun For You And Your Furry Friend
Choosing The Best Small Dog For Your Family
Latest Posts
7- Days Keto Diet And Work Plan. How Can Pets Be Accompanied For Effective Weight Loss?
How to Become a Veterinarian
Best Ways To Handle Separation Anxiety In Dogs By Pet Sitters
Cbd Dosage For Pets
If Your Pet Is Not Comfortable, It Will Not Be Happy
What Are The Common Feline Diseases Found And How To Treat It?
CBD Gummies – How Did It All Start?
Three Things To Take Care While Feeding Poultry
How To Achieve A Tranquil Expulsion Of Pets In 2018?
Eye Disease in Lovebirds – Causes and Treatment
Four Signs It’s A great opportunity to Change Pet Nourishment
Feline Versus Canine – Which Is Ideal for You?
