我有一个查询,我想在其中过滤和合并数据。正如查询的SQL看起来一样:
SELECT UCase$(DLookUp("Abbreviation", "tblRankStructure", "tblRankStructure!ID = " & [tblRoster].[Rank])) AS Rank
,UCase$([tblRoster].[LastName] & ", " & [tblRoster].[FirstName] & " " & IIf(IsNull([tblRoster].[MiddleName]), "", Left$([tblRoster].[MiddleName], 1))) AS [Full Name]
,tblRoster.EDIPI
,tblBasicIndividualRecords.PriMilOccSpec AS MOS
,tblBasicIndividualRecords.Gender
,tblBasicIndividualRecords.BloodType
,tblUnitInformationCodes.PltCode
,IIf(IsNull([tblBasicIndividualRecords] ! [MealCardNumber]), "COMRATS", [tblBasicIndividualRecords] ! [MealCardNumber]) AS [Meal Card]
,tblBasicIndividualRecords.SecurityClearance AS Clearance
,tblBasicWeaponRecords.WeaponType
,tblBasicWeaponRecords.WeaponSerial
,tblBasicWeaponRecords.OpticSerial
FROM (
(
(
tblRankStructure INNER JOIN tblRoster ON tblRankStructure.ID = tblRoster.Rank
) INNER JOIN tblBasicIndividualRecords ON tblRoster.EDIPI = tblBasicIndividualRecords.EDIPI
) LEFT JOIN tblBasicWeaponRecords ON tblRoster.EDIPI = tblBasicWeaponRecords.EDIPI
)
INNER JOIN tblUnitInformationCodes ON tblRoster.EDIPI = tblUnitInformationCodes.EDIPI;
对于这样的查询,如果他们有多个武器,它将作为同一个人的行返回。如何合并以返回相同的数据,但只查询查询中的一行,仍显示所有必需的数据?
我想要的是在手枪系列列下使用M9A1系列的行,并且只有一行用于SSgt Pilewski
通过将另一个字段添加到PistolSerial的tblBasicWeaponRecords来创建此示例。我不想在那里使用这个字段,并希望我的查询能够在tblBasicWeaponRecords中使用这个字段。
所有数据均来自以下表格:
tblRoster
+-------+------------------+----------+-----------+------------+
| EDIPI | Rank | LastName | FirstName | MiddleName |
+-------+------------------+----------+-----------+------------+
| | First Lieutenant | Mozzetta | Jennifer | L |
| | Staff Sergeant | Pilewski | Troy | Andrew |
| | Sergeant | Irizarry | Jonathon | |
+-------+------------------+----------+-----------+------------+
tblBasicIndividualRecords
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
| ID | EDIPI | DateOfBirth | DateOfRank | ArmedForcesActiveDutyBaseDate | EndOfActiveService | PriMilOccSpec | AddMilOccSpec | Billet | Deployable | OnHand | Remarks | Gender | BloodType | SecurityClearance | MealCardNumber | MartialArtsBelt |
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
| 1 | | | | | 12-Aug-2015 | 0602 | | | TRUE | TRUE | | | | | | |
| 2 | | | | | 23-Aug-2014 | 0659 | | | TRUE | TRUE | | | | | | |
| 3 | | | | | 30-Aug-2013 | 0651 | | | TRUE | TRUE | | | | | | |
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
tblBasicWeaponRecords
+----+-------+------------+--------------+-------------+
| ID | EDIPI | WeaponType | WeaponSerial | OpticSerial |
+----+-------+------------+--------------+-------------+
| 1 | | M4 | W151644 | 224604 |
| 3 | | M4 | W142663 | 228541 |
| 4 | | M16A4 | 164522845 | 225487 |
| 6 | | M9A1 | 1476541 | |
+----+-------+------------+--------------+-------------+
tblUnitInformationCodes
+----+-------+---------+
| ID | EDIPI | PltCode |
+----+-------+---------+
| 1 | | CYB |
| 2 | | CYB |
| 3 | | CYB |
+----+-------+---------+