Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Folks,(I have used the 'pubs' database)I'm looking to modify the SQL below to develop a derived table with a row count. At the moment it assigned a counter for every record in the 'sales' table regardless of any filter in the where clause. Can anyone help me get the counter to only include those records as specified by the where clause. You see the row counter starts at 4 because there are 3 records where order <'6871'I have used the 'pubs' database from SQL serveruse pubsselect a1.ord_num,count(a2.ord_num)as counterfrom(select sales.ord_num,count(sales.ord_num)as jcount from sales group by sales.ord_num)a1,(select sales.ord_num,count(sales.ord_num)as jcount from sales group by sales.ord_num)a2where a1.ord_num>=a2.ord_num and a1.ord_num >'6871'group by a1.ord_num, a1.jcountthis would really dig me out of a hole, so thanks in advanceregardsJames
rrb
SQLTeam Poet Laureate
1479 Posts
Posted - 2005-09-14 : 20:09:21
hi james - I think you can solve your problem by joining to one of your subqueries, but not sure because I don't have the pubs database herecould you post some example data and a row or two of the result you want?--I hope that when I die someone will say of me "That guy sure owed me a lot of money"
nosepicker
Constraint Violating Yak Guru
366 Posts
Posted - 2005-09-14 : 20:44:45
How about this:
select a1.ord_num, count(a2.ord_num) as counterfrom (select sales.ord_num, count(sales.ord_num) as jcount from sales where ord_num > '6871' group by sales.ord_num) a1, (select sales.ord_num, count(sales.ord_num) as jcount from sales where ord_num > '6871' group by sales.ord_num) a2where a1.ord_num >= a2.ord_num group by a1.ord_num, a1.jcount