具有多个表的子查询

时间:2014-02-09 06:05:03

标签: sql sql-server-2008

美好的一天,需要我的车辆检查数据库的帮助。您可以在下面看到结构,您也可以在http://sqlfiddle.com/#!3/4ab7e看到它。我需要的是从每个项目中提取具有至少一(1)个缺陷或违规的车辆数量。在下面的模式中,项目4的总数=两(2)辆车辆和项目9 = 1辆车。

需要的列是[Project_Name],[Vehicle_Type],[yy],[mm],[Total]

-- Vehicle Inspection Database --


-- Vehicle_Type Table

  CREATE TABLE VehicleType
  ([VehicleTypeId] int,
   [Type] varchar (36));

  INSERT INTO VehicleType ([VehicleTypeId],[Type])
  VALUES (1, 'Light Vehicle'),
         (2, 'Tanker'),
         (3, 'Goods');


-- Car Table

CREATE TABLE Vehicle  
  ([VehicleID] varchar(36), 
   [PlateNo] varchar(36),
   [VehicleTypeId] int,
   [Project] int);   

  INSERT INTO Vehicle ([VehicleID], [PlateNo],[VehicleTypeId], [Project])
  VALUES('A57D4151-BD49-4B44-AF10-000F1C298E05', '8112AG', 1, 4),
        ('C7095628-AE88-4DD0-A4FD-00363EAB767F', '60070 AD2', 2, 9),
        ('E714CCD7-E56C-46A8-89D5-003CA5BF6094', '68823 AD1', 3, 9);

-- Event Table

  CREATE TABLE Event    
  ([EventID] int, 
   [VehicleID] varchar(36), 
   [EventTime] smalldatetime, 
   [TicketStatus] varchar (10)) ;    

 INSERT INTO Event([EventID], [VehicleID], [EventTime], TicketStatus) 
 VALUES (1, 'A57D4151-BD49-4B44-AF10-000F1C298E05', '20130701', 'Open'),  
        (2, 'A57D4151-BD49-4B44-AF10-000F1C298E05', '20130702', 'Close'),
        (3, 'A57D4151-BD49-4B44-AF10-000F1C298E05', '20130703', 'Close'),
        (4, 'C7095628-AE88-4DD0-A4FD-00363EAB767F','20130705', 'Open'),
        (5, 'C7095628-AE88-4DD0-A4FD-00363EAB767F','20130710', 'Open'); 


-- Event_Defects Table

  CREATE TABLE EventDefects
  ([EventDefectsID] int, 
   [EventID] int, 
   [Status] varchar(15), 
   [DefectID] int) ; 

 INSERT INTO EventDefects ([EventDefectsID], [EventID], [Status], [DefectID]) 
 VALUES 

    -- 1st Inspection for PlateNo. 8112AG

        (1, 1, 'YES', 1),
        (2, 1, 'NO', 2),
        (3, 1, 'YES',3),
        (4, 1, 'N/A', 4),
        (5, 1, 'N/A', 5),

    -- 2nd Inspection for PlateNo. 8112AG 

        (6, 2, 'NO', 1),
        (7, 2, 'NO', 2),
        (8, 2, 'NO', 3),
        (9, 2, 'N/A', 4),
        (10,2, 'N/A', 5),

    -- 3rd Inspection for PlateNo. 8112AG

        (11, 3, 'NO', 1),
        (12, 3, 'NO', 2),
        (13, 3, 'NO', 3),
        (14, 3, 'NO', 4),
        (15, 3, 'NO', 5),

    -- 1st Inspection for PlateNo. 60070 AD2

        (16, 3, 'NO', 1),
        (17, 3, 'NO', 2),
        (18, 3, 'NO', 3),
        (19, 3, 'N/A', 4),
        (20, 3, 'N/A', 5);


-- Defects Table

 CREATE TABLE Defects
   ([DefectID] int, 
    [DefectsName] varchar (36), 
    [DefectClassID] int) ; 

 INSERT INTO Defects ([DefectID], [DefectsName], [DefectClassID]) 
 VALUES (1, 'TYRE', 1),
        (2, 'BRAKING SYSTEM', 1),
        (3, 'MIRRORS AND WINDSCREEN', 2),
        (4, 'OVER SPEEDING', 3),
        (5, 'NOT WEARING SEATBELTS', 3);

-- Defect_Class Table

  CREATE TABLE DefectClass
    ([Description] varchar (15), 
     [DefectClassID] int) ; 

INSERT INTO DefectClass ([DefectClassID], [Description]) 
VALUES (1, 'CATEGORY A'),
    (2, 'CATEGORY B'),
    (3, 'CATEGORY C'); 

1 个答案:

答案 0 :(得分:0)

将所有连接作为内部连接进行...并且它将消除空的记录。

你能检查一下这对你有用吗?

SELECT Vehicle.VehicleID from Vehicle
INNER JOIN Event ON Vehicle.VehicleID = Event.VehicleID
INNER JOIN EventDefects ON EventDefects.EventID = Event.EventID
INNER JOIN Defects ON EventDefects.DefectID = Defects.DefectID
GROUP BY Vehicle.VehicleID;