简体   繁体   English

SQL中的主从查询

[英]Master-Detail Query in SQL

I am having trouble querying my data where the expected result is a master-detail type output.我在查询预期结果是主从类型 output 的数据时遇到问题。

I have a table.我有一张桌子。 In this table I have three columns, they are all strings:在这张表中,我有三列,它们都是字符串:

Version版本

URL URL

Application应用

In this table, I have the following data:在这张表中,我有以下数据:

**Version**   **URL**                           **Application**
New           http://www.stackoverflow1.com     Application1
New           http://www.stackoverflow2.com     Application1
Old           http://www.stackoverflow3.com     Application2

The expected Output would be预期的 Output 将是

New - Application 1 - (2)
    http://www.stackoverflow1.com
    http://www.stackoverflow2.com
Old - Application 2 - (1)
    http://www.stackoverflow3.com

This table represents an inventory of applications that are deployed on a company's network.此表表示部署在公司网络上的应用程序清单。 An application can exist on multiple URLs, and be one of two versions, in this example “new” or “old “.一个应用程序可以存在于多个 URL 上,并且是两个版本之一,在本例中为“新”或“旧”。 The goal of the query I am having a problem with is to be able to provide a report where the Version, then Application, group the URLs so that one could see, for example, I have the “new” version of application “X” deployed at such and such URLs.我遇到问题的查询的目标是能够提供一个报告,其中版本,然后是应用程序,对 URL 进行分组,以便人们可以看到,例如,我有应用程序“X”的“新”版本部署在这样那样的 URL。 In addition, I also need to provide the amounts/counts of URL's for each grouping of Versions and Application, for example the “new” version of application “X” appeared this many times (this data will eventually be exported from SQL to a spreadsheet).此外,我还需要为每组版本和应用程序提供 URL 的数量/计数,例如应用程序“X”的“新”版本出现了这么多次(这些数据最终将从 SQL 导出到电子表格)。

You likely don't need to write any code.您可能不需要编写任何代码。

Use the report wizard - it will group for you.使用报告向导 - 它会为您分组。

Assuming you have the table in Access.假设您在 Access 中有表。 Just click on the table (highlight).只需单击表格(突出显示)。

Then from ribbon create - choose the report wizard.然后从功能区创建 - 选择报告向导。

ORDER that you choose the fields is VERY important.您选择字段的顺序非常重要。

So, application, version, URL.所以,应用程序,版本,URL。

Group by Application, version.按应用程序、版本分组。

Choose "stepped"选择“步进”

The report will look like this:报告将如下所示:

在此处输入图像描述

Now, you can save the report -现在,您可以保存报告 -

Now open report in design mode.现在以设计模式打开报表。 Now from ribbon - choose Group and sort.现在从功能区 - 选择组并排序。 Choose to add a sum - but choose your "new" column - it will offer a count due to this being a text value.选择添加一个总和 - 但选择您的“新”列 - 由于这是一个文本值,它将提供一个计数。

You get this:你得到这个:

在此处输入图像描述

And then you can move up the total box to the detail section.然后您可以将总框向上移动到详细信息部分。

You get this:你得到这个:

在此处输入图像描述

I suppose you could consider a SQL group by, but the sorting and grouping with the report writer can quite much group and total on rows of data just about anyway you want.我想您可以考虑使用 SQL 分组,但是报告编写器的排序和分组可以根据您的需要对多行数据进行分组和汇总。

You can remove all the extra heading stuff and other junk - once done, then from the ribbon you can export to excel.您可以删除所有额外的标题内容和其他垃圾 - 完成后,您可以从功能区导出到 excel。

Below is some VBA code that uses two recordsets, one to get the "title" information for each group, and the other to get the detailed information for each group, and outputs it all to an Excel file:下面是一些 VBA 代码,它使用两个记录集,一个获取每个组的“标题”信息,另一个获取每个组的详细信息,并将其全部输出到 Excel 文件:

Sub sExportAppData()
    On Error GoTo E_Handle
    Dim db As DAO.Database
    Dim rsMaster As DAO.Recordset
    Dim rsDetail As DAO.Recordset
    Dim strSQL As String
    Dim objXL As New Excel.Application
    Dim objXLBook As Excel.Workbook
    Dim objXLSheet As Excel.Worksheet
    Dim strXLFile As String
    Dim lngRow As Long
    strXLFile = "J:\downloads\app-data.xlsx"
    If Len(Dir(strXLFile)) > 0 Then Kill strXLFile
    Set db = DBEngine(0)(0)
    strSQL = "SELECT A.AppVersion, A.AppApplication, Count(A.AppApplication) AS AppFrequency " _
        & " FROM tblApplication A " _
        & " GROUP BY A.AppVersion, A.AppApplication " _
        & " ORDER BY A.AppVersion ASC, A.AppApplication ASC;"
    Set rsMaster = db.OpenRecordset(strSQL)
    If Not (rsMaster.BOF And rsMaster.EOF) Then
        Set objXLBook = objXL.Workbooks.Add
        Set objXLSheet = objXLBook.Worksheets(1)
        lngRow = 1            
        Do
            objXLSheet.Cells(lngRow, 1) = rsMaster!AppVersion & " - " & rsMaster!AppApplication & " - (" & rsMaster!AppFrequency & ")"
            lngRow = lngRow + 1
            strSQL = "SELECT AppURL FROM tblApplication " _
                & " WHERE AppVersion='" & rsMaster!AppVersion & "' AND AppApplication='" & rsMaster!AppApplication & "' " _
                & " ORDER BY AppURL ASC;"
            Set rsDetail = db.OpenRecordset(strSQL)
            If Not (rsDetail.BOF And rsDetail.EOF) Then
                Do
                    objXLSheet.Cells(lngRow, 1) = rsDetail!AppURL
                    lngRow = lngRow + 1
                    rsDetail.MoveNext
                Loop Until rsDetail.EOF
            End If
            rsMaster.MoveNext
        Loop Until rsMaster.EOF
        objXLBook.SaveAs strXLFile
    End If
sExit:
    On Error Resume Next
    rsDetail.Close
    rsMaster.Close
    Set rsDetail = Nothing
    Set rsMaster = Nothing
    Set objXLSheet = Nothing
    objXLBook.Close
    Set objXLBook = Nothing
    objXL.Quit
    Set objXL = Nothing
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "sExportAppData", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

Regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM