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
 SQL query - need help to refine results

Author  Topic 

Harmonygirl
Starting Member

30 Posts

Posted - 2010-07-07 : 14:42:35
Hi everyone,

I had posted requests for help on a SQL query in another thread:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=146536

I've tried another approach, and so I was hoping that this approach would better show the joins that seemed to be the lingering question in the old thread.

I'll begin by (re)including my report / requirements and then the code - and you can let me know if you need any more information.

Thanks!

Report requirements:

* Create a query / chart to plot the following:

All defects, grouped by Fridays, that meet these criteria:
Test Phase = "System Testing" or "Production"
Status = "New" or "Open"
Note on statuses - these are all the available statuses in our project:
New
Open
Fixed
Deferred
Reject
Retest
Closed

Query works but does not properly calculate the 'Open' number of defects - the numbers are way too high, I suspect they are pulling in one of the other status categories, but not sure how to verify:

SQL code:


-- *******************************************************
-- Step 3 Summarize by Date
Select distinct date_friday, sum(new_that_week) as "Total New", sum(new_production) as "New Production", sum(was_open) as "Open Defects", sum(new_testing) as "New Testing", sum(was_closed) as defects_closed, sum(defect_existing) as total_defects
from (
-- Step 2 Check if Defect was Open / New / Closed that week.
select date_friday, day_week, date_last_sat , bg_bug_id, bg_detection_date, bg_closing_date, test_phase, defect_existing, bg_status, open_date,
(case when bg_detection_date >= date_last_sat and bg_detection_date <= date_friday then 1 else 0 end) as new_that_week,
(case when bg_detection_date >= date_last_sat and bg_detection_date <= date_friday and test_phase = 'Pilot Support' then 1 else 0 end)as new_production,
(case when bg_detection_date >= date_last_sat and bg_detection_date <= date_friday and test_phase = 'System Test' then 1 else 0 end)as new_testing,
(case when bg_closing_date < DATEADD(day,1,date_friday) then 1 else 0 end)as was_closed,
(case when bg_status = 'Open' or(open_date < DATEADD(day,1,date_friday) and bg_closing_date >= DATEADD(day,1,date_friday)) then 1 else 0 end) as was_open
from (
--Step 1 get all defects existing during week range
select date_friday, day_week, date_last_sat , bg_bug_id, bg_detection_date, bg_closing_date , bg_user_05 as test_phase, 1 as defect_existing, bg_status,open_date
from (
-- Get all Fridays an calculate week range
select distinct d_friday as date_friday, DATENAME(weekday,d_friday) AS day_week,
DATEADD(day,-6,d_friday) as date_last_sat
from (
-- get all dates
SELECT au_time, DATEADD(dd,4,DATEADD(wk, DATEDIFF(wk,0,au_time), 0)) AS d_friday
FROM audit_log
where DATEPART(YEAR, au_time) = '2010'
-- end get all dates
) results_1
-- end get all Fridays
) results_2
, (select bg_bug_id, open_date, bg_detection_date, bg_closing_date , bg_user_05, bg_status
From bug left join (
select distinct au_entity_id, max(au_time) as open_date
from (
SELECT au_entity_id, au_action_id ,
ap_old_value, ap_new_value , au_time
FROM Audit_properties, audit_log
where au_action_id = ap_action_id
and ap_field_name = 'BG_STATUS' and
ap_new_value = 'Open'
) aa
group by au_entity_id
) date
on bg_bug_id = au_entity_id ) composed_bug
Where bg_detection_date <= results_2.date_friday
-- end step 1
) results_3
-- end step 2
) results_4 group by date_friday
-- end step 3
-- **********************************************

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-07-07 : 15:54:00
your code is hard to read and therefore it's hard to understand what your problem is.

for the purpose of this post, please try to extract the problem with some sample data.
Go to Top of Page

Harmonygirl
Starting Member

30 Posts

Posted - 2010-07-07 : 16:09:20
Sure, I can do that. I don't know how to insert a table into my post, so please excuse my formatting as I will try to do this by hand.

Here is the output I am trying to get:

date_friday------Total_New------New_Production------Open_Defects------New_Testing
6/11/2010--------69--------------51------------------189------------------18
6/18/2010--------47--------------24------------------163------------------23
6/25/2010--------70--------------54------------------145------------------16
7/2/2010---------64--------------45------------------132------------------19




quote:
Originally posted by slimt_slimt

your code is hard to read and therefore it's hard to understand what your problem is.

for the purpose of this post, please try to extract the problem with some sample data.

Go to Top of Page
   

- Advertisement -