![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
OK, I have done what you said and reduced the tables used in the search. After much messing around with the stored procedure, I have figured out that by commenting out the following code (at the end of the procedure), I can get results: WHERE tab1.gender = @gender AND tab1.country = @country AND tab1.bday_year BETWEEN @low AND @high AND tab2.photo_default = 1 + @photo--WHERE (and, the ORDERBY code is commented out as it depends on this code). I have also found that if I leave any one of the above lines the code again fails. So, for some reason, @gender, @country, @low, @high, and @photo are not getting passed appropriately. |
#12
| |||
| |||
|
|
On Sun, 15 Jul 2007 18:33:19 -0700, pbd22 wrote: (snip) OK, I have done what you said and reduced the tables used in the search. After much messing around with the stored procedure, I have figured out that by commenting out the following code (at the end of the procedure), I can get results: WHERE tab1.gender = @gender AND tab1.country = @country AND tab1.bday_year BETWEEN @low AND @high AND tab2.photo_default = 1 + @photo--WHERE (and, the ORDERBY code is commented out as it depends on this code). I have also found that if I leave any one of the above lines the code again fails. So, for some reason, @gender, @country, @low, @high, and @photo are not getting passed appropriately. Hi Peter, I found the stored procedure code in an earlier message in this thread. I don't know how much you changed, so the following might or might not apply. Your query uses a lot of left (outer) joins. Are you sure that these can't be inner joins? The tab1 and tab2 tables are among the tables that are outer joined. By adding a criterium in the WHERE clause, you effectively convert them to inner joins - so you should either modify the query to use inner join (improved readability and maintainability and probably better performance as well), or move the filters to the ON part of the appropriate JOIN clauses. Note that I did not do a complete review of your code; it's too long for that. Try to trim down the problem to a more simplified case that's short enough for us to invest our time in, yet similar enough to your real problem that you can translate our soultions back to your original situation. If you're not able to simplify the problem, you should probably hire a SQL developer to aide you with this issue. -- Hugo Kornelis, SQL Server MVP My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis |
#13
| |||
| |||
|
|
The below procedure seems to work except for one major error and a minor one: major: when I simply leave the edit_date column as is, I get the error: "ambiguous column name edit_date" and, when I include the alias with the edit_date column, I get the following: "The multi-part identifier "tab3.edit_date" could not be bound." |
|
The lesser problem is that when I added SELECT DISTINCT at the bottom of the procedure to avoid duplicates it seems to have thrown off the paging. I have designated 10 rows as a default page parameter. This worked well before I changed the bottom select statement to eliminate duplicates. |
#14
| |||
| |||
|
|
pbd22 (dush... (AT) gmail (DOT) com) writes: The below procedure seems to work except for one major error and a minor one: major: when I simply leave the edit_date column as is, I get the error: "ambiguous column name edit_date" and, when I include the alias with the edit_date column, I get the following: "The multi-part identifier "tab3.edit_date" could not be bound." The problem is that when inlucde the alias you do it all over town. Don't do that. With in the CTE you should do it: SELECT ROW_NUMBER() OVER (ORDER BY CASE @sortID WHEN 1 THEN registerDate --WHEN 2 THEN tab3.edit_date WHEN 3 THEN login_date --WHEN 4 THEN up_order END DESC) AS RowNum, tab1.registerDate, tab3.edit_date ,tab4.login_date, And a more general comment, as soon as more than one table is included in the query, prefix all your columns with aliases (or the table name). That makes the query easier to follow for an outsider, and also saves you from accidents if you would add, say, an up_order column to some other table later on. However, in the query where you use the CTE: (SELECT DISTINCT registerDate --,tab3.edit_date ,login_date ,bday_day ,bday_month ,bday_year ,gender --,up_order ,zipCode ,siteId ,userID FROM SavedSearch You cannot use tab3, because it is not visible at this point. It's private to the CTE. And since this is a one-table query, there is no need to use aliases, although it would not be wrong to do so. But then it would be like: (SELECT DISTINCT ss.registerDate ,ss.edit_date ,ss.login_date ... FROM SavedSearch ss I noticed another issue: (select distinct emailAddress from Users union select distinct user_name from PersonalPhotos union select distinct email_address from EditProfile union select distinct email_address from SavedSearches union select distinct email_address from UserPrecedence union select distinct email_address from LastLogin ) d First a minor point: As you see I have removed the locking hints. I only did so, to get less noise. But I recommend that you leave out all hints, until you have your query working. That helps you to focus on the essentials. Then a little bigger point: you can remove the DISTINCT, as UNION implies DISTINCT. (Use UNION ALL to retain duplicates.) But the major point is that this just feels wrong. I can't really say what it is right, because I don't know your tables. But it smells like an error in the database design. All I can say is that you should not have to do that. The lesser problem is that when I added SELECT DISTINCT at the bottom of the procedure to avoid duplicates it seems to have thrown off the paging. I have designated 10 rows as a default page parameter. This worked well before I changed the bottom select statement to eliminate duplicates. In my experience an urge to add DISTINCT is a token of that the query is not written in the best way, or that the data model is problematic. As an explanation of the first, maybe there is a JOIN that should have been a WHERE EXISTS instead. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#15
| |||
| |||
|
|
I am wondering if we are talking about the same "SELECT DISTINCT"? |
#16
| |||
| |||
|
|
pbd22 (dush... (AT) gmail (DOT) com) writes: I am wondering if we are talking about the same "SELECT DISTINCT"? Yes, we are. The point of my philosophical discussion was that you should get rid of the duplicates by writing your joins better or refine the data model. But as I still don't know what your query is supposed to achieve, I can't really say how you would do that. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#17
| |||
| |||
|
|
On Jul 20, 2:26 pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote: pbd22 (dush... (AT) gmail (DOT) com) writes: I am wondering if we are talking about the same "SELECT DISTINCT"? Yes, we are. The point of my philosophical discussion was that you should get rid of the duplicates by writing your joins better or refine the data model. But as I still don't know what your query is supposed to achieve, I can't really say how you would do that. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx Hi Erland, Thanks. I have sent you an email. I'll check back here for a continuation of the thread. Thanks again. |
|
From #data main |
#18
| |||||||
| |||||||
|
|
I have been advised to follow one of two possible templates in the constructon of my GROUP BY clause: |
|
I get this error once I have been forced to add every column name in the GROUP BY statement. But, I am trying to just add one... tab1.emailAddress (from the Users table). Shouldn't I just be grouping by this column alone? Assuming that you agree with this solution to my duplicate problem, |
|
CREATE TABLE [dbo].[Users]( [registerDate] [datetime] NULL, [password] [varchar](50) NULL, [role] [varchar](50) NULL, [securityQuestion] [varchar](50) NULL, [securityAnswer] [varchar](50) NULL, [zipCode] [varchar](50) NULL, [alternateEmail] [varchar](50) NULL, [emailAddress] [varchar](50) NULL, [bday_month] [varchar](50) NULL, [bday_day] [varchar](50) NULL, [bday_year] [varchar](50) NULL, [userID] [int] [primary key] IDENTITY(1,1) NOT NULL, |
|
CREATE TABLE [dbo].[SavedSearches]( [saved_id] [int] IDENTITY(1,1) NOT NULL, [saved_query] [text] NULL, [email_address] [varchar](50) NULL, |
|
CREATE TABLE [dbo].[PersonalPhotos]( [photoId] [int] IDENTITY(1,1) NOT NULL, [photo_name] [varchar](50) NULL, [photo_location] [varchar](100) NULL, [photo_size] [varchar](50) NULL, [user_name] [varchar](50) NULL, [photo_caption] [varchar](50) NULL, [photo_default] [bit] NULL, [photo_private] [bit] NULL, [photo_date] [datetime] NULL, [no_photo] [bit] NULL, CONSTRAINT [PK_PersonalPhotos] PRIMARY KEY CLUSTERED ( [photoId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] |
|
CREATE TABLE [dbo].[LastLogin]( [login_id] [int] IDENTITY(1,1) NOT NULL, [login_date] [datetime] NULL, [email_address] [varchar](50) NULL, |
|
CREATE TABLE [dbo].[EditProfile]( [edit_id] [int] IDENTITY(1,1) NOT NULL, [headline] [varchar](50) NULL, [about_me] [text] NULL, [edit_date] [datetime] NULL, [email_address] [varchar](50) NULL, [public_name] [varchar](50) NULL, [interests] [text] NULL, |
#19
| |||
| |||
|
|
Please answer these question: 1) Can there be a user for which there is no email address registered? ANSWER: NO 2) Can there be two users with the same email address? ANSWER: NO |
|
So my assumption is that the answer is no both questions, and that emailAddress should be the primary key of this table... [SNIP] |
#20
| |||||||
| |||||||
|
|
Users Table a) change primary key from userID to emailAddress b) leave userID as unique and not null |
|
Last Login a) create userID column, set it as not null and unique b) remove primary key from login_id column (should I delete this column?) c) make login_date the primary key column d) set foreign key from userID in Last Login (souce) to userID in Users (destination) e) delete the email_address column |
|
PersonalPhotos a) create userID column, set it as not null and unique b) remove primary key from photoId column (should I delete this column?) c) make photo_name the primary key column d) set foreign key from userID in PersonalPhotos (souce) to userID in Users (destination) e) delete the user_name column |
|
SavedSearches a) create userID column, set it as not null and unique b) remove primary key from search_id column (should I delete this column?) c) make search_name the primary key column d) set foreign key from userID in SavedSearches (souce) to userID in Users (destination) e) delete the email_address column |
|
EditProfile a) create userID column, set it as not null and unique b) remove primary key from edit_id column (should I delete this column?) c) make edit_date the primary key column d) set foreign key from userID in EditProfile (souce) to userID in Users (destination) e) delete the email_address column |
|
So, the way I originally had it was: email_address - is the user conducting the search userID - is the userID of the user returned in the search results up_order - is the ZipCode order (by distance) in the results search_name - the name of the saved search But, since we have been using a surrogate key, removing email_address columns and replacing it with userID to denote the current user, I am wondering if it is possible to have two userID columns (one describing the user that conducted the search and the other the users in the results)? I am guessing not. How would you handle this? |
|
If I still am not providing what you want to hear, maybe you could elaborate a little on what you expect in my explanation? |
![]() |
| Thread Tools | |
| Display Modes | |
| |