HeelNGville
Registered User.
- Local time
- Today, 06:44
- Joined
- Apr 13, 2004
- Messages
- 71
I preface this with many thanks for assistance. High level, I have a table source (sales) that houses both sale and return activity. I have 2 separate queries (a)qry_sales to retrieve sales and (b) qry_ret to retrieve returns by day and product the current year and previous yeard and aggregating results by day. As mentioned above have 2 separate queries retrieving this info, however really need to try to merge these 2 queries into a single query result if possible.
My SQLs:
Sales(summarizes product sales by day and date is driven by field sale_day)
SELECT
to_char(sh.sale_day,'DDD') as Day_No,
to_char(sh.sale_day,'MON-DD')as sales_day,
sk.prd_id,
---Get Sale Units
sum(case when sh.line_status in ('S','D') and sh.sale_day between '2018-
01-01' and current_date then sh.sale_qty else 0 end) as cy_sale_units,
sum(case when sh.line_status in ('S','D') and sh.sales_day between
'2017-01-01' and current_date-366 then sh.sale_qty else 0 end) as
py_sale_units,
--Get Sales Dollars
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2018-
01-01' and current_date then sh.sale_qty*sh.unit_price else 0 end) as
cy_sale_dollars,
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2017-
01-01' and current_date-366 then sh.sale_qty*sh.unit_price else 0 end) as
py_sale_dollars,
--Get Sales Cost
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2018-
01-01' and current_date then sh.sale_qty*sh.unit_cost else 0 end) as
cy_sale_cost,
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2017-
01-01' and current_date-366 then sh.sale_qty*sh.unit_cost else 0 end) as
py_sale_cost
FROM
sales sh
INNER JOIN prod sk
ON trim(sk.prd_id) = trim(sh.prd_id)
INNER JOIN buyer_type ba
ON trim (ba.buyer_area) = trim(sk.buyer_area)
WHERE
sh.sales_day between '2017-01-01' And current_date-1
AND sh.line_status in ('S','D')
AND sh.unit_price >0
AND ba.buyer_code_type is not null
Group By
to_char(sh.sales_day,'DDD'),
to_char(sh.sales_day,'MON-DD'),
sk.skn_nbr
Order By
to_char(sh.sales_day,'DDD')
Returns (summarizes product returns by day and date is driven by field ret_date)
SELECT
to_char(sh.ret_date,'DDD') as Day_No,
to_char(sh.ret_date,'MON-DD')as ret_day,
sk.prd_id,
--Get Return Units
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty else 0 end) as cy_ret_units,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty else 0 end) as py_ret_units,
--Get Return Sales
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty*sh.unit_price else 0 end) as
cy_ret_dollars,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty*sh.unit_price else 0 end) as
py_ret_dollars,
--Get Return Cost
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty*sh.unit_cost else 0 end) as
cy_ret_cost,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty*sh.unit_cost else 0 end) as
py_ret_cost
FROM
sales sh
INNER JOIN prod sk
ON trim(sk.prd_id) = trim(sh.prd_id)
INNER JOIN buyer_type ba
ON trim (ba.buyer_area) = trim(sk.buyer_area)
WHERE
sh.ret_date between '2017-01-01' And current_date-1
AND sh.line_status in ('R')
AND sh.unit_price >0
AND ba.buyer_code_type is not null
Group By
to_char(sh.ret_date,'DDD'),
to_char(sh.ret_date,'MON-DD'),
sk.skn_nbr
Order By
to_char(sh.ret_date,'DDD')
Basically, I am requesting assistance on how to combine the 2 SQL above to yield a single result. I have attached an image of result examples from the 2 statements above, along with the desired results I am attempting to achieve. Thanks for the time.
My SQLs:
Sales(summarizes product sales by day and date is driven by field sale_day)
SELECT
to_char(sh.sale_day,'DDD') as Day_No,
to_char(sh.sale_day,'MON-DD')as sales_day,
sk.prd_id,
---Get Sale Units
sum(case when sh.line_status in ('S','D') and sh.sale_day between '2018-
01-01' and current_date then sh.sale_qty else 0 end) as cy_sale_units,
sum(case when sh.line_status in ('S','D') and sh.sales_day between
'2017-01-01' and current_date-366 then sh.sale_qty else 0 end) as
py_sale_units,
--Get Sales Dollars
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2018-
01-01' and current_date then sh.sale_qty*sh.unit_price else 0 end) as
cy_sale_dollars,
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2017-
01-01' and current_date-366 then sh.sale_qty*sh.unit_price else 0 end) as
py_sale_dollars,
--Get Sales Cost
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2018-
01-01' and current_date then sh.sale_qty*sh.unit_cost else 0 end) as
cy_sale_cost,
sum(case when sh.line_status in ('S','D') and sh.sales_day between '2017-
01-01' and current_date-366 then sh.sale_qty*sh.unit_cost else 0 end) as
py_sale_cost
FROM
sales sh
INNER JOIN prod sk
ON trim(sk.prd_id) = trim(sh.prd_id)
INNER JOIN buyer_type ba
ON trim (ba.buyer_area) = trim(sk.buyer_area)
WHERE
sh.sales_day between '2017-01-01' And current_date-1
AND sh.line_status in ('S','D')
AND sh.unit_price >0
AND ba.buyer_code_type is not null
Group By
to_char(sh.sales_day,'DDD'),
to_char(sh.sales_day,'MON-DD'),
sk.skn_nbr
Order By
to_char(sh.sales_day,'DDD')
Returns (summarizes product returns by day and date is driven by field ret_date)
SELECT
to_char(sh.ret_date,'DDD') as Day_No,
to_char(sh.ret_date,'MON-DD')as ret_day,
sk.prd_id,
--Get Return Units
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty else 0 end) as cy_ret_units,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty else 0 end) as py_ret_units,
--Get Return Sales
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty*sh.unit_price else 0 end) as
cy_ret_dollars,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty*sh.unit_price else 0 end) as
py_ret_dollars,
--Get Return Cost
sum(case when sh.line_status in ('R') and sh.ret_date between '2018-01-
01' and current_date then sh.return_qty*sh.unit_cost else 0 end) as
cy_ret_cost,
sum(case when sh.line_status in ('R') and sh.ret_date between '2017-01-
01' and current_date-366 then sh.return_qty*sh.unit_cost else 0 end) as
py_ret_cost
FROM
sales sh
INNER JOIN prod sk
ON trim(sk.prd_id) = trim(sh.prd_id)
INNER JOIN buyer_type ba
ON trim (ba.buyer_area) = trim(sk.buyer_area)
WHERE
sh.ret_date between '2017-01-01' And current_date-1
AND sh.line_status in ('R')
AND sh.unit_price >0
AND ba.buyer_code_type is not null
Group By
to_char(sh.ret_date,'DDD'),
to_char(sh.ret_date,'MON-DD'),
sk.skn_nbr
Order By
to_char(sh.ret_date,'DDD')
Basically, I am requesting assistance on how to combine the 2 SQL above to yield a single result. I have attached an image of result examples from the 2 statements above, along with the desired results I am attempting to achieve. Thanks for the time.