SQL Union All 用新数据显示旧数据?

发布于 2024-07-24 16:32:09 字数 3422 浏览 5 评论 0原文

在 MS Access 中,我有一些报告使用一些查询来显示日期范围内的数据。 这些查询使用别名、if、sum 和 avg 函数,并连接多个表来获取其数据。

我想知道如果有人选择跨越新数据和旧数据的范围,我是否可以使用 UNION ALL 以及包含所有所需字段的表来显示该表中的新数据以及旧数据老的。

这是一个“旧”查询示例:

SELECT tblAssessment.fldValid, tblATraining.fldTID, tblATraining.fldTCrsID, 
tblCourses.fldCrsName, [fldCrsHrs]/8 AS Days, tblATraining.fldTLocAbr, 
tblDistrict.fldDistAbr, tblRegion.fldRegName, tblATraining.fldTDateStart, 
tblATraining.fldTDateEnd, tblATraining.fldTEnrolled, tblATraining.fldTPID, 
tblPersonnel.fldPName, tblAssessment.fldTrngSID, tblAssessment.Q1, 
IIf([fldValid]=True,IIf([Q1]>0,1,0),0) AS Q1Valid, tblAssessment.Q2, 
IIf([fldValid]=True,IIf([Q2]>0,1,0),0) AS Q2Valid, tblAssessment.Q3, 
IIf([fldValid]=True,IIf([Q3]>0,1,0),0) AS Q3Valid, tblAssessment.Q4, 
IIf([fldValid]=True,IIf([Q4]>0,1,0),0) AS Q4Valid, tblAssessment.Q5, 
IIf([fldValid]=True,IIf([Q5]>0,1,0),0) AS Q5Valid, tblAssessment.Q6, 
IIf([fldValid]=True,IIf([Q6]>0,1,0),0) AS Q6Valid, tblAssessment.Q7, 
IIf([fldValid]=True,IIf([Q7]>0,1,0),0) AS Q7Valid, tblAssessment.Q8, 
tblAssessment.Q9, 
IIf([fldValid]=True,IIf([Q9]>0,1,0),0) AS Q9Valid, tblAssessment.Q10, 
IIf([fldValid]=True,IIf([Q10]>0,1,0),0) AS Q10Valid, tblAssessment.Q11, 
IIf([fldValid]=True,IIf([Q11]>0,1,0),0) AS Q11Valid, tblAssessment.Q12, 
IIf([fldValid]=True,IIf([Q12]>0,1,0),0) AS Q12Valid, tblAssessment.Q13, 
tblAssessment.Q14, 
IIf([fldValid]=True,IIf([Q14]>0,1,0),0) AS Q14Valid, tblAssessment.Q15, 
IIf([fldValid]=True,IIf([Q15]>0,1,0),0) AS Q15Valid, tblAssessment.Q16, 
IIf([fldValid]=True,IIf([Q16]>0,1,0),0) AS Q16Valid, tblAssessment.Q17, 
IIf([fldValid]=True,IIf([Q17]>0,1,0),0) AS Q17Valid, tblAssessment.Q18, 
IIf([fldValid]=True,IIf([Q18]>0,1,0),0) AS Q18Valid, tblAssessment.Q19, 
IIf([fldValid]=True,IIf([Q19]>0,1,0),0) AS Q19Valid, tblAssessment.Q20, 
tblAssessment.Q21, 
IIf([fldValid]=True,IIf([Q21]>0,1,0),0) AS Q21Valid, tblAssessment.Q22, 
IIf([fldValid]=True,IIf([Q22]>0,1,0),0) AS Q22Valid, tblAssessment.Q23, 
IIf([fldValid]=True,IIf([Q23]>0,1,0),0) AS Q23Valid, tblAssessment.Q24, 
IIf([fldValid]=True,IIf([Q24]>0,1,0),0) AS Q24Valid, tblAssessment.Q25, 
IIf([fldValid]=True,IIf([Q25]>0,1,0),0) AS Q25Valid, tblAssessment.Q26, 
IIf([fldValid]=True,IIf([Q26]>0,1,0),0) AS Q26Valid, tblAssessment.Q27, 
IIf([fldValid]=True,IIf([Q27]>0,1,0),0) AS Q27Valid, tblAssessment.Q28, 
IIf([fldValid]=True,IIf([Q28]>0,1,0),0) AS Q28Valid, tblAssessment.Q29, 
tblAssessment.Q30, 
tblAssessment.Q31, tblAssessment.Q32
FROM ((tblDistrict 
       LEFT JOIN tblRegion ON tblDistrict.fldDRegID = tblRegion.fldRegID) 
       RIGHT JOIN (((tblATraining 
                     LEFT JOIN tblCourses ON tblATraining.fldTCrsID = tblCourses.fldCrsID) 
                   LEFT JOIN tblPersonnel ON tblATraining.fldTPID = tblPersonnel.fldPID) 
                  LEFT JOIN tblLocations ON tblATraining.fldTLocAbr = tblLocations.fldLID) ON tblDistrict.fldDistAbr = tblATraining.fldTDistAbr) 
     LEFT JOIN tblAssessment ON tblATraining.fldTID = tblAssessment.fldTrngCID
WHERE (((tblAssessment.fldValid)=True) 
        AND ((tblATraining.fldTCrsID) Like [forms]![fdlgRptCriteria].[selCrsCd]) 
        AND ((tblATraining.fldTDateStart) Between [forms]![fdlgRptCriteria].[seldate1] And [forms]![fdlgRptCriteria].[seldate2]) 
        AND ((tblAssessment.fldTrngSID) Is Not Null));

In MS Access, I have some reports that use some queries, to show data, within a date range. The queries use aliases, if, sum, and avg functions, and join multiple tables to get its data.

I'd like to know if i could use a UNION ALL, with a table that has all the needed fields, to display this new data from this table, along with the older data, if someone selects a range that spans the new and the old.

Here's an example "old" query:

SELECT tblAssessment.fldValid, tblATraining.fldTID, tblATraining.fldTCrsID, 
tblCourses.fldCrsName, [fldCrsHrs]/8 AS Days, tblATraining.fldTLocAbr, 
tblDistrict.fldDistAbr, tblRegion.fldRegName, tblATraining.fldTDateStart, 
tblATraining.fldTDateEnd, tblATraining.fldTEnrolled, tblATraining.fldTPID, 
tblPersonnel.fldPName, tblAssessment.fldTrngSID, tblAssessment.Q1, 
IIf([fldValid]=True,IIf([Q1]>0,1,0),0) AS Q1Valid, tblAssessment.Q2, 
IIf([fldValid]=True,IIf([Q2]>0,1,0),0) AS Q2Valid, tblAssessment.Q3, 
IIf([fldValid]=True,IIf([Q3]>0,1,0),0) AS Q3Valid, tblAssessment.Q4, 
IIf([fldValid]=True,IIf([Q4]>0,1,0),0) AS Q4Valid, tblAssessment.Q5, 
IIf([fldValid]=True,IIf([Q5]>0,1,0),0) AS Q5Valid, tblAssessment.Q6, 
IIf([fldValid]=True,IIf([Q6]>0,1,0),0) AS Q6Valid, tblAssessment.Q7, 
IIf([fldValid]=True,IIf([Q7]>0,1,0),0) AS Q7Valid, tblAssessment.Q8, 
tblAssessment.Q9, 
IIf([fldValid]=True,IIf([Q9]>0,1,0),0) AS Q9Valid, tblAssessment.Q10, 
IIf([fldValid]=True,IIf([Q10]>0,1,0),0) AS Q10Valid, tblAssessment.Q11, 
IIf([fldValid]=True,IIf([Q11]>0,1,0),0) AS Q11Valid, tblAssessment.Q12, 
IIf([fldValid]=True,IIf([Q12]>0,1,0),0) AS Q12Valid, tblAssessment.Q13, 
tblAssessment.Q14, 
IIf([fldValid]=True,IIf([Q14]>0,1,0),0) AS Q14Valid, tblAssessment.Q15, 
IIf([fldValid]=True,IIf([Q15]>0,1,0),0) AS Q15Valid, tblAssessment.Q16, 
IIf([fldValid]=True,IIf([Q16]>0,1,0),0) AS Q16Valid, tblAssessment.Q17, 
IIf([fldValid]=True,IIf([Q17]>0,1,0),0) AS Q17Valid, tblAssessment.Q18, 
IIf([fldValid]=True,IIf([Q18]>0,1,0),0) AS Q18Valid, tblAssessment.Q19, 
IIf([fldValid]=True,IIf([Q19]>0,1,0),0) AS Q19Valid, tblAssessment.Q20, 
tblAssessment.Q21, 
IIf([fldValid]=True,IIf([Q21]>0,1,0),0) AS Q21Valid, tblAssessment.Q22, 
IIf([fldValid]=True,IIf([Q22]>0,1,0),0) AS Q22Valid, tblAssessment.Q23, 
IIf([fldValid]=True,IIf([Q23]>0,1,0),0) AS Q23Valid, tblAssessment.Q24, 
IIf([fldValid]=True,IIf([Q24]>0,1,0),0) AS Q24Valid, tblAssessment.Q25, 
IIf([fldValid]=True,IIf([Q25]>0,1,0),0) AS Q25Valid, tblAssessment.Q26, 
IIf([fldValid]=True,IIf([Q26]>0,1,0),0) AS Q26Valid, tblAssessment.Q27, 
IIf([fldValid]=True,IIf([Q27]>0,1,0),0) AS Q27Valid, tblAssessment.Q28, 
IIf([fldValid]=True,IIf([Q28]>0,1,0),0) AS Q28Valid, tblAssessment.Q29, 
tblAssessment.Q30, 
tblAssessment.Q31, tblAssessment.Q32
FROM ((tblDistrict 
       LEFT JOIN tblRegion ON tblDistrict.fldDRegID = tblRegion.fldRegID) 
       RIGHT JOIN (((tblATraining 
                     LEFT JOIN tblCourses ON tblATraining.fldTCrsID = tblCourses.fldCrsID) 
                   LEFT JOIN tblPersonnel ON tblATraining.fldTPID = tblPersonnel.fldPID) 
                  LEFT JOIN tblLocations ON tblATraining.fldTLocAbr = tblLocations.fldLID) ON tblDistrict.fldDistAbr = tblATraining.fldTDistAbr) 
     LEFT JOIN tblAssessment ON tblATraining.fldTID = tblAssessment.fldTrngCID
WHERE (((tblAssessment.fldValid)=True) 
        AND ((tblATraining.fldTCrsID) Like [forms]![fdlgRptCriteria].[selCrsCd]) 
        AND ((tblATraining.fldTDateStart) Between [forms]![fdlgRptCriteria].[seldate1] And [forms]![fdlgRptCriteria].[seldate2]) 
        AND ((tblAssessment.fldTrngSID) Is Not Null));

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

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

发布评论

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

评论(4

耳钉梦 2024-07-31 16:32:09

是的,但您可能会考虑为联合创建一个新的 querydef。

(您需要使用我上次检查时的 SQL 设计窗口。)

大约为:

    SELECT
    tblATraining.fldTID,
    tblATraining.fldTCrsID,
    tblATraining.fldTLocAbr,
    tblATraining.fldTDateStart,
    tblATraining.fldTDateStart,
    tblATraining.fldTDateEnd,
    tblATraining.fldTEnrolled,
    tblATraining.fldTPID

    UNION ALL

    tblATrainingArchive.fldTID,
    tblATrainingArchive.fldTCrsID,
    tblATrainingArchive.fldTLocAbr,
    tblATrainingArchive.fldTDateStart,
    tblATrainingArchive.fldTDateStart,
    tblATrainingArchive.fldTDateEnd,
    tblATrainingArchive.fldTEnrolled,
    tblATrainingArchive.fldTPID

并将其命名为 tblATrainingUnion 之类的名称。

然后只需将其插入到您现有的查询中,只要您有 tblATraining 即可。

注意:许多人还倾向于在真正有意义之前创建存档表 - 您可能会考虑将其全部保留在主表中,并在可以实际测量差异时进行拆分。 (您可能已经达到了这一点并做到了这一点;并且使用 Access 比使用 SQL Server 更有意义。)

Yes, but you might consider creating a new querydef for the union.

(You'll need to use the SQL design window last time I checked.)

It would be approximately:

    SELECT
    tblATraining.fldTID,
    tblATraining.fldTCrsID,
    tblATraining.fldTLocAbr,
    tblATraining.fldTDateStart,
    tblATraining.fldTDateStart,
    tblATraining.fldTDateEnd,
    tblATraining.fldTEnrolled,
    tblATraining.fldTPID

    UNION ALL

    tblATrainingArchive.fldTID,
    tblATrainingArchive.fldTCrsID,
    tblATrainingArchive.fldTLocAbr,
    tblATrainingArchive.fldTDateStart,
    tblATrainingArchive.fldTDateStart,
    tblATrainingArchive.fldTDateEnd,
    tblATrainingArchive.fldTEnrolled,
    tblATrainingArchive.fldTPID

and name it something like tblATrainingUnion.

Then just plug that into your existing query wherever you have tblATraining.

Note: Many people also tend to create an archive table sooner than it really makes sense - you might consider just leaving it all in the main table, and make the split when you can actually measure the difference. (You may already have reached that point and done that; and it can make more sense with Access than with say SQL Server.)

一世旳自豪 2024-07-31 16:32:09

是的,Access 支持UNION ALL。 一个简单的例子是:

select foo from OldData
union all
select foo from NewData

Yes, Access supports UNION ALL. A simple example would be:

select foo from OldData
union all
select foo from NewData
睫毛溺水了 2024-07-31 16:32:09

我想强调 @le dorfier 关于存档的评论。 大多数具有 Jet 数据存储的 Access 应用程序不需要存档数据。 我的一位客户运行着一个 6 个停车场的计费系统,该系统是我在 1997-98 年为她建造的(从那时起,该系统已进行了广泛的升级、扩展和修改)。 它包含整个期间的数据(每个车库每月 200-300 张发票),而且现在的运行速度与当时一样快。 部分原因是 PC 性能的提高(即,在数据量与 1997 时代的 PC 相同的情况下,速度可能会非常慢),但它仍然运行得很好。

如果速度开始成为问题,我们可能会将后端移至 SQL Server Express。 我们最不想考虑的就是归档数据。

我的另一个客户坚持归档他们的非活动数据,然后我不得不重新编程“添加新记录”功能来检查归档(由于应用程序的要求,同一个人没有创建新记录是至关重要的,但是而是从档案中检索它)。 这大大减慢了添加新记录的过程。 现在我正试图说服他们“解除存档”他们的数据,因为这实际上并没有以任何方式帮助他们,而且使日常操作过程中的工作速度变得更慢。

I would emphasize @le dorfier's comment about archiving. Most Access applications with a Jet data store don't need data to be archived. A client of mine runs a billing system for 6 parking garages that I built for her back in 1997-98 (it's been upgraded, extended and revised extensively since then). It includes data for that entire period (200-300 invoices for each month for each garage) and it works just as fast now as it did back then. Part of that is the increase in the performance of PCs (i.e., with the same amount of data and 1997-era PCs, it would probably be very slow), but it still works just fine.

If speed started to be an issue, we'd probably move the back end to SQL Server Express. The last thing we'd contemplate would be archiving the data.

Another client of mine insisted on archiving their inactive data, and then I had to reprogram the ADD NEW RECORD function to check against the archive (because of the requirements of the app, it's crucial that the same person not have a new record created, but instead have it retrieved from the archive). This vastly slowed down the process of adding new records. Now I'm trying to convince them to "de-archive" their data, because it hasn't actually helped them in any way and has made things work more slowly during daily operations.

贪恋 2024-07-31 16:32:09

有两条评论与您的​​问题无关。

1) Q1 至 Q32 字段的用途是什么? 我想也许这些可以以某种方式重新设计和/或标准化。

2)我认为没有理由在对象名称上使用命名标准。 如tbl、fld、frm、qry等。 通过代码中的上下文,您几乎可以知道它们是什么类型的对象。 如果在各种数据库容器窗口中,这些也非常明显。

也就是说,我在 VBA 代码中确实使用了一些变量命名约定,只是为了帮助保持这些清晰。

请参阅 Tony 的对象命名约定Tony 的表和字段命名约定 了解更多详细信息。

我很希望有些人强烈反对我的第二条评论,并拒绝我的帖子。

Two comments not related to your question.

1) What's the purpose of the Q1 to Q32 fields? I'm thinking that maybe those could be somehow redesigned and/or normalized.

2) I see no reason to use naming standards on object names. Such as tbl, fld, frm, qry and so forth. You pretty much know what kind of an object they are by the context in the code. If in the various database container windows those are pretty obvious as well.

That said I do somewhat use the variable naming conventions in my VBA code just to help keep those clear.

See Tony's Object Naming Conventions and Tony's Table and Field Naming Conventions for more details.

I quite expect some folks to strenuously disagree with my second comment and to thumbs down my posting.

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