我有以下查询,虽然它达到了预期的结果,但它是一个冗长的查询,请告诉我是否有其他方法可以重组我的查询.. 我想将此长查询减少到更小的形式
SELECT final_lines.line_number line_number,
final_lines.line_desc line_desc,
Sum(final_lines.value) value
FROM (SELECT main.line_code line_number,
main.line_desc line_desc,
( COALESCE(main.net_sales, 0)
+ COALESCE(paid_out.paidout_amt, 0) ) value
FROM (SELECT t.line_num line_num,
t.line_code line_code,
t.line_desc line_desc,
Sum(t.net_sales) net_sales
FROM (SELECT pstl.prgp_id AS line_num,
line.prgp_code AS line_code,
line.prgp_desc AS line_desc,
COALESCE(Sum(txn.txn_sales), 0) AS Net_Sales
FROM prodgp_str_link pstl
JOIN prodgp_structure pstr
ON pstl.pstr_id = pstr.pstr_id
JOIN product_group line
ON line.prgp_id = pstl.prgp_id
LEFT JOIN prod_pgrp_link ppgl
ON pstl.prgp_child_id = ppgl.prgp_id
LEFT JOIN product prd
ON prd.prod_id = ppgl.prod_id
LEFT JOIN (SELECT dtl.txde_sku_code sku_code,
CASE
WHEN dtl.txde_level5_id IN (
'D100-5', 'D100-4',
'D100-7',
'D100-6',
'D11-11', 'D11-1',
'D5-1',
'D5-5',
'D100-10', 'D100-11',
'D100-69'
, 'D100-9'
,
'D100-100', 'D5-69',
'D100-8',
'D11-69',
'D100-1', 'D100-3',
'D100-2' )
THEN
COALESCE (Sum(
CASE
WHEN dtl.txde_line_refund
=
0 THEN
dtl.txde_price_sold *
dtl.txde_quantity_sold
ELSE dtl.txde_price_sold *
dtl.txde_quantity_sold *- 1
END), 0)
ELSE COALESCE(Sum(CASE
WHEN dtl.txde_line_refund = 0
THEN
dtl.txde_value_line
ELSE dtl.txde_value_line *- 1
END), 0)
END txn_sales
FROM txn_detail dtl
JOIN txn_header hdr
ON dtl.orgu_code_cmpy = hdr.orgu_code_cmpy
AND dtl.orgu_code = hdr.orgu_code
AND dtl.till_short_desc = hdr.till_short_desc
AND dtl.txhd_txn_nr = hdr.txhd_txn_nr
JOIN txn_header_wag txhw
ON hdr.orgu_code_cmpy = txhw.orgu_code_cmpy
AND hdr.orgu_code = txhw.orgu_code
AND hdr.till_short_desc = txhw.till_short_desc
AND hdr.txhd_txn_nr = txhw.txhd_txn_nr
WHERE hdr.txhd_txn_type IN ( 3, 8 )
AND dtl.txde_detail_type IN (
'1', '2', '3', '201',
'202', '203', '204', '205',
'206', '207', '208', '210',
'211' )
AND txhw.txhw_processed_date = '20121112'
AND hdr.txhd_state = 0
AND hdr.txhd_training_mode = 0
AND hdr.txhd_voided = 0
AND dtl.txde_item_void = 0
GROUP BY dtl.txde_sku_code,
dtl.txde_level5_id) txn
ON prd.prod_sku = txn.sku_code
WHERE pstl.pstr_id = 3
AND pstr.pstr_is_live = 1
AND pstl.prgp_parent_id = 0
AND line.prgp_code IN ( '30', '50', '60' )
GROUP BY pstl.prgp_id,
line.prgp_code,
line.prgp_desc
UNION ALL
SELECT pstl.prgp_id AS line_num,
line.prgp_code AS line_code,
line.prgp_desc AS line_desc,
COALESCE(Sum(txn5.txn_sales), 0) AS Net_Sales
FROM prodgp_str_link pstl
JOIN prodgp_structure pstr
ON pstl.pstr_id = pstr.pstr_id
JOIN product_group line
ON line.prgp_id = pstl.prgp_id
LEFT JOIN product_group child
ON pstl.prgp_child_id = child.prgp_id
LEFT JOIN (SELECT
dtl.txde_level5_id level5id,
CASE
WHEN dtl.txde_level5_id IN (
'D100-5', 'D100-4', 'D100-7',
'D100-6',
'D11-11', 'D11-1', 'D5-1',
'D5-5',
'D100-10', 'D100-11', 'D100-69'
, 'D100-9'
,
'D100-100', 'D5-69', 'D100-8',
'D11-69',
'D100-1', 'D100-3', 'D100-2' )
THEN
COALESCE (Sum(CASE
WHEN dtl.txde_line_refund = 0 THEN
dtl.txde_price_sold *
dtl.txde_quantity_sold
ELSE dtl.txde_price_sold *
dtl.txde_quantity_sold *- 1
END), 0)
ELSE COALESCE(Sum(CASE
WHEN dtl.txde_line_refund = 0
THEN
dtl.txde_value_line
ELSE dtl.txde_value_line *- 1
END), 0)
END txn_sales
FROM txn_detail dtl
JOIN txn_header hdr
ON dtl.orgu_code_cmpy = hdr.orgu_code_cmpy
AND dtl.orgu_code = hdr.orgu_code
AND dtl.till_short_desc = hdr.till_short_desc
AND dtl.txhd_txn_nr = hdr.txhd_txn_nr
LEFT JOIN txn_taxes tax
ON dtl.orgu_code_cmpy = tax.orgu_code_cmpy
AND dtl.orgu_code = tax.orgu_code
AND dtl.till_short_desc =
tax.till_short_desc
AND dtl.txhd_txn_nr = tax.txhd_txn_nr
AND dtl.txde_detail_nr = tax.txtx_detail_nr
JOIN txn_header_wag txhw
ON hdr.orgu_code_cmpy = txhw.orgu_code_cmpy
AND hdr.orgu_code = txhw.orgu_code
AND hdr.till_short_desc = txhw.till_short_desc
AND hdr.txhd_txn_nr = txhw.txhd_txn_nr
WHERE hdr.txhd_txn_type IN ( 3, 8 )
AND dtl.txde_level5_id IS NOT NULL
AND dtl.txde_sku_code IS NULL
AND dtl.txde_detail_type IN (
'1', '2', '3', '201',
'202', '203', '204', '205',
'206', '207', '208', '210',
'211' )
AND txhw.txhw_processed_date = '20111212'
AND hdr.txhd_state = 0
AND hdr.txhd_training_mode = 0
AND hdr.txhd_voided = 0
AND dtl.txde_item_void = 0
GROUP BY dtl.txde_level5_id) txn5
ON child.prgp_code = txn5.level5id
WHERE pstl.pstr_id = 3
AND pstr.pstr_is_live = 1
AND pstl.prgp_parent_id = 0
AND line.prgp_code IN ( '30', '50', '60' )
GROUP BY pstl.prgp_id,
line.prgp_code,
line.prgp_desc) t
GROUP BY t.line_num,
t.line_code,
t.line_desc
ORDER BY t.line_code) main
LEFT JOIN (SELECT t.line_code line_code,
t.line_desc line_desc,
Sum(t.net_sales) paidout_amt
FROM (SELECT pstl.prgp_id AS
line_num,
line.prgp_code AS
line_code,
line.prgp_desc AS
line_desc,
COALESCE(Sum(txn.txn_sales), 0) AS
Net_Sales
FROM prodgp_str_link pstl
JOIN prodgp_structure pstr
ON pstl.pstr_id = pstr.pstr_id
JOIN product_group line
ON line.prgp_id = pstl.prgp_id
LEFT JOIN prod_pgrp_link ppgl
ON pstl.prgp_child_id =
ppgl.prgp_id
LEFT JOIN product prd
ON prd.prod_id = ppgl.prod_id
LEFT JOIN (SELECT dtl.txde_sku_code
sku_code
,
COALESCE(
Sum(hdr.txhd_value_nett), 0)
txn_sales
FROM txn_detail dtl
JOIN txn_header hdr
ON
dtl.orgu_code_cmpy =
hdr.orgu_code_cmpy
AND
dtl.orgu_code =
hdr.orgu_code
AND
dtl.till_short_desc
=
hdr.till_short_desc
AND
dtl.txhd_txn_nr =
hdr.txhd_txn_nr
JOIN txn_header_wag
txhw
ON
hdr.orgu_code_cmpy =
txhw.orgu_code_cmpy
AND
hdr.orgu_code =
txhw.orgu_code
AND
hdr.till_short_desc
=
txhw.till_short_desc
AND hdr.txhd_txn_nr =
txhw.txhd_txn_nr
WHERE hdr.txhd_txn_type = 10
AND dtl.txde_detail_type IN (
'1', '2', '3', '201',
'202', '203', '204', '205',
'206', '207', '208', '210',
'211' )
AND txhw.txhw_processed_date =
'20111212'
AND hdr.txhd_state = 0
AND hdr.txhd_training_mode = 0
AND hdr.txhd_voided = 0
AND dtl.txde_item_void = 0
GROUP BY dtl.txde_sku_code) txn
ON prd.prod_sku = txn.sku_code
WHERE pstl.pstr_id = 3
AND pstr.pstr_is_live = 1
AND pstl.prgp_parent_id = 0
AND line.prgp_code IN ( '30', '50', '60' )
GROUP BY pstl.prgp_id,
line.prgp_code,
line.prgp_desc) t
GROUP BY t.line_num,
t.line_code,
t.line_desc
ORDER BY t.line_code) paid_out
ON main.line_code = paid_out.line_code) final_lines
GROUP BY line_number,
line_desc