February 29, 2008 at 6:35 am
First time caller here
I have a simple stored procedure to group data
SELECT VatCode FROM Partner GROUP BY VatCode
which gives
STANDARD
VAT FREE
SPECIAL RATE
This works fine but I need to add a sequential number on to each result row like
1 STANDARD
2 VAT FREE
3 SPECIAL RATE
Any help would be appreciated
February 29, 2008 at 6:58 am
SELECT ROW_NUMBER () OVER (ORDER BY VatCode),
VatCode
FROM Partner
GROUP BY VatCode
--Jeff Moden
Change is inevitable... Change for the better is not.
February 29, 2008 at 6:59 am
SELECT
ROW_NUMBER() OVER(ORDER BY VatCode) AS [ROW],
VatCode
FROM Partner
GROUP BY VatCode
February 29, 2008 at 6:59 am
Beat me too it 😎
February 29, 2008 at 7:04 am
Crikey chaps, I am so impressed
Sorted
February 29, 2008 at 7:05 am
Yeah, but nothing like instant confirmation that you're right, huh? Thanks, Adam...
--Jeff Moden
Change is inevitable... Change for the better is not.
February 29, 2008 at 8:33 am
Anything comparable in SQL2000??
-- You can't be late until you show up.
February 29, 2008 at 8:42 am
No. Probably the easiest way is to create a temp table with an identity column, insert your rows into that, then select from it.
John
February 29, 2008 at 8:52 am
The row_number function was introduced in SQL 2005. As John said, the best/easiet way to accomplish this is using temp tables.
February 29, 2008 at 1:10 pm
John Mitchell (2/29/2008)
No. Probably the easiest way is to create a temp table with an identity column, insert your rows into that, then select from it.John
I'll third that...
--Jeff Moden
Change is inevitable... Change for the better is not.
February 29, 2008 at 1:28 pm
Of course, you could go old-school with a correlated sub-query. Celko would be so proud of me…
select
RowNumber =
(
select
Cnt = count(distinct b.VatCode)+1
from
Partner b
where
a.VatCode > b.VatCode
),
a.VatCode
from
Partner a
group by
a.VatCode
order by
a.VatCode
February 29, 2008 at 1:40 pm
Heh... yeah, I'll just bet he would, Micheal...
http://www.sqlservercentral.com/articles/T-SQL/61539/
--Jeff Moden
Change is inevitable... Change for the better is not.
February 29, 2008 at 2:27 pm
I was just kidding about the correlated sub-query; I wouldn't use it.
I locked horns with Celko on this very issue about 12 years ago on the old Compuserve SQL Server formula. I suggested to someone that they use the identity method, and Celko complained that it wasn’t ANSI SQL. I pointed out that we were on a SQL Server forum, not an ANSI SQL forum, and that was enough to get me the full treatment.
February 29, 2008 at 2:38 pm
Heh... I know... not a problem, Michael... just having a bit of fun 'cause you used the "C" word 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply