troubleshooting Question

Subquery doesn't work right - does weird things! Help!

Avatar of stankstank
stankstank asked on
Microsoft SQL Server 2005Microsoft SQL Server
4 Comments3 Solutions228 ViewsLast Modified:
Dear Experts,

I am trying to make a query which returns all contacts who have referred at least 3 contacts within the last XXX days.  In this example, I have entered the value of 360 (1 year).  I have not even tried to figure out how to calculate the total number of referrals given becuase it gets really weird then.

It does not pull the values correctly...The query I am posting LOOKS like it works...but it doesn't.  It's just not accurate and I don't know what I have done wrong.  Please view line 17 where the code is 'WHERE Referred_By = Contact_Auto_ID)' - notice that the ')' character closes the select statement.  If I put this anywhere else, I get a really jacked up set of data...  Just an example of one problem I am having.

Here is how my table looks (I have taken out some of the columns which don't matter like names & phone number columns... you can see those in the select statement on the link)

[Contacts].Contacts
--------------------
Contact_Auto_ID (pk, int)
Date_Added (smalldatetime)
Referred_By (int - fk to Contact_Auto_ID in this table)

The column 'Referred_By' must be related to Contact_Auto_ID so that's why it's a FK to a column in the same table...  There is a default value in Contact_Auto_ID of 0.  That's how we have Referred_By set to 0 in the columns which weren't referred by another contact.

Another weird thing..in the current query, the column 'PhoneHome' has a value of null and it shows the record.  If I enter a phone number, that record goes away.  I have NO IDEA WHY!!!

I have been working with this for several hours now and still no luck...  Please help!

Here is the link to my query:
http://75.33.104.230/code/query.aspx
ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 3 Answers and 4 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 3 Answers and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004