2011-07-28 150 views
1

下面我有一个巨大的查询,我不喜欢的事情是:我怎样才能让这个查询更容易阅读

  • 我不能引用item_tax_percent_regular和item_tax_percent_cumulative作为查询中的别名。 (我不得不重复大量的总和发言时,我想使用它们)
  • 它只是BIG

查询不正是我想要的,但它是如此之大。

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 
+5

一些换行,将有助于 – Jacob

+0

绝对的换行符和缩进。 – Josh

+0

一个简单的解决方案,让你开始会减少您的SQL成beautifer。例如http://www.dpriver.com/pp/sqlformat.htm –

回答

2

使用 “为” 创建别名:

... 
LEFT OUTER JOIN phppos_sales_item_kits_taxes AS sikTaxes ON sikTaxes.sale_id=phppos_sales.sale_id 
... 

你可以使用你在整个查询创建别名。

猛拉一些算术到用户自定义函数也可能有帮助。

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 
+0

当我继续在多行的声明,我总是想结束与一些不完整的一条线,这样读者看起来到下一行。运算符或逗号将在下一行中与目标进行“内部连接”。 – HABO

+0

缩进完成。谈到最后一行在列表中会留下一个悬空逗号导致查询失败,所以我倾向于把他们在前面。 –