Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

COUNT and compare Expand / Collapse
Author
Message
Posted Tuesday, December 18, 2012 3:45 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:40 AM
Points: 5, Visits: 6
For some reason I've always gotten confuses when using COUNT!

I have two tables, one for Orders and then an Assiged Parcels table which can have multiple records attached to an Order. In the Order table is column Pieces, which must match the count for Assigned Parcels. I also need to make the Orders table 'clickable' in the results.

Here is my code. What am I doing wrong?

SELECT PA.OrderID, COUNT(PA.OrderID) AS 'TotalParcels'
FROM tblParcelAssigned as PA
INNER JOIN tblOrder AS O ON O.OrderID = PA.OrderID
WHERE PA.Type = 2
AND O.Pieces <> TotalParcels
GROUP BY PA.OrderID
ORDER BY PA.OrderID DESC

Thanks from Newbie Dan!
Post #1398081
Posted Tuesday, December 18, 2012 4:15 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:55 AM
Points: 6,142, Visits: 7,195
support 86837 (12/18/2012)

I have two tables, one for Orders and then an Assiged Parcels table which can have multiple records attached to an Order. In the Order table is column Pieces, which must match the count for Assigned Parcels.
Here is my code. What am I doing wrong?


It's an Order of Evaluation issue. You use the WHERE clause to filter what goes into the aggregation, then you use the HAVING clause to filter the results of the aggregation, like so:

SELECT 
PA.OrderID,
COUNT(*) AS 'TotalParcels'
FROM
tblParcelAssigned as PA
INNER JOIN
tblOrder AS O
ON O.OrderID = PA.OrderID
WHERE
PA.Type = 2
HAVING
o.Pieces <> Count(*)
GROUP BY
PA.OrderID
ORDER BY
PA.OrderID
DESC

I also need to make the Orders table 'clickable' in the results.


I'm not entirely sure what you mean by this.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1398090
Posted Tuesday, December 18, 2012 4:28 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:40 AM
Points: 5, Visits: 6
Your query works only if I omit the HAVING clause.
Post #1398099
Posted Tuesday, December 18, 2012 4:56 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:55 AM
Points: 6,142, Visits: 7,195
support 86837 (12/18/2012)
Your query works only if I omit the HAVING clause.


What error are you getting? You're getting untested code as I don't have a sample set to work from for your particular issue. If you check the first link in my signature, you'll see how we usually ask for code assistance requests like this to be setup so we can get you tested code.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1398106
Posted Tuesday, December 18, 2012 9:13 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:01 PM
Points: 3,609, Visits: 5,222
Evil Kraig F (12/18/2012)
support 86837 (12/18/2012)

I have two tables, one for Orders and then an Assiged Parcels table which can have multiple records attached to an Order. In the Order table is column Pieces, which must match the count for Assigned Parcels.
Here is my code. What am I doing wrong?


It's an Order of Evaluation issue. You use the WHERE clause to filter what goes into the aggregation, then you use the HAVING clause to filter the results of the aggregation, like so:

SELECT 
PA.OrderID,
COUNT(*) AS 'TotalParcels'
FROM
tblParcelAssigned as PA
INNER JOIN
tblOrder AS O
ON O.OrderID = PA.OrderID
WHERE
PA.Type = 2
HAVING
o.Pieces <> Count(*)
GROUP BY
PA.OrderID
ORDER BY
PA.OrderID
DESC

I also need to make the Orders table 'clickable' in the results.


I'm not entirely sure what you mean by this.


I think the problem may be that HAVING needs to be after GROUP BY.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1398132
Posted Wednesday, December 19, 2012 10:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:40 AM
Points: 5, Visits: 6
That doesn't seem to work either. After dropping and adding lines to the code, it seems that the comparison line is where it hiccups.

Here is an example where I get the columns to display side by side. Still, the HAVING is where I don't yet have it. If I omit the HAVING, the query runs fine.

SELECT
O.OrderID,
O.Pieces,
PA.OrderID,
COUNT(PA.OrderID) AS TotalParcels,
COUNT(*) AS 'TotalParcels'
FROM
tblParcelAssigned as PA
INNER JOIN
tblOrder AS O
ON O.OrderID = PA.OrderID
WHERE
PA.Type = 2
-- HAVING
-- o.Pieces <> Count(*)
GROUP BY
O.OrderID,
O.Pieces,
PA.OrderID
ORDER BY
PA.OrderID
DESC
Post #1398534
Posted Wednesday, December 19, 2012 10:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:40 AM
Points: 5, Visits: 6
By the way, the indentations in my code display fine when I edit my post, but the final version has them all lined up without colors or indents. Not sure what I'm doing wrong!
Post #1398544
Posted Wednesday, December 19, 2012 3:18 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:55 AM
Points: 6,142, Visits: 7,195
Use the code="sql" IfCode on the left, like so. Dwain's right, I flipped the order for having/group by, need to put it after.


SELECT 
O.OrderID,
O.Pieces,
PA.OrderID,
COUNT(PA.OrderID) AS TotalParcels,
COUNT(*) AS 'TotalParcels'
FROM
tblParcelAssigned as PA
INNER JOIN
tblOrder AS O
ON O.OrderID = PA.OrderID
WHERE
PA.Type = 2
GROUP BY
O.OrderID,
O.Pieces,
PA.OrderID
HAVING
o.Pieces <> Count(*)
ORDER BY
PA.OrderID
DESC




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1398690
Posted Thursday, December 20, 2012 7:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:40 AM
Points: 5, Visits: 6
Thank you everyone for your help. I'm sure I'll get better at this the more I practice. I've referred those who've offered me accoldes to this thread.

Dan
Post #1398976
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse