Results 1 to 9 of 9

Thread: [RESOLVED] sql server aggregate query

Threaded View

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Resolved [RESOLVED] sql server aggregate query

    Here are some rows from my table. You can see they are all for GroupID 57. I am interested in the StatusID = 5 and 6 records. I have to produce a report saying how many days the item spent in between status 5 and 6. So the first pair of 5/6 records (RowID 122 and 674) don't count because there's another newer pair. I want to build a table that has a row with three columns something like 57 - 11:02:17 - 11:02:59 (I left off the mm/dd/yy for the sake of brevity).

    RowId GroupID StatusID CreateDate
    120 57 1 2011-09-02 06:29:32.290
    121 57 3 2011-09-02 06:36:32.437
    122 57 5 2011-09-02 06:38:37.633
    674 57 6 2011-09-20 11:02:01.830
    675 57 5 2011-09-20 11:02:17.340
    676 57 6 2011-09-20 11:02:59.697



    However, it's possible there could be another 5 record even newer, such as this:
    RowId GroupID StatusID CreateDate
    120 57 1 2011-09-02 06:29:32.290
    121 57 3 2011-09-02 06:36:32.437
    122 57 5 2011-09-02 06:38:37.633
    674 57 6 2011-09-20 11:02:01.830
    675 57 5 2011-09-20 11:02:17.340
    676 57 6 2011-09-20 11:02:59.697
    677 57 5 2011-09-20 11:05:36.990

    I don't want any row at all in that case, because I can't calculate the time since it's still status 5.

    I started to write a query that looks like this, but you can see where I am having a problem:
    Code:
    	select 
    		egs.groupid,
    		MAX(egs.groupstatusid) 
    	from GroupStatus egs 
    	
    	inner join 
    	(
    		select 
    			groupid,
    			MAX(groupstatusid) as x
    		from GroupStatus egs 
    		where statusid=6 
    		group by Groupid
    	)EGS1
    	
    	on egs.GroupID = EGS1.GroupID
    	where egs.StatusID = 5
    	-- you need something in here so you get 675 and not 677
    	group by egs.GroupID
    What do I need to do here?
    Last edited by MMock; May 7th, 2013 at 11:38 AM.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width