2009-05-19 15 views
4

私はこのクエリがあります:nullの代わりに0を返すようにこのクエリを取得するにはどうすればよいですか?

SELECT (SUM(tblTransaction.AmountPaid) - SUM(tblTransaction.AmountCharged)) AS TenantBalance, tblTransaction.TenantID 
    FROM tblTransaction 
    GROUP BY tblTransaction.TenantID 

をしかし、それに問題があります。トランザクションを持たない他のテナントIDがあり、それらも取得したいと考えています。

たとえば、トランザクションテーブルには、bobの場合は3行、johnの場合は2行、janeの場合は3行あります。私はbobとjohnの合計を返し、janeの場合は0を返します。 (他の方法がない場合はnull)

どうすればいいですか?

 
Tenants 
    ID 
    Other Data 
Transactions 
    ID 
    TenantID (fk to Tenants) 
    Other Data 
+0

私の答えでクエリを書き換えましたが、あなたが求めていたものを正確に誤解しました。 –

答えて

14

(あなたはあなたのSQLエンジンを述べていなかったので、私は、MySQLのドキュメントにリンクするつもりです):

の表は、このようなものです。

これは、ちょうどCOALESCE()機能のためのものです。リストにフィードすることができ、リストの最初のnullでない値が返されます。次のようにあなたのクエリでこれを使用します。SUM()結果はNULLであるかどう

SELECT COALESCE((SUM(tr.AmountPaid) - SUM(tr.AmountCharged)), 0) AS TenantBalance, te.ID 
FROM tblTenant AS te 
    LEFT JOIN tblTransaction AS tr ON (tr.TenantID = te.ID) 
GROUP BY te.ID; 

そのように、それがゼロに置き換えられています。

を編集:私はLEFTを使用してクエリを書き直したが、JOINなどCOALESCE()、私はこれがあなたが最初に欠けていたもののキーだと思います。取引テーブルからのみ選択すると、ではないテーブルのに関する情報を取得する方法がありません。ただし、テナント表からの左結合を使用すると、既存のすべてのテナントの行を取得する必要があります。

+0

タグを更新しました。これはSQL Server Express 2005のtsqlです – Malfist

+0

SQL Serverのcoalesceも同様に動作します – catalpa

+0

私自身の解決策が見つかったため、これが動作するかどうかわかりませんが、コミュニティが好きだと仮定していますそれで私は答えとしてそれを選択しています。 – Malfist

0
Select Tenants.ID, ISNULL((SUM(tblTransaction.AmountPaid) - SUM(tblTransaction.AmountCharged)), 0) AS TenantBalance 
From Tenants 
Left Outer Join Transactions Tenants.ID = Transactions.TenantID 
Group By Tenents.ID 

構文チェックはしていませんが、十分に近いです。あなたが意図している場合あなたがする必要がありますヌルいる部品の一つのために考慮することがあるので、私はこれだけを追加

SELECT tenant.ID, ISNULL(SUM(trans.AmountPaid) - SUM(trans.AmountCharged),0) AS Balance FROM tblTenant tenant 
LEFT JOIN tblTransaction trans 
ON tenant.ID = trans.TenantID 
GROUP BY tenant.ID 
+0

メッセージ4104、レベル16、状態1、行1 マルチパート識別子 "Tenents.ID"縛られる。 メッセージ4104、レベル16、状態1、行1 マルチパート識別子「tblTransaction.AmountPaid」はバインドできませんでした。 メッセージ4104、レベル16、状態1、行1 マルチパート識別子「tblTransaction.AmountCharged」はバインドできませんでした。 – Malfist

-1

は実は、私は答えを見つけました別途ISNULL

+0

はい、私はnullまたは0と言いましたが、私は0を好みました。それが答えとして選択されていない理由です。 – Malfist

+0

今すぐ0を返します。 – Malfist

0
SELECT (SUM(ISNULL(tblTransaction.AmountPaid, 0)) 
     - SUM(ISNULL(tblTransaction.AmountCharged, 0))) AS TenantBalance 
     , tblTransaction.TenantID 
     FROM tblTransaction 
     GROUP BY tblTransaction.TenantID 

+0

は、トランザクションを持つテナントのみを返します。 – Malfist

+0

@マフリスト元の質問には、あなたがテナントと呼ぶもののアカウントは含まれていません。あなたが言っているテナントの参加はどこですか? – Joseph

+0

質問には、FKとテナントテーブルのテーブル構造が記載されています。 – Malfist

1

以下は、この問題の詳細な説明です。トランザクションがないテナントに対して、ゼロ(ゼロよりもむしろ)のバランスが返されるように、関数isnullも含まれています。

create table tblTenant 
(
    ID int identity(1,1) primary key not null, 
    Name varchar(100) 
); 

create table tblTransaction 
(
    ID int identity(1,1) primary key not null, 
    tblTenantID int, 
    AmountPaid money, 
    AmountCharged money 
); 

insert into tblTenant(Name) 
select 'bob' union all select 'Jane' union all select 'john'; 

insert into tblTransaction(tblTenantID,AmountPaid, AmountCharged) 
select 1,5.00,10.00 
union all 
select 1,10.00,10.00 
union all 
select 1,10.00,10.00 
union all 
select 2,10.00,15.00 
union all 
select 2,15.00,15.00 


select * from tblTenant 
select * from tblTransaction 

SELECT 
    tenant.ID, 
    tenant.Name, 
    isnull(SUM(Trans.AmountPaid) - SUM(Trans.AmountCharged),0) AS Balance 
FROM tblTenant tenant 
    LEFT JOIN tblTransaction Trans ON 
     tenant.ID = Trans.tblTenantID 
GROUP BY tenant.ID, tenant.Name; 

drop table tblTenant; 
drop table tblTransaction; 
+0

これも機能します。 – Malfist

関連する問題