Home > SQL > OVER and WITH in t-sql

OVER and WITH in t-sql

OVER Clause

Determines the partitioning and ordering of the rowset before the associated window function is applied.
URL: http://msdn2.microsoft.com/en-us/library/ms189461.aspx

WITH

Specifies a temporary named result set, known as a common table expression (CTE).
URL: http://msdn2.microsoft.com/en-us/library/ms175972.aspx

Below code demonstrates the creation of a temperory table and add a field new field which is an ID field of a sorted column


WITH temp_table AS
(
SELECT *, (ROW_NUMBER() OVER (ORDER BY field_name)) AS row FROM table_name
)

SELECT * FROM temp_table

The above code can also used for paging featuers ;) . Like MySQL’s LIMIT start, length.


SELECT * FROM temp_table WHERE row BETWEEN 10 AND 15

Useful, right?

Ref: Inspired from one of my friend Amit Benswal’s blog post

VN:F [1.9.18_1163]
Rating: 5.0/5 (1 vote cast)
OVER and WITH in t-sql, 5.0 out of 5 based on 1 rating

No related posts.

Categories: SQL