fikir
Programmer
- Jun 25, 2007
- 86
Hello everyone,
I have one question,
we have a table called organizations and It has three columns (actually it has more than three column but therese are enough for the query
orgId visitStartdate visitEndDate
1 01/01/2007 01/05/2007
1 02/05/2007 02/15/2007
2 01/01/2007 01/10/2007
2 01/01/2007 01/12/2007
2 02/15/2007 02/28/2007
3 03/01/2007 03/11/2007
3 04/05/2007 04/10/2007
I am trying to get the visit squence for each orgnization
the output should look like
orgId visitStartdate visitEndDate visit
1 01/01/2007 01/05/2007 v1
1 02/05/2007 02/15/2007 v2
2 01/01/2007 01/10/2007 v1
2 01/01/2007 01/12/2007 v2
2 02/15/2007 02/28/2007 v3
3 03/01/2007 03/11/2007 v1
3 04/05/2007 04/10/2007 v2
I wrote this query
select orgId, visitStartdate, visitEndDate,
'V' + count(visitStartdate) as visit
from organizations org1 inner join organizations org2
on org1.orgId = org2.orgId
and org1.visitStartdate >= org2.visitStartdate
group by orgId, visitStartdate, visitEndDate
it works fine if the the different visits have different visitStartDate, but not quite working if it is the same
the above query's resultset looks like
orgId visitStartdate visitEndDate visit
1 01/01/2007 01/05/2007 v1
1 02/05/2007 02/15/2007 v2
2 01/01/2007 01/10/2007 v2 wrong
2 01/01/2007 01/12/2007 v2
2 02/15/2007 02/28/2007 v3
3 03/01/2007 03/11/2007 v1
3 04/05/2007 04/10/2007 v2
how do I correct my query so that those visits with the same start date, by looking at their visitEndDate, get the right sequence.
Thanks
I have one question,
we have a table called organizations and It has three columns (actually it has more than three column but therese are enough for the query
orgId visitStartdate visitEndDate
1 01/01/2007 01/05/2007
1 02/05/2007 02/15/2007
2 01/01/2007 01/10/2007
2 01/01/2007 01/12/2007
2 02/15/2007 02/28/2007
3 03/01/2007 03/11/2007
3 04/05/2007 04/10/2007
I am trying to get the visit squence for each orgnization
the output should look like
orgId visitStartdate visitEndDate visit
1 01/01/2007 01/05/2007 v1
1 02/05/2007 02/15/2007 v2
2 01/01/2007 01/10/2007 v1
2 01/01/2007 01/12/2007 v2
2 02/15/2007 02/28/2007 v3
3 03/01/2007 03/11/2007 v1
3 04/05/2007 04/10/2007 v2
I wrote this query
select orgId, visitStartdate, visitEndDate,
'V' + count(visitStartdate) as visit
from organizations org1 inner join organizations org2
on org1.orgId = org2.orgId
and org1.visitStartdate >= org2.visitStartdate
group by orgId, visitStartdate, visitEndDate
it works fine if the the different visits have different visitStartDate, but not quite working if it is the same
the above query's resultset looks like
orgId visitStartdate visitEndDate visit
1 01/01/2007 01/05/2007 v1
1 02/05/2007 02/15/2007 v2
2 01/01/2007 01/10/2007 v2 wrong
2 01/01/2007 01/12/2007 v2
2 02/15/2007 02/28/2007 v3
3 03/01/2007 03/11/2007 v1
3 04/05/2007 04/10/2007 v2
how do I correct my query so that those visits with the same start date, by looking at their visitEndDate, get the right sequence.
Thanks