Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL访问-将行除以一段时间内的特定总数_Sql_Ms Access - Fatal编程技术网

SQL访问-将行除以一段时间内的特定总数

SQL访问-将行除以一段时间内的特定总数,sql,ms-access,Sql,Ms Access,我有一个SQL数据库,通过Access连接到该数据库 数据结构如下所示: Date ELEMENT VALUE 01-2010 ElementA 12 01-2010 ElementB 43 01-2010 ElementC 53 01-2010 ElementD 24 01-2010 El_Divisor 120 02-2010 ElementA 54 02-2010 ElementB 32 02-2010 ElementC 48 02

我有一个SQL数据库,通过Access连接到该数据库

数据结构如下所示:

Date    ELEMENT     VALUE
01-2010 ElementA    12
01-2010 ElementB    43
01-2010 ElementC    53
01-2010 ElementD    24
01-2010 El_Divisor  120
02-2010 ElementA    54
02-2010 ElementB    32
02-2010 ElementC    48
02-2010 ElementD    21
02-2010 El_Divisor  130
etc etc....
对于每个时间点,我需要得到每个元素除以ElementDivisor,即

Date    ELEMENT     VALUE
01-2010 ElementA    = 12 / 120
01-2010 ElementB    = 43 / 120
01-2010 ElementC    = 53 / 120
01-2010 ElementD    = 24 / 120
01-2010 El_Divisor  = 120 / 120 = 1
02-2010 ElementA    = 54 / 130
02-2010 ElementB    = 32 / 130
02-2010 ElementC    = 48 / 130
02-2010 ElementD    = 21 / 130
02-2010 El_Divisor  = 130 / 130 = 1
etc etc....
注:El_除数不是所有其他元素的总和。 目前,我正在使用一个非常不优雅的多交叉表系统,这是非常次优的

有人有主意吗? 其他SQL程序中的想法也非常受欢迎


非常感谢

不确定这是否适用于Access,可能必须将内部选择放入视图中:

Select
  v.[Date],
  v.Element,
  v.[Value] / d.[Value] As [Value]
From (
  Select
    e.[Date], e.[Value]
  From
    Elements e
  Where
    e.Element = 'El_Divisor'
 ) As d Inner Join (
   Select
     e.[Date], e.Element, e.[Value]
   From
     Elements e
   Where
     e.Element <> 'El_Divisor' -- take this out if you want to see the El_Divisor/El_Divisor row
 ) as v
   On d.[Date] = v.[Date]

编辑-根据Remou的建议修复

我将使用此解决方案:

Select
  tbl_Elements.[Date],
  tbl_Elements.Element,
  tbl_Elements.[Value] / tbl_Elements_1.[Value]
From
  tbl_Elements
    INNER JOIN
  tbl_Elements tbl_Elements_1
    ON tbl_Elements.Date = tbl_Elements_1.Date
Where
  tbl_Elements.Element <> "El_Divisor"
  AND tbl_Elements_1.Element = "El_Divisor"

如果你想看到除数被自己除掉,你可以删除tbl_元素。Element El_除数。

所以基本上每个月/年对都会有一系列的元素,并且有一个匹配的除数记录?对我来说很适合访问,只是日期和值是保留字,所以[date]、[value]或d.date和e.Element!='El_Divisor’应该是e。元素'El_Divisor'@Remou-谢谢,修复了这个问题,注意到我在外部选择中使用了错误的别名。测试成功!稍微简单一点,我将使用这个。谢谢大家,非常感谢!