MS Access 中的子查询问题

发布于 2024-08-17 21:05:51 字数 283 浏览 1 评论 0原文

我不太确定为什么以下查询无法在 Access 中运行。 它要求我提供 s1.sku 的值


SELECT s1.SkuApexId, s1.sku, s1.apex, s1.btw, s1.gebruikernr, q2.*
FROM tblSkuApex AS s1,
              (SELECT MAX(s2.begindatum)
              FROM tblskuapex  s2
              WHERE s1.sku = s2.sku) q2

I'm not quite sure why the following query won't run in Access.
It asks me to give the value of s1.sku


SELECT s1.SkuApexId, s1.sku, s1.apex, s1.btw, s1.gebruikernr, q2.*
FROM tblSkuApex AS s1,
              (SELECT MAX(s2.begindatum)
              FROM tblskuapex  s2
              WHERE s1.sku = s2.sku) q2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2024-08-24 21:05:51

按照您的说法,您需要与 q2 表进行交叉联接,该交叉联接将根据 s1 表的每个记录的 s1.sku 值而变化,这是不可接受的。

我认为您应该将 q2 子查询放置为列而不是表。由于您检索单个值,因此可以很容易地这样放置:

SELECT s1.SkuApexId, s1.sku, s1.apex, s1.btw, s1.gebruikernr, 
         (SELECT MAX(s2.begindatum)
              FROM tblskuapex  s2
              WHERE s1.sku = s2.sku) as maxbegindatum
FROM tblSkuApex AS s1

或者甚至更好,为什么不将其作为普通聚合获取,因为您从同一个表请求最大值:

SELECT 
    s1.SkuApexId, s1.sku, s1.apex, 
    s1.btw, s1.gebruikernr, MAX(begindatum)
FROM tblSkuApex AS s1
group by 
    s1.SkuApexId, s1.sku, s1.apex, 
    s1.btw, s1.gebruikernr

注意:您可以在任何地方省略 s1 别名

The way you put it, you require a cross join to the q2 table, which is to be changing depending on the s1.sku value of each record of the s1 table, which is unacceptable.

I think you should place the q2 subquery as a column and not as a table. Since you retrieve a single value, it could be easily put like this:

SELECT s1.SkuApexId, s1.sku, s1.apex, s1.btw, s1.gebruikernr, 
         (SELECT MAX(s2.begindatum)
              FROM tblskuapex  s2
              WHERE s1.sku = s2.sku) as maxbegindatum
FROM tblSkuApex AS s1

or even better, why dont you get it as a normal aggregation, since you request the max from the same table:

SELECT 
    s1.SkuApexId, s1.sku, s1.apex, 
    s1.btw, s1.gebruikernr, MAX(begindatum)
FROM tblSkuApex AS s1
group by 
    s1.SkuApexId, s1.sku, s1.apex, 
    s1.btw, s1.gebruikernr

note: you could ommit the s1 alias everywhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文