2017-04-20 15 views
0

すべての例を見つけたので、間違っていると確信しています。 私は結果を得ていますが、合計を2倍にしたり、合計を大量に複製したりしています。MySQLサブクエリはまだ2倍のSUM値を生成します

クエリは次のとおりです。

select 

(st.total_cells) as 'Cells Sorted', 

from abstract_freeze af 
left join abstract_ejaculate_collection aec on af.ejaculate_collection_id = aec.id 
left join mo_flo on aec.id = mo_flo.ejaculate_collection_id 
left join sample_time st on mo_flo.id = st.mo_flo_id 
left join collection_schedule cs on aec.collection_schedule_id = cs.id 
left join collection_plan cp on cs.collection_plan_id = cp.id 
left join sub_admission sa on cp.sub_admission_id = sa.id 
left join master_admission ma on sa.master_admission_id = ma.id 
left join animal on ma.animal_id = animal.id 
where aec.tenant_id = 8 and date(aec.scheduled) = '2017-04-12' and sa.type = 'Sexed' 
group by aec.id; 

af.ejaculate_collection_id年代の繰り返しの数に基づいて、私のSUM(st.total_cells)重複そのため、同じejaculate_collection_idで複数abstract_freezeレコードが存在します。

私のクエリまたはサブクエリが正しいと思われるので、グループ化に基づいてtotal_cellsの合計が必要です。

+0

私はどこに和が表示されません。これはあなたの全体の質問ですか? –

+0

申し訳ありませんが、 "Cells Sorted"に焦点を当てていましたが、SUM(st.total_cells)を読み込む必要があるため、私は短縮するためにいくつかの行を削除しました。私の間違い。 –

答えて

0

あなたはidごとに複数のレコードを持っているなら、あなたはおそらくidあたりSUMだけでなく、トータルを望む、あなたはこれを試みることができる:

select aec.id, SUM(st.total_cells) as 'Cells Sorted', 
from abstract_freeze af 
left join abstract_ejaculate_collection aec on af.ejaculate_collection_id = aec.id 
left join mo_flo on aec.id = mo_flo.ejaculate_collection_id 
left join sample_time st on mo_flo.id = st.mo_flo_id 
left join collection_schedule cs on aec.collection_schedule_id = cs.id 
left join collection_plan cp on cs.collection_plan_id = cp.id 
left join sub_admission sa on cp.sub_admission_id = sa.id 
left join master_admission ma on sa.master_admission_id = ma.id 
left join animal on ma.animal_id = animal.id 
where aec.tenant_id = 8 and date(aec.scheduled) = '2017-04-12' and sa.type = 'Sexed' 
group by aec.id; 
0

OK]をクリックして、その実際に前に入社する総括しなければなりませんでした。 はこれで終了し、それが適切な値を取得するために働いた。その後、

LEFT JOIN (SELECT tc.mo_flo_id, SUM(tc.total_cells) AS cells FROM sample_time tc GROUP BY mo_flo_id) tc USING (mo_flo_id) 

...

tc.cells AS 'Cells Sorted' 

が適切合計値を取得します。

助けをいただき、ありがとうございます。

関連する問題