COUNT and compare

  • 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!

  • 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

    ONO.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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Your query works only if I omit the HAVING clause.

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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

    ONO.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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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

    ONO.OrderID = PA.OrderID

    WHERE

    PA.Type = 2

    -- HAVING

    -- o.Pieces <> Count(*)

    GROUP BY

    O.OrderID,

    O.Pieces,

    PA.OrderID

    ORDER BY

    PA.OrderID

    DESC

  • 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!

  • 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

    ONO.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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply