2011-11-08 11 views
-2

私は2つのテーブルを持っています。クライアント(clientid、company、state、status)とsupportstatus(clientid、company、status、agreement)が含まれます。次のクエリは、合意値が0でステータスが「無効」ではないすべての企業を返すべきではありませんか? 私は会社を選んでいるので、私は混乱しています。私は共通の要素(clientid)で2つのテーブルに加わりました。そして、私はクエリの結果をフィルタリングしています。SQL結合問題の明確化

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
ORDER BY cl.company 

ColdFusionのソース

<cfquery name="qryPendingAgreement" datasource="support"> 
SELECT clientid 
FROM supportstatus 
WHERE agreement11 = 0 AND status <> 'disabled' 
</cfquery> 

<cfquery name="qryClient" datasource="support"> 
SELECT  clientid, company, state, serv_billing 
FROM   clients 
WHERE prod_arth = 1 OR prod_artr = 1 OR prod_epcr_host = 1 OR prod_epcr_remote = 1 OR prod_billing = 1 OR prod_collections = 1 
</cfquery> 

<cfquery name="qryResults" dbtype="query"> 
SELECT qryClient.company, qryClient.state, qryClient.serv_billing 
FROM qryPendingAgreement, qryClient 
WHERE qryPendingAgreement.clientid = qryClient.clientid 
order by qryClient.company 
</cfquery> 
+2

あなたの質問は何ですか?結果を返さない場合は、ソースデータの例と望ましい結果が表示されます。 –

+0

予期しない結果が起こっているのはどういうことでしょうか?クエリは整形式です。 –

+0

@MartinSmithが言った行に沿って、su.statusにNULL値が含まれていますか? – tawman

答えて

0

これは野生の推測ですが、あなたは、このバージョンを使用している場合:

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
     OR prod_arth = 1  OR prod_artr = 1 
     OR prod_epcr_host = 1 OR prod_epcr_remote = 1 
     OR prod_billing = 1 OR prod_collections = 1 
ORDER BY cl.company 

を問題がありますAND,OR優先。試してみてください:

SELECT cl.company 
FROM clients cl 
     INNER JOIN supportstatus su 
     ON cl.clientid = su.clientid 
WHERE su.agreement11 = 0 
     AND su.status <> 'disabled' 
     AND (prod_arth = 1  OR prod_artr = 1 
      OR prod_epcr_host = 1 OR prod_epcr_remote = 1 
      OR prod_billing = 1 OR prod_collections = 1 
      ) 
ORDER BY cl.company 
1

クエリがあなたに与えていた結果何も言いませんでした。クエリが必要以上に多くの行をフィルタリングしている場合、その答えは3値のロジックになる可能性があります。

null != 'disabled' - >nullこれは真ではなく、行はフィルタリングされます。

代わりにこのフィルタを試してください。

WHERE su.agreement11 = 0 
    AND isnull(su.status,'') <> 'disabled' 
+0

申し訳ありません。はい、私のクエリは、CFバージョン – MasterP

+0

@MasterPより5つ多くの結果を返します。CFクエリに追加のフィルタがあります: 'prod_arth = 1 OR ...' –

+0

SELECT cl.company FROM clients cl .clientid = su .clientid WHERE su.agreement11 = 0 AND isnull(su.status、 '')<> '無効' order By cl.company =ネイティブ関数 'isnull'の呼び出しでパラメータのカウントが正しくない – MasterP

0

これは、あなたのクエリをテストに役立つはずです。あなたの質問は十分に特異的ではなかったので、これは私ができる最善である:)

DECLARE @client TABLE 
    (
     clientid int, 
     company VARCHAR(50) 
    ) 
    DECLARE @supportstatus TABLE 
    (
     clientid int, 
     [status] VARCHAR(50), 
     agreement INT 
    ) 

    INSERT INTO @client ([clientid],[company]) VALUES (0,'acme') 
    INSERT INTO @client ([clientid],[company]) VALUES (1,'byron') 
    INSERT INTO @client ([clientid],[company]) VALUES (2,'cathode') 

    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'disabled',1) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'somethingelse',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (0, 'somethingelse',1) 

    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'disabled',1) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'somethingelse',0) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (1, 'somethingelse',1) 

    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'disabled',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'disabled',1) 
    --INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'somethingelse',0) 
    INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES (2, 'somethingelse',1) 

    -- all companies who have su.agreement = 0 and su.status <> 'disabled' 
    SELECT cl.company 
    FROM @client cl 
    JOIN @supportstatus su ON cl.clientid = su.clientid 
    WHERE su.agreement = 0 AND su.status <> 'disabled' 
    Order By cl.company 
+0

ご協力いただきありがとうございます – MasterP