2014-03-24 175 views
0

你能幫助這兩個查詢嗎?加入sql查詢

查詢之一:

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 

我想加入這兩個查詢的車輛和查詢兩個日期必須是從查詢一個datemin和datemax之間。

幫助,謝謝。

+0

你在MySQL中做了很多*什麼? – Rachcha

+2

第一個查詢中的FROM子句在哪裏? – Prashant16

+0

我們可以得到一些樣本起始數據和期望的結果嗎?無論如何,我們都需要缺少'FROM'子句/表引用。 –

回答

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 
0

由於您在第一個查詢中沒有提到子句,因此我假定您要從「位置」表中獲取數據的第一個查詢爲 以下回答可能有用。

;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)) 
+0

鑑於'MAX(..)'和'MIN(..)'是聚合列,你需要一個「GROUP BY」 - 這應該引發某種形式的語法錯誤。你也不想使用'BETWEEN' [帶日期/時間/時間戳類型](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the -devil-in-common.aspx),特別是在SQL Server中。然而,這似乎正在朝着可信的第一次努力邁進。 –

+0

@ Clockwork-Muse:說不應該在date/timstamp類型中使用'BETWEEN',這有點強烈嗎?儘管確實需要知道自己在做什麼(提示:請閱讀文檔),但沒有理由認爲它適用於或不適用於此處的數據。 Aaron在鏈接文章中提出了一個有效的觀點,即有些人可能會誤解操作員,但只是把它扔掉,因爲這不是解決方案。 – deroby

+0

@deroby - 「BETWEEN」的問題在於,它傾向於促進對連續值範圍的錯誤理解:它們具有不連續的結束值。例如,在發佈此評論之前,沒有具體的「最後一刻」時間,這只是「發佈之前」。這適用於除顯式積分計數以外的每種類型,其中出於一致性原因應避免使用「BETWEEN」;因此,我的立場是不應該使用「BETWEEN」(並且你認爲我之前採取了強硬立場!)。我可能需要爲此寫一篇博客文章...... –