dbTalk Databases Forums  

Strange SELECT statement

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss Strange SELECT statement in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #21  
Old   
Mike C#
 
Posts: n/a

Default Re: Strange SELECT statement - 02-25-2008 , 07:01 PM






Assuming you have a table like the following:

CREATE TABLE #emp ([Name] VARCHAR(20));
GO

INSERT INTO #emp ([Name]) VALUES ('Peter');
INSERT INTO #emp ([Name]) VALUES ('John');
INSERT INTO #emp ([Name]) VALUES ('Daniel');
INSERT INTO #emp ([Name]) VALUES ('Bob');
GO

SQL 2005 solution:

SELECT [Name], ROW_NUMBER() OVER (ORDER BY [Name]) * 5 AS SpecialNo
FROM #emp;
GO

SQL 2000 solution:

SELECT e1.[Name], COUNT(*) * 5 AS SpecialNo
FROM #emp e1
INNER JOIN #emp e2
ON e2.[Name] <= e1.[Name]
GROUP BY e1.[Name]
GO


"Deiny García Pérez" <deiny (AT) nnn (DOT) com> wrote

Quote:
Hello Everyone...

I have a particular Select statement to do... for example I want a
Select like this:

Select Name, SpecialNo from emp... etc. and get result like this:

Name SpecialNo
Peter 5
John 10
Daniel 15
Bob 20

But the problem is this, I need SpecialNo to be a generated number, not a
real exixting data and also I want this number to increment in every row
(in this case is like John's SpecialNo = Peter's SpecialNo + 5 and so on)

Please, help me, thanks in advance




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.