Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql ms access:查询(将多条记录合并为一条)_Sql_Ms Access - Fatal编程技术网

Sql ms access:查询(将多条记录合并为一条)

Sql ms access:查询(将多条记录合并为一条),sql,ms-access,Sql,Ms Access,以下是原始表格的一个概览: Occurrence Number Occurrence Date 1 0 Preanalytical (Before Testing) Cup Type 2 0 Analytical (Testing Phase) 2 0 Area 3 0 Postanalytical ( After Testing) 4 0 Other Practice Code Comments 1477 2/5/2010 1.1 Specimen Mi

以下是原始表格的一个概览:

Occurrence Number   Occurrence Date 1 0 Preanalytical (Before Testing)  Cup Type    2 0 Analytical (Testing Phase)  2 0 Area    3 0 Postanalytical ( After Testing) 4 0 Other   Practice Code   Comments
1477    2/5/2010    1.1 Specimen Mislabeled                     PURSLEY 
1476    2/5/2010    1.1 Specimen Mislabeled                     HPMR    
1475    2/5/2010    1.1 Specimen Mislabeled                     ACCIM   N008710
1474    2/5/2010    1.1 Specimen Mislabeled                     ACCIM   N008636
1473    2/5/2010    1.3 QNS-Quantity Not Sufficient                     SAPMC   
1472    2/5/2010    1.3 QNS-Quantity Not Sufficient                     RMG 
1471    2/5/2010    1.1 Specimen Mislabeled                     NMED    
1470    2/5/2010    1.9 QNS- Specimen Spilled in transit                        MRPS    
1469    2/5/2010    1.9 QNS- Specimen Spilled in transit                        ANESPC  
1468    2/5/2010            2.22 Instrument Problem-reinject                LAB 
1525    2/8/2010            2.5 Other - False (+) Blanks    Tecan 2         LAB 
1524    2/8/2010            2.5 Other - False (+) Blanks    Tecan #1            LAB Blank 019
1523    2/8/2010            2.22 Instrument Problem, 2.5 Other  Tecan           LAB 
1519    2/8/2010                    3.3A Reporting Error    4.1 LIS Problem? (see LOM 1418,1520)    LAB/SJC F356028
1518    2/8/2010    1.4 Tests Missed/Wrong Test Ordered                     SDPTC   F316628
1516    2/8/2010    1.6 Test Requisition Missing                        TPMCF   2 specimens both unlabeled
1515    2/8/2010    1.1 Specimen Mislabeled                     PALMETTO    
1514    2/8/2010    1.1 Specimen Mislabeled                     THWR    
1513    2/8/2010    1.1 Specimen Mislabeled                     THWR    
我使用以下语句从此表中获取信息:

select mid(Lom1,1,4) as LOM, sum([Count1]) as [Count] from (

SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] as Lom1,Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count1]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0

UNION SELECT [Lab Occurrence Form].[2 0 Analytical (Testing Phase)], Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)]) AS [CountOf2 0 Analytical (Testing Phase)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[2 0 Analytical (Testing Phase)]
HAVING Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)])<>0

union

SELECT  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)], Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]) AS [CountOf3 0 Postanalytical ( After Testing)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]
HAVING Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)])<>0

UNION SELECT [Lab Occurrence Form].[4 0 Other], Count([Lab Occurrence Form].[4 0 Other]) AS [CountOf4 0 Other]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[4 0 Other]
HAVING Count([Lab Occurrence Form].[4 0 Other])<>0
ORDER BY 1, 2)

group by mid(Lom1,1,4);
LOM Count
1.1     231
1.11    21
1.3     103
1.4     6
1.5     1
1.6     25
1.8     2
1.9     88
2.1     8
2.22    5
2.24    1
2.3     1
2.4     1
2.5     29
3.2     13
3.3     8
3.3A    4
4.1     2
4.6     1
4.8     7
我需要在这里添加另一列。假设它是
column3

这是需要的输出:

LOM Count   column3
1.1     231 everything from original table where LOM LIKE *1.1* separated by ","
1.11    21  everything from original table where LOM=1.11 separated by ","
1.3     103 everything from original table where LOM=1.3 separated by ","
1.4     6   everything from original table where LOM=1.4 separated by ","
1.5     1   everything from original table where LOM=1.5 separated by ","
1.6     25  
1.8     2   
1.9     88  
2.1     8   
2.22    5   
2.24    1   
2.3     1   
2.4     1   
2.5     29  
3.2     13  
3.3     8   
3.3A    4   
4.1     2   
4.6     1   
4.8     7   
prac    1   
这意味着第3列中的第一个元素将是“something1,something2,等等……somethingLSE231”


如果这个解释很糟糕,我深表歉意。如果我能澄清任何问题,请告诉我。

我找到了一个解决方案:

它需要编写一个VBA函数。我不知道用Access中的直接SQL实现这一点的方法

Public Function Conc(Fieldx, Identity, Value, Source) As Variant
  Dim cnn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim SQL As String
  Dim vFld As Variant

  Set cnn = CurrentProject.Connection
  Set rs = New ADODB.Recordset
  vFld = Null

  SQL = "SELECT [" & Fieldx & "] as Fld" & _
        " FROM [" & Source & "]" & _
        " WHERE [" & Identity & "]=" & Value

  ' open recordset.
  rs.Open SQL, cnn, adOpenForwardOnly, adLockReadOnly

  ' concatenate the field.
  Do While Not rs.EOF
    If Not IsNull(rs!Fld) Then
      vFld = vFld & ", " & rs!Fld
    End If
    rs.MoveNext
  Loop
  ' remove leading comma and space.
  vFld = Mid(vFld, 3)

  Set cnn = Nothing
  Set rs = Nothing

  ' return concatenated string.
  Conc = vFld
End Function
然后,您可以在如下查询中使用它:

SELECT [tblData].[ID], 
       Conc("Field1","ID",[ID],"tblData") AS Field1,
       Conc("Field2","ID",[ID],"tblData") AS Field2
FROM tblData
GROUP BY [tblData].[ID];
SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] as Lom1,
       Conc("NameOfTheFieldToConcatenate", 
            "[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]", 
            [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)],
            "[Lab Occurrence Form]"),
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count1]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0
编辑 因此,您的第一个查询如下所示:

SELECT [tblData].[ID], 
       Conc("Field1","ID",[ID],"tblData") AS Field1,
       Conc("Field2","ID",[ID],"tblData") AS Field2
FROM tblData
GROUP BY [tblData].[ID];
SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] as Lom1,
       Conc("NameOfTheFieldToConcatenate", 
            "[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]", 
            [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)],
            "[Lab Occurrence Form]"),
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count1]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0
选择[实验室发生表][10分析前(测试前)]作为Lom1,
Conc(“场域名称”),
“[实验室发生表].[10分析前(试验前)]”,
[实验室发生表][10分析前(试验前)],
“[实验室事件表]”,
计数([实验室发生表].[10分析前(测试前)])为[计数1]
来自[实验室事件表]
其中((([实验室发生表][发生日期])介于[表格]![会议原因与频率]![Text4]和[表格]![会议原因与频率]![Text2]))
分组依据【实验室发生表】【10分析前(测试前)】
有计数([实验室发生表][1 0分析前(测试前)]0

请注意,您可能需要稍微调整
Conc()
函数,以获得所需的wildard比较,而不是LOM字段上的精确匹配。

我找到了一个解决方案:

它需要编写一个VBA函数。我不知道用Access中的直接SQL实现这一点的方法

Public Function Conc(Fieldx, Identity, Value, Source) As Variant
  Dim cnn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim SQL As String
  Dim vFld As Variant

  Set cnn = CurrentProject.Connection
  Set rs = New ADODB.Recordset
  vFld = Null

  SQL = "SELECT [" & Fieldx & "] as Fld" & _
        " FROM [" & Source & "]" & _
        " WHERE [" & Identity & "]=" & Value

  ' open recordset.
  rs.Open SQL, cnn, adOpenForwardOnly, adLockReadOnly

  ' concatenate the field.
  Do While Not rs.EOF
    If Not IsNull(rs!Fld) Then
      vFld = vFld & ", " & rs!Fld
    End If
    rs.MoveNext
  Loop
  ' remove leading comma and space.
  vFld = Mid(vFld, 3)

  Set cnn = Nothing
  Set rs = Nothing

  ' return concatenated string.
  Conc = vFld
End Function
然后,您可以在如下查询中使用它:

SELECT [tblData].[ID], 
       Conc("Field1","ID",[ID],"tblData") AS Field1,
       Conc("Field2","ID",[ID],"tblData") AS Field2
FROM tblData
GROUP BY [tblData].[ID];
SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] as Lom1,
       Conc("NameOfTheFieldToConcatenate", 
            "[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]", 
            [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)],
            "[Lab Occurrence Form]"),
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count1]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0
编辑 因此,您的第一个查询如下所示:

SELECT [tblData].[ID], 
       Conc("Field1","ID",[ID],"tblData") AS Field1,
       Conc("Field2","ID",[ID],"tblData") AS Field2
FROM tblData
GROUP BY [tblData].[ID];
SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] as Lom1,
       Conc("NameOfTheFieldToConcatenate", 
            "[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]", 
            [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)],
            "[Lab Occurrence Form]"),
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count1]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0
选择[实验室发生表][10分析前(测试前)]作为Lom1,
Conc(“场域名称”),
“[实验室发生表].[10分析前(试验前)]”,
[实验室发生表][10分析前(试验前)],
“[实验室事件表]”,
计数([实验室发生表].[10分析前(测试前)])为[计数1]
来自[实验室事件表]
其中((([实验室发生表][发生日期])介于[表格]![会议原因与频率]![Text4]和[表格]![会议原因与频率]![Text2]))
分组依据【实验室发生表】【10分析前(测试前)】
有计数([实验室发生表][1 0分析前(测试前)]0

请注意,您可能需要调整
Conc()
使用一点函数来获取所需的wildard比较,而不是LOM字段上的精确匹配。

对不起,您能解释一下此文档能帮我将其合并到当前sql语句中的具体内容吗?对不起,您能解释一下此文档能帮我将其合并到当前sql语句中的具体内容吗