Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dwdwone
Yak Posting Veteran
71 Posts |
Posted - 2013-06-19 : 13:32:55
|
Good morning. Am trying to get my head around a problem with a query I wrote. The query compares a value on an order table to values in an attached table that shows parcels and compares the two for inequality. So, the number of pieces on an order might be '1' and the number of rows in the Parcels table for that order may be '2', and so the order is flagged in the query for review. So far so good, and the query appears to work fine.However, the eventuality I did not account for was when the order exists and shows a value for number of pieces, but no rows exist in the parcels table. What is the best way to approach this problem? Here is my code: (Sorry, indentations are not appearing even though I put them in)DECLARE @Calendar1 AS DateTimeDECLARE @Calendar2 AS DateTimeSET @Calendar1 = '{{{ Please choose a start date. }}}' SET @Calendar2 = '{{{ Please choose an end date. <(non inclusive)> }}}' SELECT O.OrderID, O.OriginName, O.ReadyTimeFrom, O.Pieces AS 'Pieces On Order', COUNT(P.OrderID) AS 'Parcels On Order'FROM tblOrder AS O INNER JOIN tblParcel as P ON P.OrderID = O.OrderIDWHERE O.ReadyTimeFrom BETWEEN @Calendar1 AND @Calendar2 + 1 AND O.Status <> 4 GROUP BY O.OrderID, O.OriginName, O.Pieces, P.OrderID, O.ReadyTimeFromHAVING O.Pieces <> Count(*) ORDER BY P.OrderID DESCThanks!Dan |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2013-06-19 : 13:47:38
|
[CODE]SELECT O.OrderID, O.OriginName, O.ReadyTimeFrom, O.Pieces AS 'Pieces On Order', COUNT(P.OrderID) AS 'Parcels On Order'FROM tblOrder AS O LEFT OUTER JOIN tblParcel as P ON P.OrderID = O.OrderIDWHERE O.ReadyTimeFrom BETWEEN @Calendar1 AND @Calendar2 + 1 AND O.Status <> 4GROUP BY O.OrderID, O.OriginName, O.Pieces, P.OrderID, O.ReadyTimeFromHAVING O.Pieces <> Count(*) OR COUNT(P.OrderID) = 0ORDER BY P.OrderID DESC[/CODE]BTW, you can use the tags CODE and /CODE (each surrounded by square brackets) around your code and it will preserve your formatting. Click on "Forum Code" in the left hand window of the Quick Reply box for additional details.=================================================The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen |
|
|
shan007
Starting Member
17 Posts |
Posted - 2013-06-19 : 13:51:10
|
I don't know whether I understood your request properly.. however I guess you are looking for the solution where you've record in Orders table, but there is no reference rows in parcel table. In such case you could use Left Join instead Inner Join. This will bring record from order table even if there is no reference rows in parcel.i.e. LEFT OUTER JOIN tblParcel as P ON P.OrderID = O.OrderIDLet me know if it helps.==============================I'm here to learn new things everyday.. |
|
|
dwdwone
Yak Posting Veteran
71 Posts |
Posted - 2013-06-19 : 14:55:23
|
Thank you very much. I supposed it's time for me to learn about OUTER JOINs! |
|
|
|
|
|
|
|