Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count Rows

Author  Topic 

tjwent69
Starting Member

30 Posts

Posted - 2009-11-27 : 09:56:05
I am trying to simply display a counter for the rows I find.

Basically I have an item, I have item discounts. I want to display the item and all of the discounts but I want each returned row of discounts to have a row count. I can do this in reporting services but I want to do it in t-sql. I have read through @@rowcount, cursors, while loops in BOL and Forums and they all seem to complicated or not quite right. Typically I don't know the 'keyword' to find what I want to do.

Select '1234',27.00,29.99
union
Select '1234',30.00,39.99
union
Select '1234',40.00,99.99
union
Select '2345',27.00,99.99
union
Select '3456',30.00,39.99
union
Select '3456',40.00,49.99
union
Select '3456',50.00,59.99
union
Select '3456',60.00,59.99
union
Select '3456',70.00,69.99
union
Select '3456',80.00,79.99

I want the above sample data to look like this.

1 1234 27 29.99
2 1234 30 39.99
3 1234 40 99.99
1 2345 27 99.99
1 3456 30 39.99
2 3456 40 49.99
3 3456 50 59.99
4 3456 60 59.99
5 3456 70 69.99
6 3456 80 79.99




madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-27 : 09:58:58

Which version of SQL Server are you using?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tjwent69
Starting Member

30 Posts

Posted - 2009-11-27 : 12:22:55
2005
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-28 : 01:18:55
select row_number() over (partition by col1 order by col1) as row_count,* from your_table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -