Self-Join with aggregation and maybe pivot?

Hello -
I need to generate a result set that will be coming from a table that includes parent and child rows. The result needs to be one row per parent and include the child row information (some aggregation like min for a date, sum of amounts). And, also include rows for the child data.

The table contains sales data and there are times when an order is changed and generates some related orders. The need is to have the original parent order on one row, that includes any information about the child orders and also one to N rows for the child orders with their details.

I have tried to use a CTE to get the self join, but i keep getting a cartesian product. I am wondering if i will need to build out separate temp tables for the query...one for the parent rows, one for the child rows?

Here is some t-sql to generate some sample data:
/** setting up table and inserting data **/
declare @mytable table (Status_Code varchar(100), Region varchar(100), Order_Amount float, OrderNum varchar(100), Orig_OrderNum varchar(100), OrderComplete datetime, OrderCreate datetime )

Insert into @mytable
Select 'Complete', 'Southeast', 15000.00, '55544400', '55544400', '2016-11-11', '2016-11-11'
Union All
Select 'Change_Complete', 'Southeast', 2500.00, '5556700', '55544400', '2016-12-09', '2016-12-09'
Union All
Select 'Change_Complete', 'Southeast', 1000.00, '5558890', '55544400', '2016-12-20', '2016-12-20'
Union All
Select 'Change_Complete', 'Southeast', 4400.00, '5567700', '55544400', '2017-01-14', '2017-01-14'

Select * from @mytable

The results need to look like this;
OrderNum Order_Complete Order_Amount Child Order Date (min) Child Order Amount Child_order_Date_new Child_Order_Amt_New Parent_Order_Num
55544400 2016-11-11 15000 2016-12-09 7900 null null null
5556700 null null null null 2016-12-09 2500 55544400
5558890 null null null null 2016-12-20 1000 55544400
5567700 null null null null 2017-01-14 4400 55544400

Is the Order_Num column UNIQUE and could be used as a Primary Key?

Yes, the order_num is unique.

You need to identify which of the columns (above) go with the columns for the data, please. For example, what is the difference between the "Child Order Amount" and the "Child_Order_Amt_New"?

Hi

I tried to make sense of the Result Data
you put

Please let me know if I am correct or wrong
where I am making mistakes

Then we can start working on the SQL
to help you get what you want

Thanks

I am trying to understand Script

DECLARE @Result_mytable TABLE
(
ordernum VARCHAR(100),
ordercomplete DATETIME,
ordercreate DATETIME,
child_order_date_min DATETIME,
child_order_date_new DATETIME,
order_amount FLOAT,
child_order_amount FLOAT,
child_order_amt_new FLOAT,
orig_ordernum VARCHAR(100),
parent_order_num VARCHAR(100)
)

INSERT INTO @Result_mytable
(ordernum,
ordercreate,
order_amount,
child_order_date_min,
child_order_amount,
child_order_date_new,
parent_order_num,
orig_ordernum)
SELECT 5554440,
2016 - 11 - 11,
15000,
2016 - 12 - 09,
7900,
NULL,
NULL,
NULL

INSERT INTO @Result_mytable
(ordernum,
ordercreate,
order_amount,
child_order_date_min,
child_order_amount,
child_order_date_new,
parent_order_num,
orig_ordernum)
SELECT 5556700,
NULL,
NULL,
NULL,
2016 - 12 - 09,
2500,
555,
44400

INSERT INTO @Result_mytable
(ordernum,
ordercreate,
order_amount,
child_order_date_min,
child_order_amount,
child_order_date_new,
parent_order_num,
orig_ordernum)
SELECT 5558890,
NULL,
NULL,
NULL,
2016 - 12 - 20,
1000,
555,
44400

INSERT INTO @Result_mytable
(ordernum,
ordercreate,
order_amount,
child_order_date_min,
child_order_amount,
child_order_date_new,
parent_order_num,
orig_ordernum)
SELECT 5567700,
NULL,
NULL,
NULL,
2017 - 01 - 14,
4400,
555,
44400

SELECT *
FROM @Result_mytable

go

Hi all -
Thanks for the questions and comments. This is now resolved.

  • will

That's great, Will but, with the idea of this being a 2 way street, can you publish the code that worked for you? Thanks.