Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SubSelect Statement not returning accurate results


SubSelect Statement not returning accurate results

Author
Message
iklektic
iklektic
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 161
Hi everyone! I am working on a report where I want to get the total orders entered per branch per day and per month. The query I have right now is:

SELECT        OwnerStaff_Branch, COUNT(WorkOrderID) AS MonthTotal,
(SELECT COUNT(WorkOrderID) AS Expr1
FROM WorkOrderReportView
WHERE (dbo.DateOnly(CreatedOn) = dbo.DateOnly(GETDATE()))
) AS DailyTotal
FROM WorkOrderReportView
WHERE (MONTH(CreatedOn) = MONTH(GETDATE())) AND (YEAR(CreatedOn) = YEAR(GETDATE()))
GROUP BY OwnerStaff_Branch




The hangup here is that the results for the sub-select is the same total in all branches... which isn't the case. How do I make this code return the unique total per branch?
As always, you guys rock and your help is greatly appreciated!

Here's what the report looks like:

Branch MonthlyTotal DailyTotal
Branch1 113 37
Branch2 7 37
Branch3 23 37
Branch4 527 37
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9017 Visits: 19028
You forgot to correlate your correlated subquery:

SELECT OwnerStaff_Branch, 
COUNT(WorkOrderID) AS MonthTotal,
(SELECT COUNT(WorkOrderID) AS Expr1
FROM WorkOrderReportView
WHERE (dbo.DateOnly(CreatedOn) = dbo.DateOnly(GETDATE())
AND OwnerStaff_Branch = w.OwnerStaff_Branch)
) AS DailyTotal
FROM WorkOrderReportView w
WHERE (MONTH(CreatedOn) = MONTH(GETDATE())) AND (YEAR(CreatedOn) = YEAR(GETDATE()))
GROUP BY OwnerStaff_Branch



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
iklektic
iklektic
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 161
Thanks! I couldn't for the life of me figure out how to do that... I knew I was missing some kind of tie in.
You're the best, thanks again.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9017 Visits: 19028
No worries.

Consider making your WHERE clauses SARGable:

DECLARE @Startmonth DATE, @Endmonth DATE, @Startday DATE, @Endday DATE
SET @Startmonth = DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)
SET @Endmonth = DATEADD(MONTH,1,@Startmonth)
SET @Startday = DATEADD(day,DATEDIFF(day,0,GETDATE()),0)
SET @Endday = DATEADD(day,1,@Startday)

SELECT @Startmonth, @Endmonth, @Startday, @Endday


SELECT OwnerStaff_Branch,
COUNT(WorkOrderID) AS MonthTotal,
(SELECT COUNT(WorkOrderID) AS Expr1
FROM WorkOrderReportView
WHERE CreatedOn >= @Startday AND CreatedOn < @Endday
AND OwnerStaff_Branch = w.OwnerStaff_Branch)
) AS DailyTotal
FROM WorkOrderReportView w
WHERE CreatedOn >= @Startmonth AND CreatedOn < @Endmonth
GROUP BY OwnerStaff_Branch



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search