PostgreSQL两个组隔离但不是仅按零价格列排序

时间:2015-04-03 17:01:52

标签: postgresql group-by postgresql-9.3

我需要一些疯狂的单一查询目标的帮助,我不确定GROUP BY或子SELECT是否适用于此?

以下查询:

SELECT id_finish, description, inside_rate, outside_material, id_part, id_metal
FROM parts_finishing AS pf 
LEFT JOIN parts_finishing_descriptions AS fd ON (pf.id_description=fd.id);

返回如下结果:

+-------------+-------------+------------------+--------------------------------+
| description | inside_rate | outside_material | id_part - id_finish - id_metal |
+-------------+-------------+------------------+--------------------------------+
| Nickle      | 0           | 33.44            | 4444-44-44, 5555-55-55         |
+-------------+-------------+------------------+--------------------------------+
| Bend        | 11.22       | 0                | 1111-11-11                     |
+-------------+-------------+------------------+--------------------------------+
| Pack        | 22.33       | 0                | 2222-22-22, 3333-33-33         |
+-------------+-------------+------------------+--------------------------------+
| Zinc        | 0           | 44.55            | 6000-66-66                     |
+-------------+-------------+------------------+--------------------------------+

我需要以下面的方式返回结果,但有捕获:

  1. 我需要按inside_rate outside_material分组,但 ORDER BY {{ 1}}列但 description或按价格排序(ORDER BYinside_rate是价格)。因此,如果outside_material为0,则我们知道它们属于一个群组;如果inside_rate为0,则我们知道它们属于另一个群组。

  2. 每个群组返回后,我需要outside_material ORDER BYdescription次要。

  3. 我需要返回一个零件清单(由三个单独的列组成),用于该整理的内/外组/价格。

  4. 堆栈格式修复。

    desc

    我正在使用的表及其数据类型:

    +-------------+-------------+------------------+--------------------------------+
    | description | inside_rate | outside_material | id_part - id_finish - id_metal |
    +-------------+-------------+------------------+--------------------------------+
    | Bend        | 11.22       | 0                | 1111-11-11                     |
    +-------------+-------------+------------------+--------------------------------+
    | Pack        | 22.33       | 0                | 2222-22-22, 3333-33-33         |
    +-------------+-------------+------------------+--------------------------------+
    | Nickle      | 0           | 33.44            | 4444-44-44, 5555-55-55         |
    +-------------+-------------+------------------+--------------------------------+
    | Zinc        | 0           | 44.55            | 6000-66-66                     |
    +-------------+-------------+------------------+--------------------------------+
    

    第二个表的第一列只是 Table "public.parts_finishing" Column | Type | Modifiers ------------------+---------+------------------------------------------------------------- id | bigint | not null default nextval('parts_finishing_id_seq'::regclass) id_part | bigint | id_finish | bigint | id_metal | bigint | id_description | bigint | date | date | inside_hours_k | numeric | inside_rate | numeric | outside_material | numeric | sort | integer | Indexes: "parts_finishing_pkey" PRIMARY KEY, btree (id) Table "public.parts_finishing_descriptions" Column | Type | Modifiers ------------+---------+------------------------------------------------------------------ id not null | bigint | default nextval('parts_finishing_descriptions_id_seq'::regclass) date | date | description | text | rate_hour | numeric | type | text | Indexes: "parts_finishing_descriptions_pkey" PRIMARY KEY, btree (id) 。 (为什么我们在2015年仍然处理1024静态宽度布局?)

    我会做一个SQL小提琴虽然不管浏览器如何都拒绝为我加载。

1 个答案:

答案 0 :(得分:1)

不完全确定我理解你的问题。可能看起来像这样:

SELECT pd.description, pf.inside_rate, pf.outside_material
     , concat_ws(' - ', pf.id_part::text
                      , pf.id_finish::text
                      , pf.id_metal::text) AS id_part_finish_metal
FROM   parts_finishing pf 
LEFT   JOIN parts_finishing_descriptions fd ON pf.id_description = fd.id
ORDER  BY (pf.inside_rate = 0)         -- 1. sorts group "inside_rate" first
     , pd.description DESC NULLS LAST  -- 2. possible NULL values last
;