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 Voter ID (1 or 2 of course).

Each time a vote is made in the vote, a timestamp is held.
Each vote line is related to a post (FK post id ).
Each vote line is related to any user (FK user id ).

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: hasvated up and hasdatedown

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 ...

 < 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 

< / 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 (Select number (1) with votes where user Id = 1 and postid = PID and Voteepid = 1 and Creative> (Select from Vote by Vote, where User Id = 1 And postid = PID and Votepid = 2) as bit of hatwit-up, (Select votes to count (1) where user id = 1 and postid = PID and whitepayed = 2 and creationdate> (bits) As user ID = 1 and postid = creates creativity with p.id and votetypeid = 1 votes Select)) Hashoutdown Down

Should be qualified:

  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

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -