Search This Blog

Monday, August 29, 2011

SELECT TOP N and BOTTOM N Rows Using SQL Server

With CTETempas(Select
ContractNumber
,ROW_NUMBER() OVER (Order BY ContractNumber) as TopFive ,ROW_NUMBER() OVER (Order BY ContractNumber Desc) as BottomFiveFROM Contracts)Select ContractNumber From CTETemp Where TopFive <=5 or BottomFive <=5
ORDERBY TopFive asc


actual table:

ContractNumberCustAddIdRenewalDateRenew ContractValueDaysUntilRen
1112/8/2008 0:001300-994
221/15/2009 0:000280-956
331/14/2009 0:000280-957
441/21/2009 0:001300-950
55NULLNULL49.99NULL
661/16/2009 0:001280-955
779/15/2008 0:001260-1078
881/3/2009 0:000280-968
992/2/2009 0:001300-938
10101/16/2009 0:001280-955
111112/18/2008 0:00NULL280-984
12123/9/2009 0:001280-903
13135/9/2009 0:000300-842
14146/21/2009 0:000280-799
15159/11/2008 0:001260-1082
16163/16/2009 0:001260-896
17177/27/2008 0:000280-1128
18181/2/2009 0:001280-969
19192/2/2009 0:001300-938
20201/31/2009 0:001280-940
21212/4/2009 0:001280-936

The UNPIVOT operator performs the reverse operation of PIVOT, by rotating columns into rows.


The UNPIVOT operator performs the reverse operation of PIVOT, by rotating columns into rows.

Sample Denormalized Table
CREATE TABLE #Student
(
StudentID int ,
Marks1 float,
Marks2 float,
Marks3 float
)

INSERT INTO #Student VALUES (1, 5.6, 7.3, 4.2)
INSERT INTO #Student VALUES (2, 4.8, 7.9, 6.5)
INSERT INTO #Student VALUES (3, 6.8, 6.6, 8.9)
INSERT INTO #Student VALUES (4, 8.2, 9.3, 9.1)
INSERT INTO #Student VALUES (5, 6.2, 5.4, 4.4)

SELECT * FROM #Student
SELECT StudentID, MarksNo, MarksRecd
FROM
(SELECT StudentID,
Marks1, Marks2, Marks3
FROM #Student) stu
UNPIVOT
(MarksRecd FOR MarksNo IN (Marks1, Marks2, Marks3)
) AS mrks

SQL Server: Export Table to CSV

bcp yourdb..test out D:\test.csv -T -f D:\format.fmt

Sunday, August 28, 2011

6 Common Uses of the undocumented Stored Procedure sp_MSforeachdb

Print all the database names in a SQL Server Instance
EXEC sp_MSforeachdb 'USE ?; PRINT DB_NAME()'

Print all the tables in all the databases of a SQL Server Instance
EXEC sp_MSforeachdb 'USE ? SELECT DB_NAME() + ''.'' + OBJECT_NAME(object_Id) FROM sys.tables'

EXEC sp_MSforeachdb 'USE ? SELECT OBJECT_NAME(object_Id) FROM sys.tables where DB_NAME() NOT IN(''master'', ''model'', ''msdb'', ''tempdb'')'

Display the size of all databases in a SQL Server instance
EXEC sp_MSforeachdb 'USE ?; EXEC sp_spaceused'

Determine all the physical names and attributes(size,growth,usage) of all databases in a SQL Server instance
EXEC sp_MSforeachdb 'USE ? EXEC sp_helpfile;'

Change Owner of all databases to 'sa'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_changedbowner ''sa'''

Check the Logical and Physical integrity of all objects in the database
sp_MSforeachdb 'DBCC CHECKDB(?)'

















 

Tuesday, August 23, 2011

TSQL Beginners Challenge 4 - Concatenating values from multiple rows


 
SELECT id, Stuff(name, ( Len(name) - 3 ), Len(name), '') FROM (SELECT DISTINCT id,
(SELECT a.name + ' ' + b.chk + ' ' FROM tc4 a WHERE a.id = b.id FOR XML PATH('')) AS name FROM (SELECT *, CASE WHEN r%2 = 0 THEN ' AND ' WHEN r%3 = 0 THEN ' OR ' ELSE '' END AS chk FROM (SELECT *,COUNT(id) OVER(PARTITION BY id ) AS r FROM tc4) df) b)bb

Using OVER() with Aggregate Functions


One of new features in SQL 2005 that I haven't seen much talk about is that you can now add aggregate functions to any SELECT (even without a GROUP BY clause) by specifying an OVER() partition for each function. Unfortunately, it isn't especially powerful, and you can't do running totals with it, but it does help you make your code a little shorter and in many cases it might be just what you need.
The way it works is similar to joining an aggregated copy of a SELECT to itself. For example, consider the following:

You can now easily return the total CNT per Area as an additional column in this SELECT, simply by adding an aggregate SUM() function with an OVER() clause:

for Example
select

Area, nvcQuestionName, [CNT], nvcResultDesc,Sum(CNT) OVER (Partition by Area) as Total from sample1

Wednesday, August 10, 2011

ORDER BY Clause and TOP WITH TIES

WITH TIES can be used only with TOP and ORDER BY, both the clauses are required. Let us understand from one simple example how this clause actually works. Suppose we have 100 rows in the table and out of that 50 rows have same value in column which is used in ORDER BY; when you use TOP 10 rows, it will return you only 10 rows, but if you use TOP 10 WITH TIES, it will return you all the rows that have same value as that of the last record of top 10 — which means a total of 50 records

For Example: select top 10 with ties * from tablename order by 1