Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Use this for problems that involve returning one or more rows from each group in a data set. Despite the name, this tag is suitable for *minimum-* and *maximum-* per group queries.
1
vote
Compare last data and second last data sql server based on id
Assuming SQL Server 2012 or newer, you can use the LAG() function:
;WITH x AS
(
SELECT Dt, Value, CompanyId,
rn = ROW_NUMBER() OVER (PARTITION BY CompanyID ORDER BY Dt DESC),
prev = LAG(Valu …
3
votes
Accepted
Select all most recent records
Your query includes a lot of tables and columns that aren't listed in your sample data. From this query you can join and pull in other things:
;WITH lastMessage AS
(
SELECT Id, IdMain, IdReply, D …
50
votes
Retrieving n rows per group
The typical way to do this in SQL Server 2005 and up is to use a CTE and windowing functions. For top n per group you can simply use ROW_NUMBER() with a PARTITION clause, and filter against that in th …
2
votes
Join between different frequency tables
Since you have defined quarters using the traditional calendar definition, you don't need to store the "end" of a quarter anywhere, particularly once for every single day in a ~90-day period.
(And th …
11
votes
The fastest way to get the most recent records
Self-joins seem cheap at low row counts, but I/O is exponential as the row count increases. I would prefer to solve this the CTE way, unless you are on SQL Server 2000 (please always specify the versi …