sql - Determine most recent 'vote type' for a record, returning 2 fields for HasVotedUp/Down? -
I have a SQL Server 2008 table that contains 'votes', either a 'vote vote' or 'vote down' 'Are Each time a vote is made in the vote, a timestamp is held. I have a query to return a list of posts, and in the same question, I want to know whether the current user has voted or not (or not at all). In some cases, a single post may include both an up and down vote, where the user changed his mind at a later date (changing one vote below vote) in this scenario , I want to identify the latest vote as the relevant vote (the original vote is held down just for the historical purposes in DB). Ideally, I should revert back to 2 simple bit areas within the query: I can include something like this (cut for short). Just enter the 'HasVotedUp' and 'HasVotedDown' property code on the basis of VoteTypeId, return the most recent vote for that user / post whether or not the vote is top ... < / Pre> ... but I was surprised that such a simple (but important, efficient) vision Which was the angle that I could use to return 2 'hasdated up / down' fields within the query, so is it ready to use in the plate? This nested approach came along with everything but I su have it disabled ?! Select Should be qualified: Voter ID (1 or 2 of course).
Each vote line is related to a post (FK post id ).
Each vote line is related to any user (FK user id ).
hasvated up and
hasdatedown
< Select the code> P.id, p.body, (select the top 1 Votipede from the posts, where user id = 1 and postid = PID according to the order creation) the current vetoteep id from the post
choose p.id case, when vv.votetypeid = 1 THEN 1 ELSE 0 END as HasVotedUp, case when vt.votetypeid = 2 THEN 1 ELSE 0 END In the left side of the HaspotedDown posts in the left outer join (v.postid, select v.votetypeid in the vote V Inner section (post group, UserAd, Max (Creativity) as creation by creation post group, vote group, UserId). Posti Most recent on d = newest.postid and v.userid = newest.userid and v.creationdate = new.creationdate where v.userid = 1) vt = vt.postid at PTI
Comments
Post a Comment