database - SQL query which puts columns from rows into one row -
i have 2 tables:
customer(cust_id, cust_name)
refund (ref_id, cust_id, ref_date, ref_amount)
cust_id foreign key points out customer.
each customer can have several refunds. want list of customers , 2 dates of refunds each cutomer. each client , dates of refunds must in 1 row. i.e. want foolowing result:
(cust_name, ref_date1, ref_amount1, ref_date2, ref_amount2)
for illustration - 'john smith', '06/06/2012', 500.0, '08/05/2014', 345.5
how can this? thanx!
(if of import utilize oracle 11g)
the next gets 2 recent refunds:
select c.cust_name, max(case when seqnum = 1 ref_date end) ref_date1, max(case when seqnum = 1 ref_amount end) ref_amount1, max(case when seqnum = 2 ref_date end) ref_date2, max(case when seqnum = 2 ref_amount end) ref_amount2 client c bring together (select r.*, row_number() on (partition cust_id order ref_date desc) seqnum refund r ) r on c.cust_id = r.cust_id seqnum <= 2 grouping c.cust_name; sql database oracle
No comments:
Post a Comment