declare @AlphaList table(AlphaKey char);insertinto @AlphaList(AlphaKey)values('A');insertinto @AlphaList(AlphaKey)values('A');insertinto @AlphaList(AlphaKey)values('B');insertinto @AlphaList(AlphaKey)values('B');insertinto @AlphaList(AlphaKey)values('C');insertinto @AlphaList(AlphaKey)values('D');insertinto @AlphaList(AlphaKey)values('D');insertinto @AlphaList(AlphaKey)values('E');select AlphaKey from @AlphaList orderby1;
//Returns the output : 1 A 2 A 3 B 4 B 5 C 6 D 7 D 8 EselectROW_NUMBER()over(orderby AlphaKey)as RowNumber, AlphaKeyfrom @AlphaList;
//Returns the output : 1 1 A 1 2 A 3 3 B 3 4 B 5 5 C 6 6 D 7 7 D 8 8 Eselect
RANK()over(orderby AlphaKey)as Rank,ROW_NUMBER()over(orderby AlphaKey)as RowNumber, AlphaKeyfrom @AlphaList;
/* checks whether a number is Digits, Decimal, floating point, number, money
*/
// A point of summary
SELECT[Ascii Code]=STR(Number),[Ascii Character]=CHAR(Number),[ISNUMERIC Returns]= ISNUMERIC(CHAR(Number))FROM Master.dbo.spt_ValuesWHERE Type ='P'AND Number BETWEEN0AND255AND ISNUMERIC(CHAR(Number))=1
//Results
SELECT ISNUMERIC(',') // returns 1 since the decimal contains ','
SELECT ISNUMERIC('$') // returns 1 since the money contain '$'
SELECT ISNUMERIC('€') // returns 1 since the money contain '€'
SELECT ISNUMERIC('£') // returns 1 since the money contain '£'
SELECT ISNUMERIC('¥') // returns 1 since the money contain '¥'
// so ISNUMERIC is not a permanent solution for checking ISALLDIGITS
// ISALLDIGITS SnippetCREATEFUNCTION dbo.IsAllDigits/********************************************************************
Purpose:
This function will return a 1 if the string parameter contains only
numeric digits and will return a 0 in all other cases. Use it in
a FROM clause along with CROSS APPLY when used against a table.
--Siva Shunmugam
********************************************************************/(@MyString VARCHAR(8000))RETURNSTABLEASRETURN(SELECTCASEWHEN @MyString NOTLIKE'%[^0-9]%'THEN1ELSE0ENDAS IsAllDigits
IF OBJECT_ID('tempdb..#MyHead','U')ISNOTNULL DROPTABLE #MyHead
SELECTTOP(1000000) SomeDateTime =RAND(CHECKSUM(NEWID()))*7305+ CAST('2005'ASDATETIME), SomeAmount = CAST(RAND(CHECKSUM(NEWID()))*100ASMONEY) INTO #MyHead
FROM master.sys.all_columns ac1
CROSSJOIN master.sys.all_columns ac2
;
/* Add a clustered index to the table just to keep it from being a heap -- and to help with speed a bit by sorting the data.*/
CREATECLUSTEREDINDEX IX_#MyHead_SomeDateTime
ON #MyHead (SomeDateTime) ;
/* This doesnt sorts based on the Month Number */
SELECT[Month]=DATENAME(mm,SomeDateTime), Amount =SUM(SomeAmount) FROM #MyHead
WHERE SomeDateTime >='2010'AND SomeDateTime <'2011' GROUPBYDATENAME(mm,SomeDateTime) ;
/* The below sorts based on the Month Number */
SELECT[Month]=DATENAME(mm,SomeDateTime), Amount =SUM(SomeAmount) FROM #MyHead
WHERE SomeDateTime >='2010'AND SomeDateTime <'2011' GROUPBYDATENAME(mm,SomeDateTime) ORDERBY CAST(DATENAME(mm,SomeDateTime)+' 1900'ASDATETIME) ;
FYI: CROSS Apply works better than Filter in terms of performance
/* To display the records using Filter */ /* Create a temporary table */
CREATE TABLE #Runners ( Runner integer NOT NULL, Time integer NOT NULL, Age integer NOT NULL )
/* Insert sample records into the table*/
INSERT INTO #Runners
SELECT 1 , 10 , 20
UNION ALL
SELECT 2 , 15 , 20
UNION ALL
SELECT 3 , 11 , 20
UNION ALL
SELECT 4 , 12 , 30
UNION ALL
SELECT 5 , 18 , 30
UNION ALL
SELECT 6 , 9 , 40
UNION ALL
SELECT 7 , 16 , 40
UNION ALL
SELECT 8 , 13 , 30
UNION ALL
SELECT 9 , 13 , 30;
/* To retrieve the top X from each group using filter */
/* If both the time has the same time then the row number will be different for each same row */
with cteRunners as ( select * ,row_number()over(partitionby Age orderby Time )as RowN from #Runners )
Select * from cteRunners where RowN <=2 orderby Age,Rown;
/* To give same row number for the same time present use DENSE_RANK */ with cteRunners as ( select*,DENSE_RANK()over (partitionby Age orderbyTime)as RowN from #Runners )
Select *from cteRunners where RowN <=2 orderby Age,Rown
/* To drop the temporary table */
DROP TABLE #Runners
/* Using CROSS APPLY */
IF OBJECT_ID('tempdb..#RunnersBig') IS NOT NULL drop table #RunnersBig
/* Create a table and enter the millions of sample data for checking the performance */ Create Table #RunnersBig ( RunnerId integer identity , Time integer not null, Age integer not null ) go
insert into #runnersbig ( Time , Age ) select top 1000000 ABS ( checksum ( newid ()))% 1000 , ABS ( checksum ( newid ()))% 99 from sys . columns a cross join sys . columns b cross join sys . columns c go
/* Create a Index */ create index idxrunnersbig on #runnersbig ( age , time ) include ( runnerid );
with cteN as ( select distinct Age number from #RunnersBig ) Select * from cteN cross apply ( Select top ( 2 ) * from #RunnersBig where #RunnersBig . Age = cteN . number order by Time ) as runners order by cteN . number , runners . Time
Siva Shunmugam is a software Professional works for Effindi Technologies. He loves technology because it makes the life easy and likes socializing. By beeing a professional he comes to know about how the technology is beeing handled in the real time world. Among the many technological programes he uses and embraces SQL-SERVER, ASP.NET, JQUERY simply because he uses it in his profession.
DISCLAIMER
This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet/ BOL. My employer do not endorse any tools, applications, books, or concepts mentioned on the blog. I have documented my personal experience on this blog.