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.
Dear i want To Count RowI am Doing Like Thisselect t1.ItemNo,t1.DocDate,(SELECT TOP 1 COUNT(*)FROM SAP tWHERE t.itemNo = t1.itemNo AND t.DocDate = t1.DocDate ) AS rnfrom SAP t1ORDER by DocDate,ItemNo,rnbut Receiving data like this(Item no) (Date) (Row No)SF00037 2011-09-09 00:00:00 4SF00037 2011-09-09 00:00:00 4SF00037 2011-09-09 00:00:00 4SF00037 2011-09-09 00:00:00 4I want Data Like this(Item no) (Date) (Row No)SF00037 2011-09-09 00:00:00 0SF00037 2011-09-09 00:00:00 1SF00037 2011-09-09 00:00:00 2SF00037 2011-09-09 00:00:00 3Any IdeasThanks in Advance
Lumbago
Norsk Yak Master
3271 Posts
Posted - 2011-10-07 : 08:30:40
A little confused about your requirement but is this what you want? ->
SELECT *FROM ( select t1.ItemNo,t1.DocDate, RowNum = ROW_NUMBER() OVER (ORDER BY DocDate,ItemNo) - 1 from SAP t1) aORDER by DocDate,ItemNo,RowNum
no i want to count Itemno where order by Date and ItemnoJust like this(Item no) (Date) (Row No)SF00037 2011-09-09 00:00:00 0SF00037 2011-09-09 00:00:00 1SF00037 2011-09-09 00:00:00 2SF00037 2011-09-09 00:00:00 3SF00038 2011-09-09 00:00:00 0SF00038 2011-09-09 00:00:00 1
Lumbago
Norsk Yak Master
3271 Posts
Posted - 2011-10-07 : 09:01:54
Ok, here goes:
SELECT *FROM ( select t1.ItemNo,t1.DocDate, RowNum = ROW_NUMBER() OVER (PARTITION BY DocDate,ItemNo ORDER BY DocDate,ItemNo) - 1 from SAP t1) aORDER by DocDate,ItemNo,RowNum