Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-25 : 18:25:45
|
| ALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName]( @ViewedUsernameID int, @FromDate DATETIME = NULL)ASSELECT TOP 100A.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViewsA.[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViewsA.[TimeStamp] as ViewedDate, ----- table tbl_ProfileViewsB.[PhotoID], ----- table tbl_PhotosB.[ProfileID], ----- table tbl_PhotosB.[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews A INNER JOIN tbl_Photos B ON A.UserNameID = B.ProfileIDWHERE A.ViewUserNameID = @ViewedUsernameID ---AND B.[p_Primary] = '1'AND A.[TimeStamp] = (SELECT MAX(C.[TimeStamp]) from tbl_ProfileViews C Where C.ViewUserNameID = @ViewedUsernameID) ORDER BY A.[TimeStamp] DESCI am getting many intances p_Primary is the main image if 1 then there is an image.Other I should get one instance if there are no primary images that is the last dated one.31 44 2011-01-24 17:22:50.023 44 31 0------------------------------------------------------------31 44 2011-01-24 17:22:50.023 40 31 031 44 2011-01-24 17:22:50.023 41 31 031 44 2011-01-24 17:22:50.023 42 31 031 44 2011-01-24 17:22:50.023 43 31 031 44 2011-01-24 17:22:50.023 44 31 031 44 2011-01-24 17:22:50.023 47 31 1Any help would be awesome. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-26 : 11:06:01
|
do you mean this?ALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName](@ViewedUsernameID int,@FromDate DATETIME = NULL)ASSELECT TOP 100 ViewerUserNameID,ViewedUserNameID,ViewedDate,PhotoID,ProfileID,p_PrimaryFROM(SELECTROW_NUMBER() OVER (PARTITION BY A.[UserNameID],A.[ViewUserNameID],A.[TimeStamp] ORDER BY CASE WHEN B.[p_Primary] = 1 THEN 0 ELSE B.[PhotoID] END DESC) AS rn,A.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViewsA.[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViewsA.[TimeStamp] as ViewedDate, ----- table tbl_ProfileViewsB.[PhotoID], ----- table tbl_PhotosB.[ProfileID], ----- table tbl_PhotosB.[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews A INNER JOIN tbl_Photos B ON A.UserNameID = B.ProfileIDWHERE A.ViewUserNameID = @ViewedUsernameID )tWHERE rn=1 OR [p_Primary] = '1'ORDER BY [TimeStamp] DES ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-26 : 12:03:51
|
| Msg 102, Level 15, State 1, Procedure prc_ProfileViews_SelectByUserName, Line 29Incorrect syntax near 'DES'.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViews [ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViews [TimeStamp] as ViewedDate, ----- table tbl_ProfileViews [PhotoID], ----- table tbl_Photos [ProfileID], ----- table tbl_Photos [p_Primary] ----- table tbl_Photos I need to return the last instance regardless p_primary = 1 This is not necessary. ALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName]( @ViewedUsernameID int, @FromDate DATETIME = NULL)ASSELECT TOP 100[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViews[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViews[TimeStamp] as ViewedDate, ----- table tbl_ProfileViews[PhotoID], ----- table tbl_Photos[ProfileID], ----- table tbl_Photos[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews INNER JOIN tbl_Photos ON tbl_Photos.ProfileID = tbl_profileViews.UserNameIDWHERE tbl_ProfileViews.ViewUserNameID = @ViewedUsernameID AND (@FromDate IS NULL OR (([TimeStamp] >= @FromDate)AND [TimeStamp] = (SELECT MAX([TimeStamp]) FROM tbl_ProfileViews A where A.UserNameID = tbl_profileViews.UserNameID)))ORDER BY [TimeStamp] DESC31 44 2011-01-24 17:22:50.023 40 31 031 44 2011-01-24 17:22:50.023 41 31 031 44 2011-01-24 17:22:50.023 42 31 031 44 2011-01-24 17:22:50.023 43 31 031 44 2011-01-24 17:22:50.023 44 31 031 44 2011-01-24 17:22:50.023 47 31 045 44 2011-01-20 11:36:12.940 50 45 147 44 2011-01-20 11:34:24.320 52 47 1I need to return this.31 44 2011-01-24 17:22:50.023 47 31 045 44 2011-01-20 11:36:12.940 50 45 147 44 2011-01-20 11:34:24.320 52 47 1Any help would be awesome. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-26 : 12:11:34
|
| thats just a typo . make last statement asORDER BY [TimeStamp] DESC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-26 : 12:33:03
|
| Did you look at the last query.I need help on fixing that.p_primary is not necessaryAny help would be awesome. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-26 : 12:37:36
|
| [code]ALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName](@ViewedUsernameID int,@FromDate DATETIME = NULL)ASSELECT TOP 100 ViewerUserNameID,ViewedUserNameID,ViewedDate,PhotoID,ProfileID,p_PrimaryFROM(SELECTROW_NUMBER() OVER (PARTITION BY A.[UserNameID],A.[ViewUserNameID],A.[TimeStamp] ORDER BY B.[PhotoID] DESC) AS rn,A.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViewsA.[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViewsA.[TimeStamp] as ViewedDate, ----- table tbl_ProfileViewsB.[PhotoID], ----- table tbl_PhotosB.[ProfileID], ----- table tbl_PhotosB.[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews A INNER JOIN tbl_Photos B ON A.UserNameID = B.ProfileIDWHERE A.ViewUserNameID = @ViewedUsernameID )tWHERE rn=1 ORDER BY [TimeStamp] DESC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-26 : 13:00:52
|
| Msg 207, Level 16, State 1, Procedure prc_ProfileViews_SelectByUserName, Line 28Invalid column name 'TimeStamp'.Any help would be awesome. |
 |
|
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-26 : 14:00:57
|
| ALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName](@ViewedUsernameID int,@FromDate DATETIME = NULL)ASSELECT TOP 100 ViewerUserNameID,ViewedUserNameID,ViewedDate,PhotoID,ProfileID,p_PrimaryFROM(SELECTROW_NUMBER() OVER (PARTITION BY A.[UserNameID],A.[ViewUserNameID],A.[TimeStamp] ORDER BY B.[PhotoID] DESC) AS rn,A.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViewsA.[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViewsA.[TimeStamp] as ViewedDate, ----- table tbl_ProfileViewsB.[PhotoID], ----- table tbl_PhotosB.[ProfileID], ----- table tbl_PhotosB.[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews A INNER JOIN tbl_Photos B ON A.UserNameID = B.ProfileIDWHERE A.ViewUserNameID = @ViewedUsernameID )tWHERE rn=1 ORDER BY ViewedDate DESCI am getting the following without the max date31 44 2011-01-24 17:22:50.023 47 31 131 44 2011-01-24 09:55:35.610 47 31 145 44 2011-01-20 11:36:12.940 50 45 147 44 2011-01-20 11:34:24.320 52 47 131 44 2011-01-18 21:04:26.860 47 31 149 44 2011-01-18 14:27:18.370 64 49 049 44 2011-01-18 14:21:54.603 64 49 047 44 2011-01-13 14:50:42.130 52 47 147 44 2011-01-13 11:47:04.007 52 47 147 44 2011-01-13 11:47:00.537 52 47 147 44 2011-01-13 11:45:50.373 52 47 131 44 2011-01-10 15:39:02.077 47 31 131 44 2011-01-07 20:54:42.763 47 31 131 44 2011-01-07 20:39:55.453 47 31 131 44 2011-01-07 20:36:02.497 47 31 1Any help would be awesome. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-27 : 12:13:14
|
so your date also varies? this was not as per initial sample postedALTER PROCEDURE [dbo].[prc_ProfileViews_SelectByUserName](@ViewedUsernameID int,@FromDate DATETIME = NULL)ASSELECT TOP 100 ViewerUserNameID,ViewedUserNameID,ViewedDate,PhotoID,ProfileID,p_PrimaryFROM(SELECTROW_NUMBER() OVER (PARTITION BY A.[UserNameID],A.[ViewUserNameID] ORDER BY A.[TimeStamp] DESC) AS rn,A.[UserNameID] as ViewerUserNameID, ----- table tbl_ProfileViewsA.[ViewUserNameID] as ViewedUserNameID, ----- table tbl_ProfileViewsA.[TimeStamp] as ViewedDate, ----- table tbl_ProfileViewsB.[PhotoID], ----- table tbl_PhotosB.[ProfileID], ----- table tbl_PhotosB.[p_Primary] ----- table tbl_PhotosFROM tbl_ProfileViews A INNER JOIN tbl_Photos B ON A.UserNameID = B.ProfileIDWHERE A.ViewUserNameID = @ViewedUsernameID )tWHERE rn=1 ORDER BY ViewedDate DESC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mcupryk
Yak Posting Veteran
91 Posts |
Posted - 2011-01-27 : 12:29:54
|
| Awesome job visa!!!Any help would be awesome. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-27 : 12:35:15
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|