C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

Count the Number of Actual rejections


Posted Date: 30 Sep 2008      Posted By: Francis jude      Member Level: Bronze     Points: 1   Responses: 2



hai
i have a Scenario
in which the table contains the fields Id, State and time as shown below..
i need the count of times that a particular id has been rejected

Id State Time
1 Submitted 12:03
1 Submitted 12:05
1 Submitted 12:08
1 Submitted 12:10
------------------
1 Rejected 12:15
------------------
1 Rejected 12:18
1 Rejected 12:19
1 Submitted 12:20
1 Submitted 12:21
------------------
1 Rejected 12:25
------------------
1 Rejected 12:26
1 Submitted 12:35
1 Submitted 12:36
1 Submitted 12:45
------------------
1 Rejected 12:46
------------------
1 Submitted 12:55

In this case i need the output as 3..
that is the rejections after every immediate submission
that is which i have shown whithin the lines
Can u find me a solution?





Responses

Author: Lakhan Pal    01 Oct 2008Member Level: DiamondRating: 1 out of 5     Points: 1

Hi-

Use This query:

SELECT id,COUNT(id) FROM <TableName> WHERE
State='Rejected ' GROUP BY id

Thanks
Lakhan Pal Garg

Please rate this answer if it helped you.
Thanks & Regards
Lakhan Pal Garg
Free Code Snippets



Author: Sajikumar    01 Oct 2008Member Level: SilverRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

try this

declare @Rejected table(IdVal int identity(1,1),Id int,times datetime,RowNum int)
declare @Count int,@Loop int,@NextVal int
declare @Current datetime,@Next datetime
insert @Rejected(Id,times)
select Id,times from Table1 where State = 'Rejected'
select @Count = count(*) from @Rejected
select @Loop = 1

while (@Loop < @Count)
begin
select top 1 @NextVal = isnull(IdVal,0) from @Rejected where IdVal > @Loop order by IdVal
if (isnull(@NextVal,0) > 0)
begin
select @Current = times from @Rejected where IdVal = @Loop
select @Next = times from @Rejected where IdVal = @NextVal
if not exists(select 1 from Table1 where State = 'Submitted' and times between @Current and @Next )
begin
delete @Rejected where IdVal = @NextVal
if (@@rowcount = 0) break
end
else
select @Loop = @NextVal
end
else
break

end
select count(*) from @Rejected




Thanks
Saji



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : SQL server backup files
Previous : creating Table
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use