During Winter Bash 2014, the Naruto hat was awarded to users who gave an answer that got accepted but hasn't received votes for 12 hours. It was a weird thing, really: why would someone ever accept a question but not upvote it? I haven't yet experienced a situation where this would be reasonable.
Accepted answers without upvotes seem to be an anomaly: it happens when the OP doesn't have enough reputation to upvote, and the post gets overlooked by other users in the community. The community might actually want to clean these up.
DECLARE @username as NVarchar(60) = RTRIM(LTRIM(##DisplayName:string? ##)); DECLARE @userId as int = ##UserId:int?-1##; SELECT TOP 100 u.id as [User Link], p.Id as [Post Link], p.CreationDate FROM Posts p JOIN Users u ON p.OwnerUserId = u.Id JOIN Posts q ON p.ParentId = q.Id WHERE (@username = '' OR u.DisplayName = @username) AND (@userId = -1 OR u.Id = @userId) AND p.Score = 0 AND p.Id = q.AcceptedAnswerId ORDER BY p.CreationDate DESC
The query accepts two parameters:
- Optional display name, to filter by username
- Optional user id, to filter by user id
- With default values it will show all eligible posts of the given site
- It won't make sense to set both display name and user id, but such misuse would be the user's fault
Something I don't like about this is the username parameter. I would have wanted NULL or empty string as default value but couldn't figure out how to do that. Using space does work, but it's not exactly pretty, I'm wondering if there's a better way I'm missing.
I'm interested in any other suggestions to improve this query.