Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GROUP BY DATE w. Count

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
I have a query that gets the date of when a "Defect" Was opened and if it was closed it grabs that date as well.

Code:
SELECT BG_BUG_ID as 'Defect ID', 
	BG_STATUS as 'Status', 
	convert(varchar,BG_DETECTION_DATE,101) as 'Start Date',
	convert(varchar,max(ENDDATE.ENDTIME),101) as 'End Time'
From Bug WITH (NOLOCK)

LEFT OUTER JOIN
(SELECT AU_ENTITY_ID,max(AU_TIME) as 'EndTime'
		FROM BUG,AUDIT_LOG, AUDIT_PROPERTIES WITH (NOLOCK)
		WHERE AU_ENTITY_TYPE = 'BUG'
		AND AP_FIELD_NAME = 'BG_STATUS'
		AND AU_ACTION_ID = AP_ACTION_ID
		AND (AP_NEW_VALUE = 'Deferred' or AP_NEW_VALUE = 'Closed')
		AND BG_DETECTED_IN_RCYC = 1118
		GROUP BY AU_ENTITY_ID
		)as ENDDATE
		ON ENDDATE.AU_ENTITY_ID = BUG.BG_BUG_ID

WHERE BG_DETECTED_IN_RCYC = 1118
AND BG_STATUS <> 'Invalid'
AND BG_STATUS <> 'New'
GROUP BY BG_BUG_ID, BG_STATUS, BG_DETECTION_DATE

This date returns like this:
Defect Status Start Date End Time
16018 Closed 10/05/2010 10/07/2010
16021 Closed 10/05/2010 10/06/2010
16023 Closed 10/06/2010 10/07/2010

What I want is it to display like this:
Defect Status DATE START_COUNT END_COUNT TOTAL
16018 Closed 10/05/2010 2 NULL 2
16021 Closed 10/06/2010 1 1 0
16023 Closed 10/07/2010 2 2 0

The Start count is the number of times that date appears in the start column and the same logic for the end_count. Which could return null or 0 it doesn't matter.

The total column is just Start - End. This helps me create a running total on the client side. I am not currently against doing a running total on the query side, I just know how to do it on the server side given a total.

Any ideas on how to convert my current date to display like that?


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Why does 10/07/2010 have a START_COUNT of 2? Shouldn' it be 0?

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
Yes my bad. I messed up you are correct.

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top