Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
5293 มุมมอง

Hello All,

 I'm working on Odoo11 and using psql. And I want to select the same column twice for the same table on a different condition, For this, I'm using case statement And I got the output, but not as expected. Because I want output in a single row but it comes in two rows.

Query

SELECT project_id,
CASE WHEN project_type = 'Internal' THEN SUM(unit_amount) END,
CASE WHEN project_type = 'Project' THEN SUM(unit_amount) END
FROM account_analytic_line where project_id = 347 GROUP BY project_id,project_type

Now result coming like that: 

project_id integer

case double_precision

case double_precision

347

7.16666666667


347


33.58333333333

Require Result:

project_id integer


case double_precision

case double_precision

347

7.16666666667

33.58333333333


For the above screenshot, it generates two rows for the same project_id and wants these columns in a single row for the same project_id. Project type is the selection field with two options(Project, Internal)

Thanks in advance


อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

I got solution for my problem:

select distinct on (project_id)
project_id,
sum(unit_amount) FILTER (where project_type = 'Internal') over(PARTITION BY project_id) as intern,
sum(unit_amount) FILTER (where project_type = 'Project') over(PARTITION BY project_id) as project
FROM account_analytic_line where project_id = 347

Thanks to all

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด
SELECT project_id,
CASE WHEN project_type = 'Internal' THEN SUM(unit_amount)
WHEN project_type = 'Project' THEN SUM(unit_amount) END 
FROM account_analytic_line where project_id = 347 GROUP BY project_id,project_type

pls use this 
อวตาร
ละทิ้ง
ผู้เขียน

Hello Usman, it still take two rows. I want it only in single row.

ผู้เขียน

I added the expected table in the above problem.

คำตอบที่ดีที่สุด

Hi, you can follow this for using PSQL commands using python: https://youtu.be/ntMaAYbsDYM 

Hope it helps

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ต.ค. 20
4230
2
มี.ค. 15
3547
1
ต.ค. 24
1668
1
ก.พ. 24
2280
Hide menu for all except specific group แก้ไขแล้ว
1
พ.ย. 22
2513