Tahun baru, skill baru! 🚀. Masukkan kupon "skill2025" untuk diskon 30% di kelas apa saja

Mengambil data berdasarkan bulan dan tahun (Odoo 8, sql query)

Saya memiliki data seperti gambar dibawah ini:

Saya mau mengambil 1 cost saja per id berdasarkan month_input, month_input=month_price dan jika month_input tidak sama dengan month_price, maka mengambil data berdasarkan bulan(month_price) sebelumnya. Misal month_input=9, month_price=1 dan 6, nah berarti ambil data cost yg month_price nya 6.

Code nya seperti dibawah ini:

select
  e.month as month_price,
  extract(month from b.date_invoice)::integer as month_invoice,
  case extract(month from b.date_invoice)::integer = extract(month from b.date_invoice)::integer-1
	   when e.month=(extract(month from b.date_invoice)::integer) then e.cost else null end
	as cost, -- DATA COST
  d.id as id,
  sum(a.quantity) as qty
from (select account_analytic_id,invoice_id,product_id,quantity,price_subtotal from account_invoice_line) a
LEFT OUTER JOIN (select id, date_invoice from account_invoice) b
  ON a.invoice_id=b.id
LEFT OUTER JOIN product_product c
  ON a.product_id=c.id
LEFT OUTER JOIN product_template d
  ON c.product_tmpl_id=d.id
LEFT OUTER JOIN (select extract(month from datetime)::integer as month, cost, product_template_id from product_price_history where cost!=0 group by month,cost,product_template_id) e
  ON d.id=e.product_template_id
where b.date_invoice>='2017-09-01' and b.date_invoice<='2017-09-13' and d.id in ('1229','1250','1176') and a.account_analytic_id=482 and cost!=0
group by e.month,d.id,b.date_invoice,e.cost,a.quantity
order by d.name

Apa bisa memakai case when? atau mengatur loopingnya di python?

avatar FJMhmmd
@FJMhmmd

1 Kontribusi 0 Poin

Dipost 7 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban