sql server - Count how many rows are for a value in SQL? -


i have table looks this:

[contractid]     [contractdate] [snapshottimeid]    [dayspastdue] [exposure]  int(not unique)  datetime       int(format20160431) int           int 

the table sorted contractid, contractdate.

now, add 6th column, let's call unique, has value 1 first contractid value adds 1 until bumps across next contractid. basically, want know how many rows have each contractid , put values, incrementally, in column.

edit: want output this

>documentid contractdate    snapshottimeid  dpd exposure unique >1          31-aug-15       31-aug-15       0   500      1 >1          31-aug-15       30-sep-15       5   450      2 >1          31-aug-15       31-oct-15       35  450      3 >1          31-aug-15       30-nov-15       7   350      4 >1          31-aug-15       31-dec-15       37  350      5 >1          31-aug-15       31-jan-16       67  340      6 >2          31-aug-15       30-jun-14       3   800      1 >2          31-aug-15       31-jul-14       15  760      2 >2          31-aug-15       31-aug-14       45  750      3 >2          31-aug-15       30-sep-14       75  750      4 >2          31-aug-15       31-oct-14       0   630      5 >2          31-aug-15       30-nov-14       15  590      6 >2          31-aug-15       31-dec-14       45  580      7 

i think want row_number():

select t.*,        row_number() on (partition contractid order contractdate) seqnum t; 

this put incremental value, think describing.

if want count of rows each contract in each row, use:

select t.*,        count(*) on (partition contractid) cnt t; 

this put "6" in each row, if there 6 rows contract.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -