2017-06-19 2 views
0

おかげで..私はの合計を取得するために上記のコードを使用していますSQLグループは

SELECT width, thickness, lengthfeet, lengthinch, Sum(nos), Sum(areasqft) 
FROM materialsdetails 
WHERE materialcode='" & Me.txtMaterialCode.Text & "' 
GROUP BY width, thickness, lengthfeet, lengthinch 
ORDER BY width, thickness, lengthfeet, lengthinch 

、フィールドの番号 'とグループによる「areasqft」。

「トランザクションスルー」の列にある(画像をご覧ください)enter image description hereデータとして「購入」、「問題」、「返品」、「損害」を入力しました。クエリ内の同じGroup byフィールドを持つダメージ - 号+リターン -

実際には、代わりにSum(nos)Sum(areasqft)の私はちょうど 購入として値を必要とします。

+0

エラーは何ですか? – Minh

答えて

0

合計内の以下のifステートメントを使用して、問題と損害の値を無効にすることができます。 TransactionThroughによって

SELECT width 
     ,thickness 
     ,lengthfeet 
     ,lengthinch 
     ,SUM(nos*iif(or(transactionthrough="Issue",transactionthrough="Damage"),-1,1)) 
     ,SUM(areasqft*iif(or(transactionthrough="Issue",transactionthrough="Damage"),-1,1)) 
FROM materialsdetails 
WHERE materialcode = '" & Me.txtMaterialCode.Text & "' 
GROUP BY width 
     ,thickness 
     ,lengthfeet 
     ,lengthinch 
ORDER BY width 
     ,thickness 
     ,lengthfeet 
     ,lengthinch 
0

まず、グループ:

create view byTT as 
    SELECT 
    transactionthrough, 
    sum(width) as width, 
    sum(thickness) as thickness, 
    -- etc for each column you care about 
    FROM materialsdetails 
    WHERE -- whatever where clause you want 
    group by transactionthrough; 

その後否定:

create view negByTT as 
select 
transactionthrough, 
-width as width, 
-thickness as thickness 
-- and for each other column 
from byTT; 

を次に適切な記号で、あなたが気にトランザクションタイプによって合計:

select sum(width), sum(thickness) from (
    select width, thickness 
    from byTT where transactionthrough = "Purchase" 
    or transactionthrough = "Return" 
    union select width, thickness 
    from negByTT where transactionthrough = "Issue" 
    or transactionthrough = "Damage" 
);