Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi there,

I have a problem to select data. The data are: _
ID     Date          Status
1      2008-01-01    A
1      2008-01-05    A
1      2008-01-11    A
1      2008-01-28    A
1      2008-02-02    A
1      2008-02-15    B
1      2008-02-26    A
1      2008-03-04    A
1      2008-03-17    B
1      2008-03-29    A
2      2008-01-01    A
2      2008-02-24    A
2      2008-03-26    B


The data should be shown in following _
ID    Status    Count      
1     A         1          
1     B         1
1     A         1          
1     B         1
1     A         1
2     A         1
2     B         1               


The status A should be count as 1 until the status is B. The flow is many A, once B.

I need a hand.
Thanks all.
Posted
Updated 29-Sep-11 18:42pm
v4
Comments
Mehdi Gholam 29-Sep-11 4:47am    
How do you want the grouping to be it is not apparent from your description, be more specific?
OriginalGriff 29-Sep-11 4:48am    
The problem is that it is not obvious what criteria to use to work out what data you are trying to select - Why is the count 1 for 01-01 to 02-02 when you have five entries in the data, for example.

Work out what you need, and then use the "Improve question" widget to edit your question and provide better information.
Corporal Agarn 29-Sep-11 12:18pm    
What have you tried?

1 solution

There's a lot of interpretation in this solution, but try this query and tell me what's missing or wrong.

Assumption 1: The ID column can actually be different and all id's should be treaded separately, if not, omit the o1.ID = o2.ID
Assumption 2: The count column is there just for telling us that there should be only one entry in the result set for the first date until the status changes
And the same goes for the --description column

SQL
WITH ordered AS(
    SELECT  ID,Date,Status,ROW_NUMBER() OVER(ORDER BY Date) as rn
    FROM    MyTable
    )
SELECT  o1.ID,o1.DATE
FROM    ordered o1,ordered o2
WHERE   o1.ID = o2.ID
    AND o1.rn = o2.rn -1
    AND o1.Status <> o2.Status
 
Share this answer
 
Comments
z4rk 4-Oct-11 5:35am    
But thanks you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900