DISTINCT 不起作用

发布于 2024-10-07 23:00:29 字数 1518 浏览 2 评论 0原文

Ms-Access

INSERT INTO tblTmpEventLog( TrackingNumber, PartNumber, PartNumberChgLvl,
                            EnteredBy, EventTypeSelected, EventDate )
SELECT DISTINCT tblRevRelLog_Detail.RevRelTrackingNumber,
       tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel,
       [Forms]![frmEventLog_Input]![EnteredBy] AS EnteredBy,
       [Forms]![frmEventLog_Input]![EventTypeSelected] AS EventTypeSelected,
       CDate([Forms]![frmEventLog_Input]![EventDate]) AS EventDate
  FROM tblRevRelLog_Detail LEFT JOIN tblEventLog
    ON (tblEventLog.PartNumber =  tblRevRelLog_Detail.PartNumber)
   AND (tblEventLog.PartNumberChgLvl =  tblRevRelLog_Detail.ChangeLevel)
 WHERE ((([tblRevRelLog_Detail]![RevRelTrackingNumber]) = 
          [Forms]![frmEventLog_Input]![TrackingNumber]))
   AND ((tblEventLog.PartNumber) NOT IN
            (SELECT tblEventLog.PartNumber FROM tblEventLog
              WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
                AND tblEventLog.TrackingNumber =
                         tblRevRelLog_Detail.RevRelTrackingNumber
                AND tblEventLog.PartNumber =  tblRevRelLog_Detail.PartNumber
                AND tblEventLog.PartNumberChgLvl =  
                         tblRevRelLog_Detail.ChangeLevel
             ));

DISTINCT 关键字中 EnteredBy、EventTypeSelected 中的 SQL 查询不起作用。我的意思是,当我使用 DISTINCT 关键字时,这些列的数据不会显示。

EVENTDATE 工作正常,但我不明白为什么它不显示 EneteredBy 和 EventTypeSelected 列。

谁能告诉我如何处理这个问题?

SQL query in Ms-Access

INSERT INTO tblTmpEventLog( TrackingNumber, PartNumber, PartNumberChgLvl,
                            EnteredBy, EventTypeSelected, EventDate )
SELECT DISTINCT tblRevRelLog_Detail.RevRelTrackingNumber,
       tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel,
       [Forms]![frmEventLog_Input]![EnteredBy] AS EnteredBy,
       [Forms]![frmEventLog_Input]![EventTypeSelected] AS EventTypeSelected,
       CDate([Forms]![frmEventLog_Input]![EventDate]) AS EventDate
  FROM tblRevRelLog_Detail LEFT JOIN tblEventLog
    ON (tblEventLog.PartNumber =  tblRevRelLog_Detail.PartNumber)
   AND (tblEventLog.PartNumberChgLvl =  tblRevRelLog_Detail.ChangeLevel)
 WHERE ((([tblRevRelLog_Detail]![RevRelTrackingNumber]) = 
          [Forms]![frmEventLog_Input]![TrackingNumber]))
   AND ((tblEventLog.PartNumber) NOT IN
            (SELECT tblEventLog.PartNumber FROM tblEventLog
              WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
                AND tblEventLog.TrackingNumber =
                         tblRevRelLog_Detail.RevRelTrackingNumber
                AND tblEventLog.PartNumber =  tblRevRelLog_Detail.PartNumber
                AND tblEventLog.PartNumberChgLvl =  
                         tblRevRelLog_Detail.ChangeLevel
             ));

DISTINCT keyword for EnteredBy, EventTypeSelected is not working..I mean, data for these columns is not displaying when I use DISTINCT keyword.

EVENTDATE is working fine, but I do not understand why is it not displaying for EneteredBy and EventTypeSelected columns.

Can anyone tell me how to handle this?

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

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

发布评论

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

评论(2

苏别ゝ 2024-10-14 23:00:29

查询可能无法将表单直接正确解释为最终数据类型。但是,在日期字段中,您将其包装在函数 CDATE( ... ) 中。因此,SQL 引擎知道结果类型。我建议对其他领域也这样做。例如:执行 CAST(...您的表单控件...作为 DateTime )作为 OtherColumn 等...我认为 Access 允许强制转换,但不是积极的。否则,将表单值预先拉入已声明的数据类型变量中,并像您所做的那样在查询 AS OtherColumn 中使用该变量。

除了@Jack提到的之外,您可以随时返回您的帐户,查看您的历史问题,然后单击实际帮助/解决您的问题的任何答案。有些问题永远不会得到答案,没关系,只要感谢那些确实提供帮助的人即可。

It may be that the query can't interpret properly from the form directly as the final data type. However in your date field, you are wrapping it in a function CDATE( ... ). So, the SQL engine knows the result type. I would suggest doing the same for the other fields. Ex: doing a CAST ( ...your form control... as DateTime ) as OtherColumn, etc... I THINK Access allows casting, but not positive. Otherwise, pre-pull the form value into a declared data type variable and use THAT variable in the query AS OtherColumn as you are doing.

Additionally to what @Jack mentioned, you can always go back to your account, look at your historical question, and click on whatever answers actually helped / solve your problems. Some questions never do get answers and that's ok, just give credit to those that DO help.

用心笑 2024-10-14 23:00:29

我过去发现(我不记得这是哪个旧版本的 Access),如果您在 VBA 中设置表单控件的值,然后在查询中使用该控件,查询将看不到您的值在VBA中设置。如果用户正常编辑控件,则查询会看到预期值。也许这就是这里发生的事情。

要解决这个问题,您可以声明一个返回所需值的 VBA 函数。例如,代替这个:

SELECT ..., Forms!MainForm!TextEntry AS TextEntry, ... FROM ...

使用这个:

SELECT ..., GetTextEntry() AS TextEntry, ... FROM ...

和这个:

Public Function TextEntry() As Variant
    TextEntry = Forms!MainForm!TextEntry
End Function

I have found in the past (I don't remember which old version of Access this was) that if you set the value of a form control in VBA, and then use that control in a query, the query will not see the value you set in VBA. If the user edits the control normally, the query sees the expected value. Perhaps that's what happened here.

To work around that, you can declare a VBA function that returns the desired value. For example, instead of this:

SELECT ..., Forms!MainForm!TextEntry AS TextEntry, ... FROM ...

use this:

SELECT ..., GetTextEntry() AS TextEntry, ... FROM ...

along with this:

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