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
 General SQL Server Forums
 New to SQL Server Programming
 How to get certain rows count?

Author  Topic 

Ally
Starting Member

10 Posts

Posted - 2011-05-16 : 13:55:11
Hello
How can I get the rows count if Recommendation_code = "REC_REV" after the firs REC_REV record?
Bu i need to count only 1 time if they comes each other. I should get count of 6 from below table.

Thanks for any help

credit_application_id app_date Type Recommendation_code login
222147 2011-01-19 18:03:24.000 S REC_REV admin
222147 2011-01-19 18:03:24.000 D DECL willis4
222147 2011-02-07 15:23:44.000 S REC_REV admin
222147 2011-02-08 17:32:10.000 S REC_REV admin
222147 2011-02-08 17:32:10.000 D REC_DECL willis4
222147 2011-02-08 17:32:10.000 D DECL yorkb
222147 2011-03-05 11:16:56.000 S REC_REV admin
222147 2011-03-08 10:17:59.000 S REC_REV admin
222147 2011-03-08 10:17:59.000 S REC_REV admin
222147 2011-03-08 10:17:59.000 D REC_DECL levanm4
222147 2011-03-08 10:17:59.000 D REC_DECL levanm4
222147 2011-03-08 10:17:59.000 D DECL chowd6
222147 2011-03-08 10:17:59.000 D DECL chowd6
222147 2011-03-10 09:34:51.000 S REC_REV admin
222147 2011-03-10 09:34:51.000 D DECL simonj4
222147 2011-03-10 17:08:31.000 S REC_REV admin
222147 2011-03-11 15:26:11.000 S REC_REV admin
222147 2011-03-11 15:26:11.000 D DECL simonj4
222147 2011-03-22 13:30:00.000 S REC_REV admin
222147 2011-03-22 13:30:00.000 D REC_DECL simonj4
222147 2011-03-22 13:30:00.000 D APPR yorkb
222147 2011-03-22 13:30:00.000 D DECL admin
222147 2011-03-29 11:55:37.000 S REC_REV admin
222147 2011-03-29 11:55:37.000 D REC_APP mawany
222147 2011-03-29 11:55:37.000 D APPR Yorkb

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-05-16 : 14:03:35
You need something to order by and there doesn't seem to be anything here you have three rows with date 20110305 17:32:10 - what order are they in?


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Ally
Starting Member

10 Posts

Posted - 2011-05-16 : 14:17:13
quote:
Originally posted by nigelrivett

You need something to order by and there doesn't seem to be anything here you have three rows with date 20110305 17:32:10 - what order are they in?


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



It is ordered by App_date
it doesn't look good but there are 5 rows
Here are they (, separated now)

I dont know how to use cursor :(
credit_application_id,app_date,Type,Recommendation_code,login
222147,2011-01-19 18:03:24.000,S,REC_REV,admin
222147,2011-01-19 18:03:24.000,D,DECL,willis4
222147,2011-02-07 15:23:44.000,S,REC_REV,admin
222147,2011-02-08 17:32:10.000,S,REC_REV,admin
222147,2011-02-08 17:32:10.000,D,REC_DECL,willis4
222147,2011-02-08 17:32:10.000,D,DECL, yorkb
222147,2011-03-05 11:16:56.000,S,REC_REV,admin
222147,2011-03-08 10:17:59.000,S,REC_REV,admin
222147,2011-03-08 10:17:59.000,S,REC_REV,admin
222147,2011-03-08 10:17:59.000,D,REC_DECL,levanm4
222147,2011-03-08 10:17:59.000,D,REC_DECL,levanm4
222147,2011-03-08 10:17:59.000,D,DECL, chowd6
222147,2011-03-08 10:17:59.000,D,DECL, chowd6
222147,2011-03-10 09:34:51.000,S,REC_REV,admin
222147,2011-03-10 09:34:51.000,D,DECL, simonj4
222147,2011-03-10 17:08:31.000,S,REC_REV,admin
222147,2011-03-11 15:26:11.000,S,REC_REV,admin
222147,2011-03-11 15:26:11.000,D,DECL, simonj4
222147,2011-03-22 13:30:00.000,S,REC_REV,admin
222147,2011-03-22 13:30:00.000,D,REC_DECL,simonj4
222147,2011-03-22 13:30:00.000,D,APPR, yorkb
222147,2011-03-22 13:30:00.000,D,DECL, admin
222147,2011-03-29 11:55:37.000,S,REC_REV,admin
222147,2011-03-29 11:55:37.000,D,REC_APP,mawany
222147,2011-03-29 11:55:37.000,D,APPR, Yorkb

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-05-16 : 14:20:58
222147,2011-02-08 17:32:10.000,S,REC_REV,admin
222147,2011-02-08 17:32:10.000,D,REC_DECL,willis4
222147,2011-02-08 17:32:10.000,D,DECL, yorkb

These still look like they have the same app date.

If you sort that out it could be something like

with cte as
(
select credit_application_id, Recommendation_code, seq = row_number() over (partition by credit_application_id order by app_date)
)
select credit_application_id, count(*)
from cte c
where Recommendation_code = 'REC_REV'
and not exists (select * from cte c2 where c.credit_application_id = c2.credit_application_id and c2.seq = c.seq - 1 and c2.Recommendation_code = 'REC_REV')
group by credit_application_id


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Ally
Starting Member

10 Posts

Posted - 2011-05-16 : 14:32:05
quote:
Originally posted by nigelrivett

222147,2011-02-08 17:32:10.000,S,REC_REV,admin
222147,2011-02-08 17:32:10.000,D,REC_DECL,willis4
222147,2011-02-08 17:32:10.000,D,DECL, yorkb

These still look like they have the same app date.

If you sort that out it could be something like

with cte as
(
select credit_application_id, Recommendation_code, seq = row_number() over (partition by credit_application_id order by app_date)
)
select credit_application_id, count(*)
from cte c
where Recommendation_code = 'REC_REV'
and not exists (select * from cte c2 where c.credit_application_id = c2.credit_application_id and c2.seq = c.seq - 1 and c2.Recommendation_code = 'REC_REV')
group by credit_application_id


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.





Sorry I am new with sql I didnt get the first part.
with cte as
(
select credit_application_id, Recommendation_code, seq = row_number() over (partition by credit_application_id order by app_date)
)

Can you explain it please?

Thanks
Ally
Go to Top of Page
   

- Advertisement -