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
 GroupBy question

Author  Topic 

mickleyd
Starting Member

5 Posts

Posted - 2010-11-15 : 16:26:36
I have a table with records submited by a user weekly. I have the ProjectNumber, Date, and Status (in addition to other data) that is returned grouped by the ProjectNumber. The values for the status are Active and Closed.

I have a query that will display the last 4 weeks worth of records grouped by ProjectNumber. I want to remove the entire group (all records for that ProjectNumber) from my report if the status of the just 1 of the previous 3 weeks is Closed.

Basically I want to see that a project number is closed in the current week, but after that I don't want any records for that project number to show up on my reports at all.

Thanks
Dwight

jessiefun
Starting Member

35 Posts

Posted - 2010-11-15 : 21:52:13
Hi Mick,

You'd better provide your table's schema and the resuts that you expect, so others can understand clearly.

Regards, Jessie
Go to Top of Page

mickleyd
Starting Member

5 Posts

Posted - 2010-11-16 : 13:51:25
Okay I have a table like this:

ID, ProjectNumber, RecordDate, Status, Comment
1, Z001234, 10/18/2010, Active, Project is on time and under budget
2, Z001234, 10/25/2010, Active, Project is on time and under budget
3, Z009876, 10/25/2010, Active, Project is on time and under budget
4, Z001234, 11/1/2010, Active, Project is on time and under budget
5, Z009876, 11/1/2010, Active, Project is on time and under budget
6, Z001234, 11/8/2010, Closed, Completed under budget
7, Z009876, 11/8/2010, Active, We are a little over Budget
8, Z009876, 11/15/2010, Closed, Completed on time but over budget
9, Z001010, 11/15/2010, Active, On time and budget


If I run the report the week of the 15th want the report to look like this (not displaying any records from Z001234 because its status was closed last week):
Z009876
10/25/2010, Active, Project is on time and under budget
11/01/2010, Active, Project is on time and under budget
11/08/2010, Active, We are a little over Budget
11/15/2010, Closed, Completed on time but over budget
Z001010
11/15/2010, Active, On time and budget

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-16 : 13:55:04
WHERE Status <> 'Closed'

???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

mickleyd
Starting Member

5 Posts

Posted - 2010-11-16 : 14:12:59
Brett,
Sorry but that is not what I want. There are multiple records for Project Number Z001234, Where status is not equal to Closed will still return records for Z001234 when the status was active.

I want to hide all records for Z001234 because the status was marked as closed last week, and still show all records for Z009876 because the status was marked as closed this week.

I run the report each week. I do not want to see any records for projects closed last week, but do want to see records for active projects and projects that were closed this week.

Sorry I must not be explaining this very well.

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-16 : 14:28:31
WHERE ProjectNumber NOT IN (SELECT ProjectNumber FROM Table WHERE Status <> 'Closed')


????

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

mickleyd
Starting Member

5 Posts

Posted - 2010-11-17 : 16:41:51
Thanks the query in the query is what I needed. I am not there just yet, but I think I can figure out the rest.

Thanks again for your help!
Dwight
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-17 : 17:43:05
it's called a sub-query

and you're welcome...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

mickleyd
Starting Member

5 Posts

Posted - 2010-11-18 : 15:42:26
Just in case someone else was looking to do something similar here is my query.

SELECT * FROM TableName
WHERE ([Project Number] NOT IN (SELECT [Project Number] FROM TableName WHERE(([Project Status] = 'Closed') AND (TimePeriodEnding
Between DateAdd("m",-4,DateSerial(Year(Now()),Month(Now()),1))
And DateAdd("m",-1,DateSerial(Year(Now()),Month(Now()),1)))))) AND (TimePeriodEnding Between DateAdd("m",-4,DateSerial(Year(Now()),Month(Now()),1)) And DateAdd("d",-1*Day(Date()),Date())) ORDER BY [Project Number], TimePeriodEnding;
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-18 : 19:07:28
DateSerial?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -