加入sql查询

时间:2014-03-24 11:14:13

标签: sql sql-server

你能帮忙解决这两个问题吗?

查询一个:

declare @startdate datetime
declare @enddate datetime
set @datainicio='2014-03-01'
set @datafim='2014-03-03'
select right([Location Code],4) as Vehicle,MIN(CAST(CAST([date]AS DATE) AS DATETIME) +
    CAST([entry time]AS TIME)) as DaeaMin,min([Veihicle Kms]) as KmsMin,MAX(CAST(CAST([date]AS DATE) AS DATETIME) +
    CAST([entry time]AS TIME)) as DateMax,max([Veihicle Kms])as KmsMax
where quantity>=0 and [Location code] like 'v%'  and [item no_]='601.0001' and ([date] between @startdate and @enddate)
group by [Location Code]

查询二:

 SELECT Vehicles.Designation as Vehicle,
 SUM(Locations.DistanceFromLastLocation)/1000 as
 KMS,convert(varchar(10),LocationDate,120) as Date 
 FROM Locations INNER JOIN Vehicles ON Locations.VehicleId = Vehicles.VehicleId
 GROUP BY Vehicles.Designation,LocationDate

我想通过Vehicle加入这两个查询,查询二中的日期必须介于查询之一的datemin和datemax之间。

帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

由于您在第一次查询中没有提及from子句,我假设第一次查询您从“地点”表中提取数据。 以下答案可能有用。

;with temp as 
(
    SELECT Vehicles.Designation as Vehicle, 
           sum(Locations.DistanceFromLastLocation)/1000 as KMS,
           convert(varchar(10),Locations.LocationDate,120) as Date 
    FROM Locations 
    INNER JOIN Vehicles 
    ON Locations.VehicleId = Vehicles.VehicleId 
    group by Vehicles.Designation,Locations.LocationDate 
)

select right(Locations.[Location Code],4) as Vehicle,
       MIN(CAST(CAST([Locations.date]AS DATE) AS DATETIME) + CAST([Locations.entry time]AS TIME))
       as DateaMin,
       min([Locations.Veihicle Kms]) as KmsMin,
       MAX(CAST(CAST([Locations.date]AS DATE) AS DATETIME) + CAST([Locations.entry time]AS TIME))
       as DateMax,
       max([Locations.Veihicle Kms])as KmsMax 
FROM Locations
Inner Join temp on
temp.Vehicle = right([Locations.Location Code],4)
where Locations.quantity>=0 
and [Locations.Location code] like 'v%' 
and [Locations.item no_]='601.0001' 
and ([Locations.date] between @startdate and @enddate)
and temp.[Date] between 
MIN(CAST(CAST([Locations.date]AS DATE) AS DATETIME) + CAST([Locations.entry time]AS TIME)) 
and 
MAX(CAST(CAST([Locations.date]AS DATE) AS DATETIME) + CAST([Locations.entry time]AS TIME))

答案 1 :(得分:0)

希望这会有所帮助,并且比其他答案更容易理解。注意我已经包含了拼写错误;)

declare @startdate datetime
declare @enddate datetime

set @datainicio='2014-03-01'
set @datafim='2014-03-03'

SELECT * 
FROM (
    select right([Location Code],4) as Vehicle,MIN(CAST(CAST([date]AS DATE) AS DATETIME) +
    CAST([entry time]AS TIME)) as DaeaMin,min([Veihicle Kms]) as KmsMin,MAX(CAST(CAST([date]AS DATE) AS DATETIME) +
    CAST([entry time]AS TIME)) as DateMax,max([Veihicle Kms])as KmsMax
    FROM YOURTABLE
    where quantity>=0 and [Location code] like 'v%'  and [item no_]='601.0001' and ([date] between @startdate and @enddate)
    group by [Location Code]
) x
INNER JOIN (
    SELECT Vehicles.Designation as Vehicle,
     SUM(Locations.DistanceFromLastLocation)/1000 as
     KMS,convert(varchar(10),LocationDate,120) as Date 
     FROM Locations INNER JOIN Vehicles ON Locations.VehicleId = Vehicles.VehicleId
     GROUP BY Vehicles.Designation,LocationDate
) y
ON x.Vehicle = y.Vehicle
AND y.DATE BETWEEN x.DaeaMin AND x.DateMax