私は2つのクエリを持っています。各照会は、組織と顧客の間の合計発注数、および発注の受注総額を引き出します。クエリは日付範囲を除いて同一です。1つのSQLクエリで複数の日付範囲からデータを取得するにはどうすればよいですか?
SELECT org.organization_id, org.name, cust.name as customer,
count(*) as num_orders, round (sum(cast(o.total_charge as real))) as receivables
FROM
organization as org, orders as o, organization as cust, reconcile_order as ro
WHERE org.organization_id = o.shipper_org_id
and o.broker_org_id = cust.organization_id
and o.order_id = ro.order_id
and o.status = 'D'
and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL)
and (NOW()::DATE - o.delivery_confirmed_date::DATE) < 31
group by org.organization_id, org.name,
cust.name
order by org.name asc limit 20
SELECT org.organization_id, org.name, cust.name as customer,
count(*) as num_orders, round (sum(cast(o.total_charge as real))) as receivables
FROM
organization as org, orders as o, organization as cust, reconcile_order as ro
WHERE org.organization_id = o.shipper_org_id
and o.broker_org_id = cust.organization_id
and o.order_id = ro.order_id
and o.status = 'D'
and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL)
and (NOW()::DATE - o.delivery_confirmed_date::DATE) between 31 and 60
group by org.organization_id, org.name,
cust.name
order by org.name asc limit 20
しかし、私はこれらの列の隣の第二日付範囲の列の別のペアを出力するが、最初の日付の範囲内の注文や債権のための列を持つ単一のテーブルであるように、この1つのクエリを作成する必要があり、そして。 (すなわちnum_orders < 31、債権< 31、num_orders 31から60、債権31〜60)
http://stackoverflow.com/q/39919206/330315 –