我下面有一个大量的问题,我不喜欢的是:
查询完全符合我的要求,但它太大了。
CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_items.sale_id, comment,payment_type, customer_id, employee_id, phppos_items.item_id, NULL as item_kit_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, category,
SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END) as item_tax_percent_regular,
SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END) as item_tax_percent_cumulative, discount_percent,
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_items.line as line, serialnumber, phppos_sales_items.description as description,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100),2) as total,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax,
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit FROM phppos_sales_items INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id
LEFT OUTER JOIN phppos_suppliers ON phppos_items.supplier_id=phppos_suppliers.person_id
LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28"
GROUP BY sale_id, item_id, line)
UNION ALL
(SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_item_kits.sale_id, comment,payment_type, customer_id, employee_id, NULL as item_id, phppos_item_kits.item_kit_id, '' as supplier_id, quantity_purchased, item_kit_cost_price, item_kit_unit_price, category,
SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END) as item_kit_tax_percent_regular, SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END) as item_kit_tax_percent_cumulative, discount_percent, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) as subtotal,
phppos_sales_item_kits.line as line, '' as serialnumber, phppos_sales_item_kits.description as description, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)+ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100),2) as total,
ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax,
(item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) - (item_kit_cost_price*quantity_purchased) as profit FROM phppos_sales_item_kits
INNER JOIN phppos_sales ON phppos_sales_item_kits.sale_id=phppos_sales.sale_id INNER JOIN phppos_item_kits ON phppos_sales_item_kits.item_kit_id=phppos_item_kits.item_kit_id
LEFT OUTER JOIN phppos_sales_item_kits_taxes ON phppos_sales_item_kits.sale_id=phppos_sales_item_kits_taxes.sale_id and phppos_sales_item_kits.item_kit_id=phppos_sales_item_kits_taxes.item_kit_id and phppos_sales_item_kits.line=phppos_sales_item_kits_taxes.line WHERE date(sale_time)
BETWEEN "2011-07-28" and "2011-07-28"
GROUP BY sale_id, item_kit_id, line) ORDER BY sale_id, line
答案 0 :(得分:2)
使用“as”创建别名:
...
LEFT OUTER JOIN phppos_sales_item_kits_taxes AS sikTaxes ON sikTaxes.sale_id=phppos_sales.sale_id
...
您可以使用在整个查询中创建的别名。
将一些算法运用到用户定义的函数中也可能会有所帮助。
答案 1 :(得分:1)
这个怎么样?我已经结合了一些换行格式来协助。我将操作符放在下一行(例如逗号分隔或减法)以便于评论:
CREATE TEMPORARY TABLE phppos_sales_items_temp
(
SELECT phppos_sales.deleted as deleted
, date(sale_time) as sale_date
, phppos_sales_items.sale_id
, comment
, payment_type
, customer_id
, employee_id
, phppos_items.item_id
, NULL as item_kit_id
, supplier_id
, quantity_purchased
, item_cost_price
, item_unit_price
, category
, SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) as item_tax_percent_regular
, SUM(CASE
WHEN cumulative = 1
THEN percent
ELSE 0
END) as item_tax_percent_cumulative
, discount_percent
, (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) as subtotal
, phppos_sales_items.line as line
, serialnumber
, phppos_sales_items.description as description
, ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
+ ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
+ (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ ((ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100))
* (SUM(CASE
WHEN cumulative = 1
THEN percent
ELSE 0
END)) / 100), 2) as total
, ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ ((ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent/100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100))
* (SUM(CASE
WHEN cumulative = 1
THEN percent
ELSE 0
END)) / 100) as tax
, (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
- (item_cost_price * quantity_purchased) as profit
FROM phppos_sales_items
INNER JOIN phppos_sales ON phppos_sales_items.sale_id = phppos_sales.sale_id
INNER JOIN phppos_items ON phppos_sales_items.item_id = phppos_items.item_id
LEFT OUTER JOIN phppos_suppliers
ON phppos_items.supplier_id = phppos_suppliers.person_id
LEFT OUTER JOIN phppos_sales_items_taxes
ON phppos_sales_items.sale_id = phppos_sales_items_taxes.sale_id
AND phppos_sales_items.item_id = phppos_sales_items_taxes.item_id
AND phppos_sales_items.line = phppos_sales_items_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28"
GROUP BY sale_id
, item_id
, line
)
UNION ALL
(
SELECT phppos_sales.deleted as deleted
, date(sale_time) as sale_date
, phppos_sales_item_kits.sale_id
, comment
, payment_type
, customer_id
, employee_id
, NULL as item_id
, phppos_item_kits.item_kit_id
, '' as supplier_id
, quantity_purchased
, item_kit_cost_price
, item_kit_unit_price
, category
, SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) as item_kit_tax_percent_regular
, SUM(CASE
WHEN cumulative = 1
THEN percent
ELSE 0
END) as item_kit_tax_percent_cumulative
, discount_percent
, (item_kit_unit_price * quantity_purchased-item_kit_unit_price * quantity_purchased * discount_percent / 100) as subtotal
, phppos_sales_item_kits.line as line
, '' as serialnumber
, phppos_sales_item_kits.description as description
, ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
+ ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ ((ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100))
* (SUM(CASE WHEN
cumulative = 1
THEN percent
ELSE 0
END)) / 100), 2) as total
, ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ ((ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
* (SUM(CASE
WHEN cumulative != 1
THEN percent
ELSE 0
END) / 100), 2)
+ (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100))
* (SUM(CASE
WHEN cumulative = 1
THEN percent
ELSE 0
END)) / 100) as tax
, (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
- (item_kit_cost_price * quantity_purchased) as profit
FROM phppos_sales_item_kits
INNER JOIN phppos_sales
ON phppos_sales_item_kits.sale_id = phppos_sales.sale_id
INNER JOIN phppos_item_kits
ON phppos_sales_item_kits.item_kit_id = phppos_item_kits.item_kit_id
LEFT OUTER JOIN phppos_sales_item_kits_taxes
ON phppos_sales_item_kits.sale_id = phppos_sales_item_kits_taxes.sale_id
AND phppos_sales_item_kits.item_kit_id = phppos_sales_item_kits_taxes.item_kit_id
AND phppos_sales_item_kits.line = phppos_sales_item_kits_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28"
GROUP BY sale_id
, item_kit_id
, line
)
ORDER BY sale_id
, line