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 |
krainov
Yak Posting Veteran
57 Posts |
Posted - 2008-09-27 : 12:40:56
|
Hi there!Need help on this complicated way to say Hooray :-)As a part of an web site I have a Photo Gallery where I decided to have "Today Best Photo" - kind of addition to make the site visitors feel the spirit of competition posting the images to the web site. So there are actually 5 tables created for this PhotoGallery which are: Categories, Medias(as Albums), Images, ImageComments, CommentsCount, ImagesViewCounts, ImagesVotes, TodayFavoriteImages.When the new image is inserted into table "Images" today, and right after someone is Voting on photo or inserting the new comment on it I run this MESS MY POSSIBILITIES (See the transact body below:-) So dont blame me! This is my first startUp project on asp.net! This procedure is inserting the ImageID, Date, and TotalTodayRating into "TodayFavoriteImages" table. The TotalTodayRating consist of number of today ViewCounts, todayVotes (values from 1 to 5) and today CommentsCount. I know that this way is too complicated and that there are other easier ways! Help to simplify this procedure!ALTER PROCEDURE dbo.tbh_Test_Portfolios_Images_ADD_to_TodayImage(@TodayImageID int OUTPUT)ASSET NOCOUNT ON/*Declare TODAY date*/DECLARE @TodayIs datetimeSELECT @TodayIs = GetDate()/*All today images into tempTable*/SELECT ImageID INTO #userImages From tbh_Portfolios_ImagesWHERE DATEDIFF(DAY,'', tbh_Portfolios_Images.AddedDate) = DATEDIFF(DAY, '', @TodayIs)/*1 tempTable more for collecting the viewCouns, Sum Votes and commentsCount ???-?? ?????????? ?? ???????, ????? ?????? ? ?????????? ????????????*/SELECT --IDImageID,(--TotalRating(SELECT SUM(Vote) FROM tbh_Portfolios_Images_Votes where ImageID = #userImages.ImageID) +--TodayComments(SELECT COUNT(*) FROM tbh_Portfolios_Images_Comments where ImageID = #userImages.ImageID) +--TodayViewCount(SELECT COUNT(*) FROM tbh_Portfolios_Images_ViewCounts where ImageID = #userImages.ImageID)) AS TotalImageRatingINTO #todayImagesFROM #userImages/*Declare highest today rating imageID */DECLARE @TodayMaxRatingID int/*Find highest today rating imageID in #todayImages*/SELECT @TodayMaxRatingID = (SELECT TOP 1 ImageIDFROM #todayImagesORDER BY TotalImageRating DESC)/*Lets count the @TodayMaxRatingIdVotes*/DECLARE @TodayMaxRatingIdVotes intSELECT @TodayMaxRatingIdVotes = (SELECT TotalImageRatingFROM #todayImagesWHERE ImageID = @TodayMaxRatingID)-- check is there @TodayMaxRatingID in table DECLARE @CurrID intSELECT @CurrID = ImageID FROM tbh_Portfolios_Images_Today_ImageWHERE ImageID = @TodayMaxRatingIDAND DATEDIFF(DAY,'', AddedDate) = DATEDIFF(DAY, '', @TodayIs)--If yesIF @CurrID IS NOT NULLBEGIN/*Refresh VotesCount in table /UPDATE tbh_Portfolios_Images_Today_ImageSET VotesCount = @TodayMaxRatingIdVotesWHERE ImageID = @TodayMaxRatingIDRETURNEND--If notIF @CurrID IS NULLBEGIN--Delete the image that was before this oneDELETE tbh_Portfolios_Images_Today_ImageWHERE DATEDIFF(DAY,'', tbh_Portfolios_Images_Today_Image.AddedDate) = DATEDIFF(DAY, '', @TodayIs)/*Insert new record"*/INSERT INTO tbh_Portfolios_Images_Today_Image (ImageID, AddedDate, VotesCount)VALUES (@TodayMaxRatingID, @TodayIs, @TodayMaxRatingIdVotes)SET @TodayImageID = scope_identity()END |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-09-27 : 12:46:41
|
duplicatehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=111530 |
|
|
|
|
|
|
|