访问SQL交叉表功能SELECT TOP 1

时间:2018-09-18 11:01:04

标签: sql ms-access crosstab

我正在基于使用从表中读取的校正因子校正的施用量来计算浪费。带有一些示例数据的表的结构如下:

RMP管理:

Nuclide      Product          MBq      Date Given
-------------------------------------------------
Tc-99m       Pertechnetate    700      2018/01/01
I-131        NaI              399      2018/02/01
I-131        NaI              555      2018/01/01
I-123        MIBG             181      2018/01/01
I-123        NaI              29       2018/01/03

废物因素

Nuclide    Product     MinActivity     MaxActivity    Factor
------------------------------------------------------------
Tc-99m     *                                          0.3
I-123      *                           150            0.3
I-123      *           150                            1
I-123      MIBG                                       0.6
I-131      *           400                            0.5
I-131      *                           400            1

因此,此表很复杂,但这是我想到的代表表中校正因子的最佳方法。首先匹配核素,然后如果乘积与使用的校正因子匹配,最后我们根据最小/最大列检查活性(MBq)以确定。然后,我们将这个因素与活动一起使用,以使用以下SQL来确定浪费

SELECT
  Nuclide,
  [Date Given] AS Given,
  (SELECT TOP 1
    Factor
  FROM WasteFactors
  WHERE [RMP Administrations].Nuclide = WasteFactors.Nuclide
  AND [RMP Administrations].Product LIKE WasteFactors.Product
  AND (WasteFactors.MinActivity IS NULL
  OR WasteFactors.MinActivity > [RMP Administrations].MBq)
  AND (WasteFactors.MaxActivity IS NULL
  OR WasteFactors.MaxActivity <= [RMP Administrations].MBq)
  ORDER BY WasteFactors.Nuclide ASC, WasteFactors.Product DESC)
  AS Waste
FROM [RMP Administrations] WHERE NOT [RMP Administrations].Nuclide IS NULL AND NOT [RMP Administrations].MBq IS NULL

因此,通过对“因子”表进行排序,从而使具有产品名称的因子出现在适用于所有其他产品的因子之前,从而达到了我们的要求,因此,对“ I-123 MIBG”以上的数据在“ I-123 *”之前进行了检查'。

因此,将此SQL与上面的数据一起运行应返回以下内容:

Nuclide   Given          Waste
--------------------------------------------------------
Tc-99m    2018/01/01     0.3         (All Tc-99m is 0.3)
I-131     2018/02/01     1           (Activity <=400)
I-131     2018/01/01     0.5         (Activity >400)
I-123     2018/01/01     0.6         (Product is MIBG)
I-123     2018/01/03     0.3         (Not MIBG, <150)

,该因子在实际代码中用作MBq * (SELECT TOP 1...) AS Waste。所以...这很好并且我可以每年按常规SUM(Waste)GROUP BY NuclideWHERE Year(Given)=[Enter Year]进行数据汇总。当我尝试在以下交叉表查询中使用它时,我的问题开始了:

PARAMETERS [Enter Year] Short;
TRANSFORM SUM(T.MBq *
  (SELECT TOP 1
    Factor
  FROM WasteFactors
  WHERE T.Nuclide = WasteFactors.Nuclide
  AND T.Product LIKE WasteFactors.Product
  AND (WasteFactors.MinActivity IS NULL
  OR WasteFactors.MinActivity >T.MBq)
  AND (WasteFactors.MaxActivity IS NULL
  OR WasteFactors.MaxActivity <= T.MBq)
  ORDER BY WasteFactors.Nuclide ASC, WasteFactors.Product DESC)
)
SELECT T.Nuclide
FROM [RMP Administrations] AS T
WHERE Year(T.[Date Given])=[Enter Year]
GROUP BY T.Nuclide
PIVOT Format(T.[Date Given],"mm - mmm");

给出错误“访问无法将T.Nuclide识别为有效字段或....”。我看不到我的SQL错误,也看不到为什么它不能按书面要求工作,我尝试制作一个VBA函数来计算浪费量SUM(GetWaste(Nuclide, Product,MBq)),并且该函数运行与在记录集中的上方,但是我的查询太复杂而无法评估。

是否有人对我的交叉表查询出了什么问题,如何重组我的WasteFactors以使其更易于查询有任何想法,或者这太复杂以至于无法在SQL中尝试做,我应该这样做在VBA中代替?

真正的数据集是大约1000条记录,跨越了多个月,我想将表名和列名更改为一些废话,但我没有创建数据库。以上数据的预期输出为:

Nuclide    01 - Jan    02 - Feb
-------------------------------
Tc-99m     210
I-131      277.5       399
I-123      117.3

1 个答案:

答案 0 :(得分:2)

您不能通过afaik在TRANSFORM子句中使用子查询。

在您的FROM子句中使用子查询,将该子查询乘以MBq。然后仅在TRANSFORM子句

中使用纯聚合

示例,可能需要改进:

PARAMETERS [Enter Year] Short;
TRANSFORM SUM(TransformField)
SELECT R.Nuclide
FROM (SELECT *, 
Mbq * (SELECT TOP 1
    Factor
  FROM WasteFactors
  WHERE T.Nuclide = WasteFactors.Nuclide
  AND T.Product LIKE WasteFactors.Product
  AND (WasteFactors.MinActivity IS NULL
  OR WasteFactors.MinActivity >T.MBq)
  AND (WasteFactors.MaxActivity IS NULL
  OR WasteFactors.MaxActivity <= T.MBq)
  ORDER BY WasteFactors.Nuclide ASC, WasteFactors.Product DESC) As TransformField
 FROM [RMP Administrations] T
) AS R
WHERE Year(R.[Date Given])=[Enter Year]
GROUP BY R.Nuclide
PIVOT Format(R.[Date Given],"mm - mmm");