0

The following is my query to go through about a million rows to calculate MTBUR (Mean Time Before Unscheduled Repair):

DECLARE @BeginDate date = '01-01-2013', 
        @EndDate date = '12-31-2013'
BEGIN
SELECT H.AutoType, 
COALESCE(((SUM(H.Hours))/(CASE WHEN R.ReceivedDate BETWEEN @BeginDate AND @EndDate THEN COUNT(R.Confirmed) END)), SUM(H.Hours)) AS 'MTBUR'
FROM Hours H
INNER JOIN Repair R
ON H.SN = R.SN 
WHERE (R.Confirmed NOT LIKE 'C%' AND R.Confirmed NOT LIKE 'O%')
AND (H.Date BETWEEN @BeginDate AND @EndDate) 
GROUP BY H.AutoType, 
R.ReceivedDate
END

The following are example results for 2 types:

Type | MTBUR
------------
a    | value
a    | value
a    | value
b    | value
b    | value
b    | value

I want my results to look like this:

Type | MTBUR
------------
a    | value
b    | value

Why is it grouping the same type several times. I want only 1 value for each type.

Also, Why is the DBMS making me also group by ReceivedDate? I get the feeling that is screwing my results up. Any suggestions?

The following are my CREATE TABLE:

CREATE TABLE [dbo].[acss_hours](
    [hoursId] [uniqueidentifier] NOT NULL,
    [name] [nvarchar](100) NULL,
    [Type] [nvarchar](100) NULL,
    [SN] [nvarchar](100) NULL,
    [Reg] [nvarchar](100) NULL,
    [Hours] [float] NULL,
    [Date] [datetime] NULL)

CREATE TABLE [dbo].[repair](
    [repairId] [uniqueidentifier] NOT NULL,
[Part] [nvarchar](100) NULL,
    [Customer] [nvarchar](100) NULL,
    [AutoType] [nvarchar](100) NULL,
    [ReceivedDate] [datetime] NULL,
    [Confirmed] [nvarchar](100) NULL,
    [Company] [nvarchar](100) NULL,
    [Reg] [nvarchar](100) NULL,
    [Manu] [nvarchar](100) NULL,
    [SN] [nvarchar](100) NULL)

4 Answers 4

2

You are correct, adding ReceivedDate is screwing up your results. You are getting one row for each type for RecievedDate.

SQL Server if forcing you to add RecievedDate to the group by because you are using it in the select clause. When SQL Server processes each AutoType, what ReceivedDate should it use? It has multiple ReceivedDates per AutoType. Either it needs to use each seperate ReceivedDate by adding it to the group by, or it can use a aggregate function like min or max to select one of the RecievedDates.

How do you want your query to handle it?

I think you should wrap your case in the COUNT.

COUNT(CASE WHEN R.ReceivedDate BETWEEN @BeginDate AND @EndDate 
THEN R.Confirmed ELSE 0 END)
Sign up to request clarification or add additional context in comments.

1 Comment

This was the answer I was looking for. I deduced that it was grouping my autotype by as many times there is a dateReceived. Thanks
1

You need to include R.ReceivedDate from your calculation in the group by because you're evaluating the column with the between statement. Its the same as including the column in the select. Basically any column in the select line that doesn't have an aggregation function needs to be in the group by.

2 Comments

How could I reword my script so do not have to add ReceivedDate in my Group By clause? I think that's what is causing me problems.
Try using an inner query... something like this:
0

You will have to make use of the keyword DISTINCT.

So effectively, you can query as such:

DECLARE @BeginDate date = '01-01-2013', 
        @EndDate date = '12-31-2013'
BEGIN
SELECT       DISTINCT H.AutoType, 
             COALESCE(((SUM(H.Hours))/(CASE WHEN R.ReceivedDate BETWEEN @BeginDate AND @EndDate THEN COUNT(R.Confirmed) END)), SUM(H.Hours)) AS 'MTBUR'

FROM         Hours H
INNER JOIN   Repair R ON H.SN = R.SN 
WHERE        (R.Confirmed NOT LIKE 'C%' AND R.Confirmed NOT LIKE 'O%')
AND          (H.Date BETWEEN @BeginDate AND @EndDate) 
GROUP BY     H.AutoType, R.ReceivedDate
END

Hope this helps!!!

3 Comments

I used DISTINCT previously, although it produces less results for the same type, it still produces multiple results for the same type. Would you recommend putting the case statement in the beginning of the query?
@JeffOrris I can't seem to understand the "it still produces multiple results for the same type" part. Could you please post the table structure and sample data for these two tables in order to make it easier for us to understand?
The results should be distinct car types with their own MTBUR calculation. Im still getting multiple results of the same car type with differenct MTBUR calcs..If you look at my first result example set from the question, it is still looking like that.
0

Didn't let me post this in the comment. Try an inner query:

SELECT H.AutoType, COALESCE(((SUM(H.Hours))/(SUM(x.CountConfirmed))), SUM(H.Hours)) AS 'MTBUR'
FROM 
    (SELECT H.AutoType, CASE WHEN R.ReceivedDate BETWEEN @BeginDate AND @EndDate THEN 1 ELSE 0 END AS CountConfirmed
    FROM Hours H
    INNER JOIN Repair R
    ON H.SN = R.SN 
    WHERE (R.Confirmed NOT LIKE 'C%' AND R.Confirmed NOT LIKE 'O%')
    AND (H.Date BETWEEN @BeginDate AND @EndDate)) x
JOIN Hours H
ON H.AutoType = x.AutoType
WHERE (H.Date BETWEEN @BeginDate AND @EndDate) 
GROUP BY H.AutoType

2 Comments

I like that idea. Was going to try that but didn't know if it would make a diff
I posted the CREATE TABLE scripts if it would be any more help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.