Microsoft Access 掲示板

クエリが複雑すぎます / 8

18 コメント
views
4 フォロー
8
名前なし 2020/04/03 (金) 10:16:46 6d87b@60c58

新たにデータベース、テーブルを作成し、レコードを数行入力して下記コードで実行してみましたが
10か月未満では表示できましたが、それを超えると表示できなくなってしまいました。

何か他に原因があるのでしょうか。

SELECT
 q.商品ID,
 Max(q.商品名) AS 商品名,
 Sum(IIf(月=-1,仕入数量)) AS 期首数量,
 Sum(IIf(月=-1,仕入金額)) AS 期首金額,
 Sum(IIf([月]=0,[仕入数量])) AS 7月仕入数量,
 Sum(IIf([月]=0,[仕入金額])) AS 7月仕入金額,
 Sum(IIf([月]=0,[売上数量])) AS 7月売上数量,
 ([期首金額]+[7月仕入金額])/([期首数量]+[7月仕入数量])*[7月売上数量] AS 7月払出金額,
 Sum(IIf([月]<=0,[仕入数量]-[売上数量])) AS 7月数量,
 [期首金額]+Nz(Sum(IIf([月]=0,[仕入金額])),0)-Nz([7月払出金額],0) AS 7月金額,

 Sum(IIf([月]=1,[仕入数量])) AS 8月仕入数量,
 Sum(IIf([月]=1,[仕入金額])) AS 8月仕入金額,
 Sum(IIf([月]=1,[売上数量])) AS 8月売上数量,
 ([7月金額]+[8月仕入金額])/([7月数量]+[8月仕入数量])*[8月売上数量] AS 8月払出金額,
 Sum(IIf([月]<=1,[仕入数量]-[売上数量])) AS 8月数量,
 [7月金額]+Nz(Sum(IIf([月]=1,[仕入金額])),0)-Nz([8月払出金額],0) AS 8月金額,

 Sum(IIf([月]=2,[仕入数量])) AS 9月仕入数量,
 Sum(IIf([月]=2,[仕入金額])) AS 9月仕入金額,
 Sum(IIf([月]=2,[売上数量])) AS 9月売上数量,
 ([8月金額]+[9月仕入金額])/([8月数量]+[9月仕入数量])*[9月売上数量] AS 9月払出金額,
 Sum(IIf([月]<=2,[仕入数量]-[売上数量])) AS 9月数量,
 [8月金額]+Nz(Sum(IIf([月]=2,[仕入金額])),0)-Nz([9月払出金額],0) AS 9月金額,

 Sum(IIf([月]=3,[仕入数量])) AS 10月仕入数量,
 Sum(IIf([月]=3,[仕入金額])) AS 10月仕入金額,
 Sum(IIf([月]=3,[売上数量])) AS 10月売上数量,
 ([9月金額]+[10月仕入金額])/([9月数量]+[10月仕入数量])*[10月売上数量] AS 10月払出金額,
 Sum(IIf([月]<=3,[仕入数量]-[売上数量])) AS 10月数量,
 [9月金額]+Nz(Sum(IIf([月]=3,[仕入金額])),0)-Nz([10月払出金額],0) AS 10月金額,

 Sum(IIf([月]=4,[仕入数量])) AS 11月仕入数量,
 Sum(IIf([月]=4,[仕入金額])) AS 11月仕入金額,
 Sum(IIf([月]=4,[売上数量])) AS 11月売上数量,
 ([10月金額]+[11月仕入金額])/([10月数量]+[11月仕入数量])*[11月売上数量] AS 11月払出金額,
 Sum(IIf([月]<=4,[仕入数量]-[売上数量])) AS 11月数量,
 [10月金額]+Nz(Sum(IIf([月]=4,[仕入金額])),0)-Nz([11月払出金額],0) AS 11月金額,

 Sum(IIf([月]=5,[仕入数量])) AS 12月仕入数量,
 Sum(IIf([月]=5,[仕入金額])) AS 12月仕入金額,
 Sum(IIf([月]=5,[売上数量])) AS 12月売上数量,
 ([11月金額]+[12月仕入金額])/([11月数量]+[12月仕入数量])*[12月売上数量] AS 12月払出金額,
 Sum(IIf([月]<=5,[仕入数量]-[売上数量])) AS 12月数量,
 [11月金額]+Nz(Sum(IIf([月]=5,[仕入金額])),0)-Nz([12月払出金額],0) AS 12月金額,

 Sum(IIf([月]=6,[仕入数量])) AS 1月仕入数量,
 Sum(IIf([月]=6,[仕入金額])) AS 1月仕入金額,
 Sum(IIf([月]=6,[売上数量])) AS 1月売上数量,
 ([12月金額]+[1月仕入金額])/([12月数量]+[1月仕入数量])*[1月売上数量] AS 1月払出金額,
 Sum(IIf([月]<=6,[仕入数量]-[売上数量])) AS 1月数量,
 [12月金額]+Nz(Sum(IIf([月]=6,[仕入金額])),0)-Nz([1月払出金額],0) AS 1月金額,

 Sum(IIf([月]=7,[仕入数量])) AS 2月仕入数量,
 Sum(IIf([月]=7,[仕入金額])) AS 2月仕入金額,
 Sum(IIf([月]=7,[売上数量])) AS 2月売上数量,
 ([1月金額]+[2月仕入金額])/([1月数量]+[2月仕入数量])*[2月売上数量] AS 2月払出金額,
 Sum(IIf([月]<=7,[仕入数量]-[売上数量])) AS 2月数量,
 [1月金額]+Nz(Sum(IIf([月]=7,[仕入金額])),0)-Nz([2月払出金額],0) AS 2月金額,

 Sum(IIf([月]=8,[仕入数量])) AS 3月仕入数量,
 Sum(IIf([月]=8,[仕入金額])) AS 3月仕入金額,
 Sum(IIf([月]=8,[売上数量])) AS 3月売上数量,
 ([2月金額]+[3月仕入金額])/([2月数量]+[3月仕入数量])*[3月売上数量] AS 3月払出金額,
 Sum(IIf([月]<=8,[仕入数量]-[売上数量])) AS 3月数量,
 [2月金額]+Nz(Sum(IIf([月]=8,[仕入金額])),0)-Nz([3月払出金額],0) AS 3月金額,

 Sum(IIf([月]=9,[仕入数量])) AS 4月仕入数量,
 Sum(IIf([月]=9,[仕入金額])) AS 4月仕入金額,
 Sum(IIf([月]=9,[売上数量])) AS 4月売上数量,
 ([3月金額]+[4月仕入金額])/([3月数量]+[4月仕入数量])*[4月売上数量] AS 4月払出金額,
 Sum(IIf([月]<=9,[仕入数量]-[売上数量])) AS 4月数量,
 [3月金額]+Nz(Sum(IIf([月]=9,[仕入金額])),0)-Nz([4月払出金額],0) AS 4月金額,


 Sum(IIf([月]=10,[仕入数量])) AS 5月仕入数量,
 Sum(IIf([月]=10,[仕入金額])) AS 5月仕入金額,
 Sum(IIf([月]=10,[売上数量])) AS 5月売上数量,
 ([4月金額]+[5月仕入金額])/([4月数量]+[5月仕入数量])*[5月売上数量] AS 5月払出金額,
 Sum(IIf([月]<=10,[仕入数量]-[売上数量])) AS 5月数量,
 [4月金額]+Nz(Sum(IIf([月]=10,[仕入金額])),0)-Nz([5月払出金額],0) AS 5月金額,


 Sum(IIf([月]=11,[仕入数量])) AS 6月仕入数量,
 Sum(IIf([月]=11,[仕入金額])) AS 6月仕入金額,
 Sum(IIf([月]=11,[売上数量])) AS 6月売上数量,
 ([5月金額]+[6月仕入金額])/([5月数量]+[6月仕入数量])*[6月売上数量] AS 6月払出金額,
 Sum(IIf([月]<=11,[仕入数量]-[売上数量])) AS 6月数量,
 [5月金額]+Nz(Sum(IIf([月]=11,[仕入金額])),0)-Nz([6月払出金額],0) AS 6月金額

FROM (SELECT
 商品ID,
 商品名,
 -1 AS 月,
 期首数量 AS 仕入数量,
 期首金額 AS 仕入金額,
 0 AS 売上数量,
 0 AS 売上金額
  FROM t商品マスタ, t決算日

  union all
  SELECT
 仕入商品ID,
 "",
 (month([仕入日])+5) mod 12,
 仕入数量,
 仕入単価*仕入数量,
 0,
 0
  FROM t仕入明細, t決算日
  WHERE 仕入日 Between DateAdd("yyyy",-1,[決算日]+1) And [決算日]

  union all
  SELECT
 売上商品ID,
 "",
 (month([売上日])+5) mod 12,
 0,
 0,
 売上数量,
 売上単価*売上数量
  FROM t売上明細, t決算日
  WHERE 売上日 Between DateAdd("yyyy",-1,[決算日]+1) And [決算日]
)  AS q
GROUP BY q.商品ID;
通報 ...