SQL Access查询:显示重复条目,交叉表

时间:2014-12-03 00:55:59

标签: sql count ms-access-2007

创建一个查询,显示已进行超过2次维修的任何车辆。显示Vehicle SerialNo,Year,Make,Model和RepairOrders的计数

我有两张表来提取信息:

RepairOrder表

------------------------------------------------------------------------------------
| OrdNo | Odometer |   DateIN   | TimeRecvd | TimeFinish | PhoneWnRdy |  SerialNo  |
------------------------------------------------------------------------------------
|     1 |    50000 |  10/3/2011 |   1:31 PM |    5:30 PM |        Yes | AZXS230I87 |
------------------------------------------------------------------------------------
|     2 |    30000 |  12/2/2011 |   4:20 PM |    5:00 PM |        Yes | IOMEQ54397 |
------------------------------------------------------------------------------------
|     3 |     6000 |  12/4/2011 |   9:00 AM |    1:30 PM |        Yes | JKLP7IIIJF |
------------------------------------------------------------------------------------
|     4 |    75000 |  12/5/2011 |  11:15 AM |    6:00 PM |         No | AZXS230I87 |
------------------------------------------------------------------------------------
|     5 |    15000 |  12/7/2011 |  11:53 AM |    3:30 PM |         No | TYYYI87590 |
------------------------------------------------------------------------------------
|     6 |    55000 |  12/7/2011 |   4:42 PM |    5:30 PM |         No | BHGY08631Q |
------------------------------------------------------------------------------------
|     7 |    25500 |  12/8/2011 |  11:53 AM |    4:00 PM |        Yes | V121BHD481 |
------------------------------------------------------------------------------------
|     8 |   125000 |  12/9/2011 |   2:30 PM |    3:30 PM |        Yes | PLJFVC5609 |
------------------------------------------------------------------------------------
|     9 |    60800 | 12/12/2011 |  12:03 PM |    5:00 PM |        Yes | XCVY760PIQ |
------------------------------------------------------------------------------------
|    10 |    11000 | 12/13/2011 |  11:41 AM |    1:15 PM |        Yes | THYDF55639 |
------------------------------------------------------------------------------------
|    11 |    55060 | 12/15/2011 |  12:04 PM |    4:45 PM |         No | MNMJ7H6098 |
------------------------------------------------------------------------------------
|    12 |    25600 | 12/17/2011 |   9:08 AM |   12:30 PM |        Yes | THYDF55639 |
------------------------------------------------------------------------------------
|    13 |    56000 | 12/17/2011 |   3:14 PM |    4:00 PM |         No | QWER9LK982 |
------------------------------------------------------------------------------------

车辆表

--------------------------------------------------------------------------------------------
| SerialNo   | Year | Make      | Model    | LicenseNo | LicenseState | Cylinders | CustNo |
--------------------------------------------------------------------------------------------
| AZXS230I87 | 2009 | Ford      | Skylark  | 145UKI    | WA           |         4 |     10 |
--------------------------------------------------------------------------------------------
| BHGY08631Q | 2006 | Chevrolet | Blazer   | 567WER    | WA           |         8 |      7 |
--------------------------------------------------------------------------------------------
| GHL39789UI | 2012 | Honda     | Del Sol  | 123XYZ    | MT           |         4 |     13 |
--------------------------------------------------------------------------------------------
| IOMEQ54397 | 2008 | Buick     | Regal    | 367ASZ    | WA           |         8 |      2 |
--------------------------------------------------------------------------------------------
| JHMCCF673Q | 2000 | Honda     | Civic Si | 367ABC    | WA           |         4 |      8 |
--------------------------------------------------------------------------------------------
| JHMEC3348H | 1993 | Buick     | Corolla  | 345XYZ    | WA           |         6 |      5 |
--------------------------------------------------------------------------------------------

我编写了这段代码,我正在努力解决如何从两个不同的表中提取信息的问题。

SELECT Vehicle.SerialNo, Vehicle.Year, Vehicle.Make, Vehicle.Model
FROM Vehicle
Where (Vehicle.SerialNo) IN(Select (RepairOrder.SerialNo) 
                              from RepairOrder 
                              group by (Vehicle.SerialNo)
                              HAVING COUNT(RepairOrder.SerialNo) > 2);

1 个答案:

答案 0 :(得分:0)

你可以试试这个

SELECT V.SerialNo, V.Year, V.Make, V.Model , COUNT(R.OrdNo) AS NumberOfRepairs
FROM Vehicle V INNER JOIN RepairOrder R ON V.SerialNo = R.SerialNo
GROUP BY V.SerialNo
HAVING COUNT(R.OrdNo) > 2;