Group records, but select specific fields only from the first record

I am collecting an aggregate function for several records that are grouped by a common identifier. The problem is that I also want to export some other fields that may be different in grouped records, but I want to get these specific fields from one of the records (the first, as requested by ORDER BY).

Example starting point:

SELECT
  customer_id,
  sum(order_total),
  referral_code
FROM order
GROUP BY customer_id
ORDER BY date_created

I need to request a referral code, but doing it outside the aggregated function means that I need to group this field as well, and this is not what I want - in this example I need exactly one line for each client. I really care only about the referral code from the first order, and I am glad to throw out any subsequent codes of referral messages.

PostgreSQL, , , .

:

  • max() min(), .
  • , ; . , referral_code, , WHERE, , , .
+3
6

, .

, :

select customer_id, sum(order_total)
from order
group by customer_id

, , 1- _ date_created customer_id:

select distinct on (customer_id) customer_id, date_created, referral_code
from order
order by customer_id, date_created

2 :

select
    x1.customer_id,
    x1.sum,
    x2.date_created,
    x2.referral_code
from
    (
        select customer_id, sum(order_total)
        from order
        group by customer_id
    ) as x1
    join
    (
        select distinct on (customer_id) customer_id, date_Created, referral_code
        from order
        order by customer_id, date_created
    ) as x2 using ( customer_id )
order by x2.date_created

, , .

+1

. GROUP BY, . Oracle.

0

, - :

SELECT
     O1.customer_id,
     O1.referral_code,
     SQ.total
FROM
     Orders O1
LEFT OUTER JOIN Orders O2 ON
     O2.customer_id = O1.customer_id AND
     O2.date_created < O1.date_created
INNER JOIN (
     SELECT
          customer_id,
          SUM(order_total) AS total
     FROM
          Orders
     GROUP BY
          customer_id
     ) SQ ON SQ.customer_id = O1.customer_id
WHERE
     O2.customer_id IS NULL
0

date_created customer_id, :

[ ]

create table ordertable (customer_id int, order_total int, referral_code char, date_created datetime)
insert ordertable values (1,10, 'a', '2009-01-01')
insert ordertable values (2,15, 'b', '2009-01-02')
insert ordertable values (1,35, 'c', '2009-01-03')

[ - :)]

SELECT
  orderAgg.customer_id,
  orderAgg.order_sum,
  referral.referral_code as first_referral_code
FROM (
        SELECT
          customer_id,
          sum(order_total) as order_sum
        FROM ordertable
        GROUP BY customer_id
    ) as orderAgg join (
        SELECT
          customer_id,
          min(date_created) as first_date
        FROM ordertable
        GROUP BY customer_id
    ) as dateAgg on orderAgg.customer_id = dateAgg.customer_id
    join ordertable as referral 
        on dateAgg.customer_id = referral.customer_id
            and dateAgg.first_date = referral.date_created
0

- ?

SELECT
  customer_id,
  sum(order_total),
  (SELECT referral_code 
   FROM order o 
   WHERE o.customer_id = order.customer_id 
   ORDER BY date_created 
   LIMIT 1) AS customers_referral_code
FROM order
GROUP BY customer_id, customers_referral_code
ORDER BY date_created

, WHERE โ€‹โ€‹ , , " ", referral_code. ( , MySQL).

, referral_code , , 1:1, . .

:

SELECT
  o.customer_id,
  sum(o.order_total),
  c.referral_code, c.x, c.y, c.z
FROM order o LEFT JOIN (
    SELECT referral_code, x, y, z
    FROM orders c 
    WHERE c.customer_id = o.customer_id 
    ORDER BY c.date_created
    LIMIT 1
) AS c
GROUP BY o.customer_id, c.referral_code
ORDER BY o.date_created
0
SELECT  customer_id, order_sum,
        (first_record).referral, (first_record).other_column
FROM    (
        SELECT  customer_id,
                SUM(order_total) AS order_sum,
                (
                SELECT  oi
                FROM    order oi
                WHERE   oi.customer_id = o.customer_id
                LIMIT 1
                ) AS first_record
        FROM    order o
        GROUP BY
                customer_id
        ) q
0

Source: https://habr.com/ru/post/1707623/


All Articles