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