2016-05-02 23 views
1

私は2つのテーブルw300v600を持っています。私はマシンの数を数えたいと思います。問題はテーブルフィールドv600.vbと結合されたマシンだけを数えることです。例えば2つのテーブルのSQL合計カウント(*)

Table v600 fields: vb, konto, satzart, fi_nr 

Table w300 fields: maschnr, konto, satzart, fi_nr, 

fi_nr[Key] 
konto[Key] 
satzart[Key] 

: 私は

select 'Anz. Machine: ', count(*)   \ 
     from w300         \ 
     where length(w300.maschnr) in (6,7)   \ 
     and w300.maschnr LIKE ('%M%')    \ 
     and w300.maschnr not LIKE ('%FLP%')   \ 
     and w300.maschnr not LIKE ('%DFS%')   \ 
     and w300.maschnr not LIKE ('%SB%')   \ 
     and w300.maschnr not LIKE ('%C%')   \ 
     and w300.maschnr not LIKE ('%DL%')   \ 
     and w300.konto   = 8000031   \ 
     and w300.satzart  = 1     \ 
     and w300.fi_nr   = 1     \ 

をしたいが、私はそれを取得しないだけで、これらの場所v600.vb = 8000001.

。 ありがとうございます。

写真をいくつか追加しました。

Example

Example

Tables

Result

結果は、ANZ銀行のw300.konto = 8000001場合とv600.vb = 8000001量であるべきです。マシン= 60

select * from v600 where vb = 8000001; 

v600 CUSTOMERS   
fi_nr satzart konto  vb 

1  1  8000034  8000001 

1  1  8000039  8000001 

1  1  8000219  8000001 

1  1  8000031  8000001 

1  1  8000001  8000001 

1  1  8000037  8000001 

select * from w300 where konto = 8000039; 

w300 MACHINES    
fi_nr maschnr identnr satzart konto 

1  #M2684D #M2684D 1  8000039 

1  #M2719D #M2719D 1  8000039 

select * from w300 where konto = 8000219; 

w300 MACHINES  
fi_nr maschnr identnr satzart konto 

1  #M2581 #M2581 1 8000219 

1  #M2591 #M2591 1 8000219 

1  #M2599 #M2599 1 8000219 

1  #M2600 #M2600 1 8000219 

1  #M2601 #M2601 1 8000219 

1  #M2604 #M2604 1 8000219 

1  #M2605 #M2605 1 8000219 

1  #M2606 #M2606 1 8000219 


select 'Anz. Machine: ', count(*)   
from w300         
where length(w300.maschnr) in (6,7)   
and w300.maschnr LIKE ('%M%')    
and w300.maschnr not LIKE ('%FLP%')   
and w300.maschnr not LIKE ('%DFS%')   
and w300.maschnr not LIKE ('%SB%')   
and w300.maschnr not LIKE ('%C%')   
and w300.maschnr not LIKE ('%DL%')   
and  w300.konto   = 8000039  
and w300.satzart  = 1   
and w300.fi_nr  = 1 ; 

plus: 

    select 'Anz. Machine: ', count(*)   
from w300         
where length(w300.maschnr) in (6,7)   
and w300.maschnr LIKE ('%M%')    
and w300.maschnr not LIKE ('%FLP%')   
and w300.maschnr not LIKE ('%DFS%')   
and w300.maschnr not LIKE ('%SB%')   
and w300.maschnr not LIKE ('%C%')   
and w300.maschnr not LIKE ('%DL%')   
and  w300.konto   = 8000219  
and w300.satzart  = 1   
and w300.fi_nr  = 1 ; 

plus: 

    select 'Anz. Machine: ', count(*)   
from w300         
where length(w300.maschnr) in (6,7)   
and w300.maschnr LIKE ('%M%')    
and w300.maschnr not LIKE ('%FLP%')   
and w300.maschnr not LIKE ('%DFS%')   
and w300.maschnr not LIKE ('%SB%')   
and w300.maschnr not LIKE ('%C%')   
and w300.maschnr not LIKE ('%DL%')   
and  w300.konto   = 8000034  
and w300.satzart  = 1   
and w300.fi_nr  = 1 ; 

= Anz. Machine where v600.vb = 8000001 
+0

良い方法のようなものを探しているかもしれない考える質問をする:いくつかのサンプルコードを掲載し、期待される出力をここでもっと見る... https://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a- public-forum/ – TheGameiswar

+0

INNを使用するER JOINからv600.vbへ –

+3

それは同時にできることはできませんMySQLとSQLサーバー?このような場合には – e4c5

答えて

0

を使用することができ、私はあなたが

select 'Anz. Machine: ', count(*)   
from w300         
where length(w300.maschnr) in (6,7)   
... 
and  w300.konto   IN (SELECT konto FROM v600 
           WHERE vb = 8000001) 
... ; 
+0

うん。これは私が意味していたものです。 ありがとうございます。:) –

0

あなたは、INNERこの方法のように登録しようあなたの編集に基づいて

select 'Anz. Machine: ', count(*)   
     from w300 INNER JOIN v600 ON v600.vb = w300.konto         
     where length(w300.maschnr) in (6,7)   
     and w300.maschnr LIKE ("%M%")    
     and w300.maschnr not LIKE ("%FLP%")   
     and w300.maschnr not LIKE ("%DFS%")   
     and w300.maschnr not LIKE ("%SB%")   
     and w300.maschnr not LIKE ("%C%")   
     and w300.maschnr not LIKE ("%DL%")   
     and w300.konto   = 8000031   
     and w300.satzart  = 1     
     and w300.fi_nr   = 1 
+0

私はこの結果をAnzに使用します。マシン= 0でも60でなければなりません。 –

関連する問題