2016-11-29 11 views
0

年に毎月の従業員給与の割引または動機付けのためのテーブルがあります。この表には、検索を使用して "Discount"または "Motivation"という単語のみを含むことができるフィールドが含まれています。アクセスクエリで複数の行のテキスト値を1つのセルに結合する方法

毎月私は複数のディスカウントやモチベーションを追加することができますので、月末に給料を支払う場合は、これらのデータを各従業員ごとに1行にまとめてください。

これは、その月に割引とモチベーションをとる表です。テーブル名が表1である: enter image description here

目的のクエリ結果: enter image description here

+0

こちらをご覧ます。http:// STAC koverflow.com/questions/19478272/converting-mysql-code-to-access-group-concat-and-a-triple-join –

答えて

0

1つの例外を除いて、次のクエリでは、ご希望の結果を提供します。ただし、Total_DiscountTotal_Motivationの列は通貨としてフォーマットされていません(フォーマットされた列は数値ではなく文字列になるため、望ましくない場合があります)。

SELECT t.The_Year 
    , t.The_Month 
    , t.Emp_Num 
    , Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0) AS Total_Discount 
    , ConcatRelated("Reason","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Discount_Reasons 
    , Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0) AS Total_Motivation 
    , ConcatRelated("Reason","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Motivation_Reasons 
FROM Table1 t 
GROUP BY t.The_Year, t.Emp_Num, t.The_Month 
ORDER BY t.The_Year, t.Emp_Num, t.The_Month 
; 

unformatted


、このようなフォーマットは、本当に必要な場合しかし、あなたは単にFormat機能でこれらの列の行をラップし、2番目の引数として「通貨」を提供することで、そうすることができます。

SELECT t.The_Year 
    , t.The_Month 
    , t.Emp_Num 
    , Format(Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0), "Currency") AS Total_Discount 
    , ConcatRelated("Reason","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Discount_Reasons 
    , Format(Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0), "Currency") AS Total_Motivation 
    , ConcatRelated("Reason","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Motivation_Reasons 
FROM Table1 t 
GROUP BY t.The_Year, t.Emp_Num, t.The_Month 
ORDER BY t.The_Year, t.Emp_Num, t.The_Month 
; 

formatted

+0

本当にありがとう、本当にありがとう、あなたは最高です:) –

関連する問題