■ Make a Query to collect todouhuken list with the number of motorcycles following each matched todouhuken
.todouhuken_code
.todouhuken_name
.todouhuken_area_code
.area_name . The number of motorcycles following each matched todouhuken (if it is null, it will be showed 0).
■ Table need to query:
.mst_todouhuken.
.mst_area.
.mst_model_v2.
.search_motorcycle.
■ Sort data in ascending order:
.todouhuken_area_code2.
.todouhuken_code.
อันนี้ โค้ดของผมครับ
select
t.todouhuken_code,
t.todouhuken_name,
t.todouhuken_area_code2,
area_name,
case
when motorcycle_no is null or motorcycle_no = ''
then '0' else motorcycle_no
end as cnt
from
mst_area a, mst_todouhuken t , search_motorcycle s , mst_model_v2 v2
where
a.area_code2 = t.todouhuken_area_code2 and
a.area_code = t.todouhuken_area_code and
t.todouhuken_code = s.dealer_todouhuken_code and
s.motorcycle_model_code = v2.model_code and
s.motorcycle_maker_code = v2.model_maker_code and
motorcycle_jyoukyo in (1,5,6) and
model_maker_code = 4 and
dealer_todouhuken_code > 0
group by
todouhuken_name,todouhuken_name,todouhuken_area_code2,area_name asc
order by
todouhuken_area_code2,todouhuken_code asc