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


Looking for more efficient way to perform this simple query


Looking for more efficient way to perform this simple query

Author
Message
wizzlestick
wizzlestick
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
Hello -

As the subject states, I can't help but think there is a more efficient way to rewrite this query so that I don't have to perform the Counts twice, though my bag of SQL tricks is quite limited.

I am essentially trying to create a daily report which will grab the order counts for specific statuses for each user that has at least one order matching one of those statuses.

Any help is greatly appreciated!


-- Daily Report
SELECT u.UserID,
(SELECT COUNT(OrderID) FROM Orders WHERE OrderStatusID = 2) As Status2Count,
(SELECT COUNT(OrderID) FROM Orders WHERE OrderStatusID = 3) As Status3Count,
(SELECT COUNT(OrderID) FROM Orders WHERE OrderStatusID = 4) As Status4Count,
(SELECT COUNT(OrderID) FROM Orders WHERE OrderStatusID = 8) As Status8Count
FROM Users u
JOIN Orders o ON u.UserID = o.userID
WHERE (
((SELECT COUNT(*) FROM Orders WHERE OrderStatusID = 2) > 0) OR
((SELECT COUNT(*) FROM Orders WHERE OrderStatusID = 3) > 0) OR
((SELECT COUNT(*) FROM Orders WHERE OrderStatusID = 4) > 0) OR
((SELECT COUNT(*) FROM Orders WHERE OrderStatusID = 8) > 0)
)
GROUP BY u.UserID


Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8592 Visits: 18175
There's an easier way and it's explained in this article: http://www.sqlservercentral.com/articles/T-SQL/63681/

This is an example that might not work because I have nothing to test on.

-- Daily Report
SELECT UserID,
SUM(CASE WHEN OrderStatusID = 2 THEN OrderCount END) As Status2Count,
SUM(CASE WHEN OrderStatusID = 3 THEN OrderCount END) As Status2Count,
SUM(CASE WHEN OrderStatusID = 4 THEN OrderCount END) As Status2Count,
SUM(CASE WHEN OrderStatusID = 8 THEN OrderCount END) As Status2Count,
FROM(
SELECT u.UserID,
o.OrderStatusID,
COUNT(*) OrderCount
FROM Users u
JOIN Orders o ON u.UserID = o.userID
WHERE OrderStatusID IN( 2, 3, 4, 8)
GROUP BY u.UserID, OrderStatusID
HAVING COUNT(*) > 0)x --This is a pre-aggregation to improve performance, but should be tested.
GROUP BY UserID



EDIT: I was evaluating wy word choice and easier might not be accurate when there's no practice on how to do it, but it's certainly more efficient and will come natural when you fully understand it.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
wizzlestick
wizzlestick
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
Thank you so much Luis!

This worked perfectly, makes more sense, and I appreciate the link. :-D
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8592 Visits: 18175
You're welcome. I hope you understand how it works. If you don't, please post any questions that you have.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
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