Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Count() function? [STUMPED]

Status
Not open for further replies.

yeewaitu

Technical User
Nov 13, 2003
39
SG
Hi,

Got a SQL question for you experts out here. I would like to generate a count field based on the table below. Basically the count would reflect the number of records with the dates lesser than it and of the same ID.

Source Table
============

ID DATE
=== =======
123 01/01/05
123 01/01/04
123 01/01/03
124 01/01/05


Result Table
============

ID DATE Count
=== ======= ======
123 01/01/05 2
123 01/01/04 1
123 01/01/03 0
124 01/01/05 0

Any ideas anyone???? I am stumped.

 
This is row numbering partitioned by ID - trivial in SQL2005, requires temp table or self-join in SQL2000. Choose one :)

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
can you show me how to do this in SQL 2000?
 
Self-join then:
Code:
select A.ID, A.Date, count(B.Date)
from SourceTable A
left outer join SourceTable B on A.ID=B.ID and A.Date > B.Date
group by A.ID, A.Date
order by A.ID, A.Date desc
Note this will work well only if there are no duplicate dates per ID. Otherwise temp table must be used.

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
But what if there are duplicate dates per ID? How can you use temp table then?
 
Based on sample data I assumed there are no duplicate dates per ID. :p

Do you have 'em or not?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Yes. I wld have duplicate IDs... sadly. =(

Source Table
============

ID DATE
=== =======
123 01/01/05
123 01/01/05
123 01/01/04
123 01/01/03
124 01/01/05


Result Table
============

ID DATE Count
=== ======= ======
123 01/01/05 2
123 01/01/05 2
123 01/01/04 1
123 01/01/03 0
124 01/01/05 0
 
OK then... what is primary key in SourceTable?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
PID as shown below...

Source Table
============

PID ID DATE
=== === =======
1 123 01/01/05
2 123 01/01/05
3 123 01/01/04
4 123 01/01/03
5 124 01/01/05

Result Table
============

PID ID DATE Count
=== === ======= ======
1 123 01/01/05 2
2 123 01/01/05 2
3 123 01/01/04 1
4 123 01/01/03 0
5 124 01/01/05 0
 
Try this:
Code:
select A.ID, A.Date, A.PID, count(distinct B.Date)-1
from SourceTable A
left outer join SourceTable B on A.ID=B.ID and A.Date >= B.Date
group by A.ID, A.Date, A.PID
order by A.ID, A.Date desc

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top