3

私はこれらの3つのテーブルを持っています: ここに画像の説明を入力

そして、私はこのコードを使用してそれらを結合しています (私は Delphi を使用しています):

ADOQ.SQL.Text := 'select a.IdArt as [Code d''Article], '+
                   'a.Nom,'+
                   'Str(a.Prix)+" TND" as Prix, '+
                   '(select Str(sum(QteEntree))+" "+a.unit from Stock where IdArt = a.IdArt group by IdArt) as [Quantite Entree],' +
                   '(select Str(sum(Qte))+" "+a.unit from Sold where IdArt = a.IdArt group by IdArt) as [Quantite Vendu],'+
                   'Str((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) -' +
                   '(select sum(Qte) from Sold where IdArt = a.IdArt group by IdArt))+" "+a.unit as [Quantite Existe]'+

                   'from Article a ';

ここに画像の説明を入力

ご覧のとおり、1 つのテーブルに欠落レコードがあると、DbGrid に Null が返されるので、その欠落レコードを「0」に置き換えたいと考えています。私はこのコードを試しました:

ADOQ.SQL.Text := 'select a.IdArt as [Code d''Article], '+
                   'a.Nom,'+
                   'Str(a.Prix)+" TND" as Prix, '+
                   '(select Str(sum(QteEntree))+" "+a.unit from Stock where IdArt = a.IdArt group by IdArt) as [Quantite Entree],' +
                   '(select IIF( IsNull(sum(Qte)), "111" , Format( sum(Qte),"00.00") ) from Sold where IdArt = a.IdArt group by IdArt) as [Quantite Vendu],'+
                   'Str((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) -' +
                   '(select sum(Qte) from Sold where IdArt = a.IdArt group by IdArt))+" "+a.unit as [Quantite Existe]'+

                   'from Article a ';

しかし、このコードは完全に機能しますが、何も変わりません:

ADOQ.SQL.Text := 'Select a.IdArt,IIF(( IsNull( s.qte) ), "00,00" , Format( (s.qte),"00.00") ) from Article a left join sold s on s.IdArt = a.IdArt';

ここに画像の説明を入力

ここで何が間違っていますか?

4

2 に答える 2

0

「groupbyIdArt」の部分を取り出してみてください。WHERE句で他のすべての部分をすでに除外しているため、これらの部分は不要のようです。

そうでなければ、このようなものはあなたがあなたのクエリを書く方法の改善であるかもしれません:

select 
    a.IdArt as [Code d''Article], 
    a.Nom,
    Str(a.Prix)+" TND" as Prix, 
    Str(sum(stock.QteEntree))+" "+a.unit as [Quantite Entree],
    IIF( IsNull(sum(sold.Qte)), "111" , Format( sum(sold.Qte),"00.00") ) as [Quantite Vendu],
    Str(sum(stock.QteEntree) - sum(sold.Qte))+" "+a.unit as [Quantite Existe]

from Article a 
left join stock on a.IdArt = stock.IdArt
left join sold on a.IdArt = sold.IdArt
group by a.IdArt, a.Nom, a.Prix, a.unit

あなたはそれを微調整する必要があるかもしれません

実際、私はこのステートメントを上から書きたいと思います。

IIF( IsNull(sum(sold.Qte)), "111" , Format( sum(sold.Qte),"00.00") ) as [Quantite Vendu],

代わりにこのように:

Format( sum( IIF( IsNull( sold.Qte ) , 0, sold.Qte ) ), "00.00" ) as [Quantite Vendu],

繰り返しますが、それが機能するかどうかは100%わかりません。関数の順序とロジックを見ているだけです。

于 2012-07-05T07:56:10.767 に答える
0

わかりました解決策を見つけました:

iif( isnull((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt)) ,0, (select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) )  
于 2012-07-05T20:15:23.543 に答える