Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-01-14 : 09:35:47
|
HiI don't know how to do this.I have a sales table.The table has the following columns (this is simplified of course)Date (DD/MM/YY), Userid, UniqueSaleid, Product, amountI want to do a query that will add an incremental number column. but the number has to reset each month for each user.E.g. the results might look like thisUnique Date Userid IncrementalNumber 31231 12/12/09 Jim 1 12313 14/12/09 Jim 234555 15/12/09 Mary 165455 17/12/09 Jim 376556 17/12/09 Mary 255554 26/12/09 Jim 444344 03/01/10 Jim 198777 03/01/10 Jim 221123 04/01/10 Jim 386756 04/01/10 Mary 145454 16/01/10 Mary 2hope that makes sense |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 09:38:28
|
[code]SELECT ROW_NUMBER() OVER (PARTITION BY Userid,DATEADD(mm,DATEDIFF(mm,0,Date),0) ORDER BY Date) AS Seq,*FROM Table[/code] |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 09:57:16
|
"Date (DD/MM/YY)"Hopefully that column is actually of DATETIME datatype, rather than a date being stored as a text string in dd/mm/yy format?If so it won't have any inherent format, such as the dd/mm/yy you mention, but it will be capable of being formatted in any reasonable date-style format that you like If its actually a text string date then using it "as a date" will turn into nightmare over time |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:01:41
|
quote: Originally posted by Kristen "Date (DD/MM/YY)"Hopefully that column is actually of DATETIME datatype, rather than a date being stored as a text string in dd/mm/yy format?If so it won't have any inherent format, such as the dd/mm/yy you mention, but it will be capable of being formatted in any reasonable date-style format that you like If its actually a text string date then using it "as a date" will turn into nightmare over time 
I'm still wondering while people always tend to worry so much on formats of dates and use character fields to store it. I've seen numerous instances of this myself when i do reviews |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 10:06:24
|
From an Application Programming background, rather than Relational Data background, perhaps?They were the ones who created the Y2K issue (or are you too young to remember that? ) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:08:04
|
quote: Originally posted by Kristen From an Application Programming background, rather than Relational Data background, perhaps?They were the ones who created the Y2K issue (or are you too young to remember that? )
very true |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-01-14 : 10:12:05
|
Thanks will give it a goit's actually a datetime |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:14:24
|
quote: Originally posted by icw Thanks will give it a goit's actually a datetime
Ok great then it wont be stored in dd/mm/yy format in table. the default format in table is yyyy-mm-dd hh:mi:ss. Though you could use formatting functions to get it in format you want at front end app or using CONVERT in T-sql (which i dont recommend) |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-01-14 : 10:17:49
|
Hi againI have just tried it (query below)SELECT recid, ondate, userid ,USerROW_NUMBER() OVER (PARTITION BY Userid,DATEADD(mm,DATEDIFF(mm,0,Date),0) ORDER BY Date) AS Seq,*From conthistWhere Ref like 'M%' and ondate > '20090101'and it comes back asServer: Msg 195, Level 15, State 10, Line 1'USerROW_NUMBER' is not a recognized function name.I'm sorry I think i have posted into the wrong forum as I have realised this is SQL 2005 and I querying SQL 2000.Is that the problem?Thanks a lot |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:22:05
|
quote: Originally posted by icw Hi againI have just tried it (query below)SELECT recid, ondate, userid ,USer,ROW_NUMBER() OVER (PARTITION BY Userid,DATEADD(mm,DATEDIFF(mm,0,Date),0) ORDER BY Date) AS Seq,*From conthistWhere Ref like 'M%' and ondate > '20090101'and it comes back asServer: Msg 195, Level 15, State 10, Line 1'USerROW_NUMBER' is not a recognized function name.I'm sorry I think i have posted into the wrong forum as I have realised this is SQL 2005 and I querying SQL 2000.Is that the problem?Thanks a lot
you missed a commab/w ROW_NUMBER is not available in sql 2000 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:25:16
|
here's a sql 2000 solutionSELECT Unique, Date, Userid,(SELECT COUNT(*) FROM table WHERE Userid=t.Userid AND MONTH(Date)=MONTH(t.Date) AND Unique < t.Unique) + 1 AS IncrementalNumberFROM table t |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-01-14 : 10:44:38
|
Hi Visakh16 I really appreciate your help thanks.i pasted the query and edited it and tried to run it but it crashed the SQL server.Have I edited it incorrectly (see below)My fields areUnique = Reciddate = OndateUser = UseridThetable is called ConthistI have done a simple query found that there it should only be bringing back 389 rows so there is not a huge amount of data.SELECT Recid, ondate, Userid,(SELECT COUNT(*) FROM conthist WHERE Userid=t.Userid AND MONTH(onDate)=MONTH(t.onDate) AND recid < t.recid) + 1 AS IncrementalNumberFROM conthist tWhere srectype = 'S' and Ref like 'M%' and ondate > '20090101'Thanks again if you can help |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:48:08
|
quote: Originally posted by icw Hi Visakh16 I really appreciate your help thanks.i pasted the query and edited it and tried to run it but it crashed the SQL server.Have I edited it incorrectly (see below)My fields areUnique = Reciddate = OndateUser = UseridThetable is called ConthistI have done a simple query found that there it should only be bringing back 389 rows so there is not a huge amount of data.SELECT Recid, ondate, Userid,(SELECT COUNT(*) FROM conthist WHERE Userid=t.Userid AND MONTH(onDate)=MONTH(t.onDate) AND recid < t.recid and srectype = 'S' and Ref like 'M%' and ondate > '20090101') + 1 AS IncrementalNumberFROM conthist tWhere srectype = 'S' and Ref like 'M%' and ondate > '20090101'Thanks again if you can help
you need to add where conditions inside as well |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-01-14 : 10:52:14
|
"it should only be bringing back 389 "If there are a lot of rows in [Conthist] and not well indexed for this particular query, it could be slow - makign multiple table scans.You could try getting the data into a temporary table first, and going from there. (You ought to pre-create a @TableVar and use that, rather than my #TempTable approach, which will potentially block TEMPDB)SELECT Recid,ondate,UseridINTO #MyTempFROM conthist tWhere srectype = 'S' and Ref like 'M%' and ondate > '20090101'SELECT Recid,ondate,Userid,(SELECT COUNT(*) FROM #MyTemp WHERE Userid=t.Userid AND MONTH(onDate)=MONTH(t.onDate) AND recid < t.recid) + 1 AS IncrementalNumberFROM #MyTemp AS T |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:54:15
|
quote: Originally posted by Kristen "it should only be bringing back 389 "If there are a lot of rows in [Conthist] and not well indexed for this particular query, it could be slow - makign multiple table scans.You could try getting the data into a temporary table first, and going from there. (You ought to pre-create a @TableVar and use that, rather than my #TempTable approach, which will potentially block TEMPDB)SELECT Recid,ondate,UseridINTO #MyTempFROM conthist tWhere srectype = 'S' and Ref like 'M%' and ondate > '20090101'SELECT Recid,ondate,Userid,(SELECT COUNT(*) FROM #MyTemp WHERE Userid=t.Userid AND MONTH(onDate)=MONTH(t.onDate) AND recid < t.recid) + 1 AS IncrementalNumberFROM #MyTemp AS T
and adding to that you could create indexes on created #MyTemp table also |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-01-14 : 10:58:26
|
Thanks so much , I added the condition inside too and it works nowthanks alothave a great day!! |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-01-15 : 01:47:45
|
Where do you want to show data with incremental numbers?If you use front end application, do numbering thereMadhivananFailing to plan is Planning to fail |
 |
|
|