2011-07-28 14 views
1

を読み取るために、このクエリを簡単にすることができ、私は以下の大規模なクエリを持って、私は好きではないものがあります:どのように私は

  • 私はitem_tax_percent_regularを参照すると、クエリ内の別名としてitem_tax_percent_cumulativeすることはできません。
  • それだけでBIG IS(私はそれらを使用したい時はいつでも私は、大規模な和文を繰り返す必要があり)

クエリは、私が欲しいものを正確に行いますが、それはとても大きいです。エイリアスを作成する "として"

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を美人にすることです。例えば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

インデントはそれを実現します。リストの最後の行にコメントすると、クエリーを失敗させるダングリングカンマが残るので、私はそれらを先頭に置く傾向があります。 –