2012-03-28 20 views
0

全く同じクエリを使用して2つのテーブルからデータを取得していました。 c.IsReceived = 0部分を使用しないと、日付が返されますが、使用時には何も返されません。 RepDailyCollectionにはIsReceived = 0の行があり、RepID = 205には行があります。私は間違っていると思いますか?これは2005年のSQL Serverにあります。あなたを刺してください!sql query return null

Select 
    distinct RepDailyInfo.Date 
from 
    RepDailyInfo 
left outer join 
    RepDailyCollection c 
on 
    c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID 
where 
    RepDailyInfo.RepID = 205 and c.IsReceived = 0 

編集:

私はこの素晴らしい作品に似た他のストアドプロシージャを使用します。

Select i.Date 
--i.RepDailyInfoID, i.RepID, i.TypeofDayID, i.CommuniTeeID, sum(cast(c.AmountSold as numeric(10,2))) as AmountSold, 
-- sum(cast (c.AmountCollected as numeric(10,2))) as AmountCollected, c.NewCompanyName,c.PaymentMethod, 
-- c.TypeofCreditCard, c.CheckNumber, c.Invoice, c.Spots, TypeofDay.TypeofDay, CommuniTee.City, CommuniTee.State, 
-- CommuniTee.year, SalesRep_Info.FirstName, SalesRep_Info.LastName 
from RepDailyInfo i 
left outer join RepDailyCollection c on i.RepDailyInfoID = c.RepDailyInfoID 
left outer join TypeOfDay on TypeOfDay.TypeofDayID = i.TypeofDayID 
left outer join SalesRep_Info on SalesRep_Info.RepID = i.RepID 
left outer join CommuniTee on CommuniTee.CommuniTeeID = i.CommuniTeeID 
where i.RepID = 205 and c.IsReceived = 0 
group by i.RepDailyInfoID, i.Date, i.TypeofDayID, i.CommuniTeeID, SalesRep_Info.FirstName, TypeofDay.TypeofDay, 
CommuniTee.City, CommuniTee.State, CommuniTee.year, SalesRep_Info.FirstName, 
SalesRep_Info.LastName, i.RepID, c.NewCompanyName, c.PaymentMethod, c.TypeofCreditCard, c.CheckNumber, c.Invoice, c.Spots 
order by SalesRep_Info.FirstName desc 

答えて

0

ここにはいくつかの問題があります。あなたが原因あなたが同様に内部結合を言ったかもしれないwhere句に、「外部結合左」と言っていても

Select 
    distinct RepDailyInfo.Date 
from 
    RepDailyInfo 
left outer join 
    RepDailyCollection c 
on 
    c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID 
where 
    RepDailyInfo.RepID = 205 and c.IsReceived = 0 

:あなたが持っている最初のクエリで

これに関係なく、このクエリは2番目のクエリと一致しません。 2番目のクエリでは、RepDailyInfoIDにRepDailyInfoへのRepDailyInfoへの参加がになります。最初のクエリでは、完全に異なる列に参加しています。

+0

あなたは正しいクリスです。私は質問を混乱させた。 :-(私は間違っていたかもしれないことを理解しています。 – Ram

0
あなたが持っている

"LEFT JOINのON ... ... = RepDailyInfo.RepDailyInfoID"、それは実際には "LEFT ... ON ... = RepDailyInfo.RepDailyCollectionIDを登録しよう" すべきですか?

+0

編集部分をご覧ください。それはうまく動作し、私に尋ねた実際の質問ではありません。 – Ram

+0

私はあなたの答えを得てうれしいです。私が参加しているフィールドが不一致のときに結果が得られない傾向があることは分かっています。 – Brian

0

varchar/charフィールドですか?

c.IsReceived = '0' 
+0

いいえ、それはブール値です:-( – Ram

0

ありIsReceived = 0RepID = 205RepDailyCollectionレコードかもしれませんが、205のREPIDとRepDailyInfoですべてのレコードが存在しないならば、レコードがあるため、あなたのwhere句で返されません。

あなたの句があるべき場所のようなあなたの文

に基づき= 205

REPIDのための受信される= 0とRepDailyCollection内の行があり、それは聞こえる:代わりに

c.RepID = 205 and c.IsReceived = 0 

RepDailyInfo.RepID = 205 and c.IsReceived = 0 
あなたのテストのための
+0

ありがとう、RepDailyInfoにRepID = 205の行があります – Ram

+0

RepDailyCollectionに列RepIDがありません。私は二重チェックしました:-(結合のために問題ですか? – Ram

+0

質問の編集部分を見てください?私は編集部分を使用して結果を得ることができますが、私は尋ねられた最初の質問に何が間違っていたのだろうかと思います..あなたの時間をありがとう@abe – Ram

0

..あなたは、クエリを次の操作を行うことができます。..

Select distinct c.IsReceived from RepDailyInfo 
left outer join RepDailyCollection c on c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID 
where RepDailyInfo.RepID = 205 and c.IsReceived = 0 

あなたは0この列は、VARCHAR2データ型でなければなりません見ることができれば...

その後、

Select distinct RepDailyInfo.Date from RepDailyInfo 
left outer join RepDailyCollection c on c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID 
where RepDailyInfo.RepID = 205 and c.IsReceived = '0' 
\次のクエリを使用します
+0

IsReceivedは真または偽です。 : – Ram

+0

Pratikの質問の編集部分を見てもらえますか?私は結果を得ることができますが、私は尋ねられた最初の質問で何が間違っているのだろうかと疑問に思っています。 – Ram

+0

私はあなたにSQLを求めていると思っています。 ブール型の値を0>と比較している理由を教えてください。? SQLサーバーにあります。> oracleでは、真偽を「真」または「偽」で直接チェックすることができます –