SQL如何将其转换为SubQuery?

时间:2015-06-11 00:07:22

标签: sql sql-server

学习,善待。

我正在努力了解这是如何运作的,并且我已经完成了几次成功的转换,但是这个我很难过。

如何获取此代码并将其转换为子查询?我有点失落。

SELECT o.FirstName + ' ' + o.LastName AS Driver, COUNT(DISTINCT s.vehicleID) NoOfBusesUsed
FROM Operators AS o, Runs AS r, Schedules AS s JOIN Trips AS t
ON s.scheduleID = t.scheduleID 
WHERE r.BidDate BETWEEN '09/01/2004' AND '09/30/2004'
GROUP BY o.FirstName + ' ' + o.LastName
HAVING COUNT(s.vehicleID) > 1

以下是我的表格设置方式。如果需要更多信息,我可以发帖。

CREATE TABLE Operators
    (
    SeniorityNumber     char(4)     NOT NULL
            CONSTRAINT ck_Operators_Seniority 
            CHECK (SeniorityNumber LIKE '[0-9][0-9][0-9][0-9]'), 
    FirstName               varchar(25) NOT NULL, 
    LastName                varchar(35) NOT NULL, 
    HireDate                    smalldatetime
            CONSTRAINT ck_Operators_HireDate CHECK (HireDate <=Getdate())
    )

CREATE TABLE Trips
    (
    RouteNumber         varchar(4)      NOT NULL, 
    StartLocation           varchar(50) NOT NULL, 
    StartTime               time                NOT NULL, 
    EndLocation         varchar(50) NOT NULL, 
    EndTime                 time                NOT NULL,
    EffectiveDate           smalldatetime   NOT NULL
        CHECK (EffectiveDate >= cast('1/1/2000' as smalldatetime)),
    CONSTRAINT ck_Trips_StartEnd CHECK (EndTime > StartTime)
    )

CREATE TABLE Vehicles
    (
    Manufacturer            varchar(50)
        DEFAULT 'Gillig',
    Model                   varchar(50), 
    ModelYear               int
        DEFAULT DatePart(yyyy,GetDate())
        CHECK (ModelYear <= DatePart(yyyy,GetDate())),

    PurchaseDate            smalldatetime
    )

    GO
    ALTER TABLE operators
        ADD OperatorID int IDENTITY --Primary Key

    GO
    ALTER TABLE Operators
        ADD CONSTRAINT pkOperators Primary key (OperatorID)

    ALTER TABLE Vehicles
        ADD VehicleID   int IDENTITY Primary Key

    ALTER TABLE Trips
        ADD TripID      int IDENTITY Primary key
    GO

CREATE TABLE Runs
(
    RunID           int     IDENTITY        NOT NULL        Primary Key,
    OperatorID  int     NOT NULL        REFERENCES Operators,
    BidDate     date    NOT NULL        
        CONSTRAINT ckRunBidDate CHECK
        (biddate <= dateadd(mm,6,getdate())) --getdate() + 180
)
GO

CREATE TABLE Schedules
(
    ScheduleID  int     IDENTITY        Primary Key,
    RunID           int     NOT NULL,
    VehicleID       int     NOT NULL,
    CONSTRAINT fk_Schedules_Runs FOREIGN KEY (RunID)
        REFERENCES Runs(RunID),
    CONSTRAINT fk_Schedules_Vehicles FOREIGN KEY (VehicleID)
        REFERENCES Vehicles
)   

ALTER TABLE Trips
    ADD ScheduleID  int NULL REFERENCES Schedules

1 个答案:

答案 0 :(得分:1)

如果要将查询用作子查询,可以使用WITH语句或派生表,如:

  • With

    ;WITH subQuery AS (
        /* Your query here */
    )
    SELECT *
    FROM subQuery
    
  • 派生表

    SELECT *
    FROM (
        /* your query here */
        ) As subQuery
    

我认为你应该使用这样的查询:

SELECT 
    o.FirstName + ' ' + o.LastName AS Driver, 
    DT.cnt AS NoOfBusesUsed
FROM 
    Operators AS o
JOIN
    (SELECT
        r.OperatorID,
        COUNT(DISTINCT s.VehicleID) AS cnt
     FROM
        Schedules s
     JOIN
        Runs r ON s.RunID = r.RunID
    ) AS DT
    ON  DT.OperatorID = o.OperatorID
WHERE
    ISNULL(DT.cnt, 0) > 1