4

数学関数について少し助けが必要です。

次のクエリでは、次のlotように 2 行ずつ結果が返されます。

group | style | lot | section | q1 | q2 |q3  | q4  | ...

aaaaa | sssss | 123 | 111111  | 55 | 77 | 88 | 99 | ...

aaaaa | sssss | 123 | 222222  | 10 | 20 | 20 | 10 | ...

aaaaa | sssss | 321 | 111111  | 11 | 22 | 44 | 55 | ...

aaaaa | sssss | 321 | 222222  | 10 | 23 |33  | 10  | ...

lot結果 2 差分section code (2 行)

質問section code:列 q1 q2 q3 q4 q5 ...の 2 の間で減算を行うにはどうすればよいですか?

予想された結果:

group | style | lot | q1 | q2 |q3 | q4  | ...

aaaaa | sssss | 123 | 45 | 57 |68 | 89 | ...

aaaaa | sssss | 321 | 1  | -1 |11 | 45 | ...

これまでのクエリ:

SELECT DISTINCT gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot,
po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, po_lot.date_livraison,
po_lot_sp.qte_1, po_lot_sp.qte_2, po_lot_sp.qte_3, po_lot_sp.qte_4, po_lot_sp.qte_5,
po_lot_sp.qte_6, po_lot_sp.qte_7, po_lot_sp.qte_8, po_lot_sp.qte_9, po_lot_sp.qte_10,
po_lot_sp.qte_11, po_lot_sp.qte_12, po_lot_sp.qte_13, po_lot_sp.qte_14, po_lot_sp.qte_15,
po_lot_sp.qte_16, po_lot_sp.qte_17, po_lot_sp.qte_18, po_lot_sp.qte_19, po_lot_sp.qte_20,
po_lot_sp.qte_21, po_lot_sp.qte_22, po_lot_sp.qte_23, po_lot_sp.qte_24, po_lot_sp.qte_25,
po_lot_sp.qte_26, po_lot_sp.qte_27, po_lot_sp.qte_28, po_lot_sp.qte_29, po_lot_sp.qte_30

FROM po_lot_sp

LEFT OUTER JOIN po_lot_se ON po_lot_se.num_lot = po_lot_sp.num_lot 
and po_lot_se.num_secti = po_lot_sp.num_secti

LEFT OUTER JOIN po_lot ON po_lot.num_lot = po_lot_sp.num_lot

LEFT OUTER JOIN gp_style_gr ON gp_style_gr.num_style = po_lot.num_style

WHERE
((gp_style_gr.code_groupe = 'INSTOCK') and (po_lot.terminer = '0') 
and (po_lot_se.code_secti = '01')) or ((gp_style_gr.code_groupe = 'INSTOCK') 
and (po_lot.terminer = '0') and (po_lot_se.code_secti = '09'))

ORDER BY gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot, 
po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, po_lot.date_livraison, 

ありがとう !

4

3 に答える 3

2

例のようにセクション コードが何らかのパターンに従っている場合は、単純にテーブルをそれ自体に対して結合できます。

あなたの例のように、テーブルが呼び出されるふりをpo_lot_spします。

次のクエリでは、2 行目のセクション番号が大きいと想定しています。それが条件t1.section > t2.sectionです。そうでない場合は、適切に変更します。セクション番号がパターンに従っていない場合は、これを完全に無視してください。

SELECT t1.`group`, t1.style, t1.lot, t1.section,
  t2.q1 - t1.q1 q1, t2.q2 - t1.q2 q2, t2.q3 - t1.q3 q3, t2.q4 - t1.q4 q4
FROM t t1
JOIN t t2 ON t1.`group` = t2.`group` AND t1.style = t2.style AND
  t1.lot = t2.lot AND t1.section > t2.section

ここでフィドル。

于 2013-11-12T06:32:28.593 に答える
0

これを試して:

select a.group,a.style,a.lot, 
coalesce(a.q1 - 
(select b.q1 from  tablename b where b.ID = a.ID + 1), a.q1) as q1,  
coalesce(a.q2- 
(select b.q2 from  tablename b where b.ID = a.ID + 1), a.q2) as q2,
coalesce(a.q3- 
(select b.q3 from  tablename b where b.ID = a.ID + 1), a.q3) as q3,
coalesce(a.q4- 
(select b.q4 from  tablename b where b.ID = a.ID + 1), a.q4) as q4
from tablename a group by a.lot 

注:ここでIDは、テーブルの主キーをtablename参照し、元のテーブル名を参照します。そのため、フィールドを主キー フィールドに置き換え、IDテーブル名をその逆に置き換えます。

デモ

于 2013-11-12T06:42:31.817 に答える
0

これが私が考える最速の方法です。もちろん、これは、値が「111111」のセクションからデクリメントすることを前提としています。

SELECT `group`, style, lot,
  sum(if(section = '111111', q1, -q1)),
  sum(if(section = '111111', q2, -q2)),
  sum(if(section = '111111', q3, -q3)),
  sum(if(section = '111111', q4, -q4))
FROM t
GROUP BY `group`, style, lot

ここでフィドル。

ちなみに、group列名として使用しないようにしてください。予約語です。

減分したいセクションの値がわからず、最も低いセクションからだけ減分したい場合は、Andy のソリューションを使用してください。

于 2013-11-12T06:48:34.120 に答える