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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need an incremental number

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-01-14 : 09:35:47
Hi
I 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, amount

I 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 this
Unique Date Userid IncrementalNumber
31231 12/12/09 Jim 1
12313 14/12/09 Jim 2
34555 15/12/09 Mary 1
65455 17/12/09 Jim 3
76556 17/12/09 Mary 2
55554 26/12/09 Jim 4
44344 03/01/10 Jim 1
98777 03/01/10 Jim 2
21123 04/01/10 Jim 3
86756 04/01/10 Mary 1
45454 16/01/10 Mary 2

hope 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]
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

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? )
Go to Top of Page

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
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-01-14 : 10:12:05
Thanks will give it a go

it's actually a datetime
Go to Top of Page

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 go

it'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)
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-01-14 : 10:17:49
Hi again

I 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 conthist
Where Ref like 'M%' and ondate > '20090101'

and it comes back as
Server: 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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 10:22:05
quote:
Originally posted by icw

Hi again

I 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 conthist
Where Ref like 'M%' and ondate > '20090101'

and it comes back as
Server: 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 comma
b/w ROW_NUMBER is not available in sql 2000
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 10:25:16
here's a sql 2000 solution


SELECT Unique,
Date,
Userid,
(SELECT COUNT(*) FROM table WHERE Userid=t.Userid AND MONTH(Date)=MONTH(t.Date) AND Unique < t.Unique) + 1 AS IncrementalNumber
FROM table t
Go to Top of Page

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 are
Unique = Recid
date = Ondate
User = Userid
Thetable is called Conthist

I 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 IncrementalNumber
FROM conthist t
Where srectype = 'S' and Ref like 'M%' and ondate > '20090101'


Thanks again if you can help
Go to Top of Page

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 are
Unique = Recid
date = Ondate
User = Userid
Thetable is called Conthist

I 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 IncrementalNumber
FROM conthist t
Where srectype = 'S' and Ref like 'M%' and ondate > '20090101'


Thanks again if you can help


you need to add where conditions inside as well
Go to Top of Page

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,
Userid
INTO #MyTemp
FROM conthist t
Where 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 IncrementalNumber
FROM #MyTemp AS T
Go to Top of Page

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,
Userid
INTO #MyTemp
FROM conthist t
Where 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 IncrementalNumber
FROM #MyTemp AS T


and adding to that you could create indexes on created #MyTemp table also
Go to Top of Page

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 now

thanks alot

have a great day!!
Go to Top of Page

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 there

Madhivanan

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

- Advertisement -