2016-04-25 117 views
0

我想拉開具有大於30,60和90天的出發日期(全部來自同一文件)的Folio ID。如果其中一個ID的日期少於30天,我不希望這個ID被拉下。 只有當該ID的出發日期大於30,60或90天時,我也希望爲帶有空白(空?)出發日期的作品集ID拉取日期。Excel/VBA/ADO - SQL查詢不返回正確的結果

我嘗試了左,內,右和嵌套連接的多種組合(感謝提供給前一個問題的答案)。我已經接近,但由於某種原因,我的查詢並沒有拉出任何空白的出發日期,即使我知道它們存在。我的查詢到目前爲止如下:

sSQL = "SELECT t1.[Folio Type], t1.[Folio ID],t1.[Departure Date],t1.[Folio Total]" 
    sSQL = sSQL & " FROM" 
    sSQL = sSQL & " (SELECT [Folio Type], [Folio ID],[Folio Total],[Departure Date]" 
    sSQL = sSQL & " FROM [Individual Folios$B2:O150000] GROUP BY [Folio Type],[Folio ID],[Folio Total],[Departure Date]" 
    sSQL = sSQL & " HAVING [Folio Total] <> 0 and [Departure Date] < " & G30E & ") As t1 " 
    sSQL = sSQL & " LEFT JOIN" 
    sSQL = sSQL & " (SELECT [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
    sSQL = sSQL & " FROM [Individual Folios$B2:O150000]" 
    sSQL = sSQL & " group by [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
    sSQL = sSQL & " HAVING [Folio Total] <> 0 and ([Departure Date] > " & G30E & " or [Departure Date] is null)) as t2" 
    sSQL = sSQL & " on t1.[Folio ID] = t2.[Folio ID]" 
    sSQL = sSQL & " where t2.[Folio ID] is null" 

我也有類似的查詢(即試圖完成同樣的事情,第一次查詢)使用嵌套的聯接。我認爲這兩個查詢應該拉相同的值,但總數是不同的:

sSQL = "SELECT t1.[Folio Type], t1.[Folio ID],t1.[Departure Date],t1.[Folio Total]" 
sSQL = sSQL & " FROM" 
sSQL = sSQL & " ((SELECT [Folio Type], [Folio ID],[Folio Total],[Departure Date]" 
sSQL = sSQL & " FROM [Individual Folios$B2:O150000] GROUP BY [Folio Type],[Folio ID],[Folio Total],[Departure Date]" 
sSQL = sSQL & " HAVING [Folio Total] <> 0 and [Departure Date] < " & G30E & ") As t1 " 
sSQL = sSQL & " LEFT JOIN" 
sSQL = sSQL & " (SELECT [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
sSQL = sSQL & " FROM [Individual Folios$B2:O150000]" 
sSQL = sSQL & " group by [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
sSQL = sSQL & " HAVING [Folio Total] <> 0 and [Departure Date] > " & G30E & ") as t2" 
sSQL = sSQL & " on t1.[Folio ID] = t2.[Folio ID])" 
sSQL = sSQL & " LEFT JOIN" 
sSQL = sSQL & " (SELECT [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
sSQL = sSQL & " FROM [Individual Folios$B2:O150000]" 
sSQL = sSQL & " group by [Folio Type],[Folio ID],[Departure Date],[Folio Total]" 
sSQL = sSQL & " HAVING [Folio Total] <> 0 and [Departure Date] is null) as t3" 
sSQL = sSQL & " on t1.[Folio ID] = t3.[Folio ID]" 
sSQL = sSQL & " where t2.[Folio ID] is null or t3.[Folio ID] is null" 

任何幫助,非常感謝。

回答

1

嘗試使用一系列UNION查詢。每個查詢都有它的WHERE子句來限制返回的記錄。

由於大部分的SELECT的是我會使用WITH子句相同:

WITH A AS(SELECT * FROM [FoliosXXX]) 

SELECT * FROM A WHERE [Departure DATE] <= 30 
UNION 
SELECT * FROM A WHERE [Departure DATE] > 30 AND [Departure DATE] <= 60 
UNION 
SELECT * FROM A WHERE [Departure DATE] >= 60 

您可以進行選擇用於是一個複雜的/加盟/等。查詢。不確定GROUPing需要將它添加到後面的SELECT中。

一個變體是使用UNION,並在SELECT中使用NOT IN子句來排除已處理的記錄。

+0

OP用於連接到Excel工作簿的Jet/ACE SQL引擎不支持窗口函數。 WITH()不起作用。 – Parfait

+0

你的權利 - 排序 - WITH以上是一個SQL函數,而不是Windows函數。但是你很可能是正確的,不支持Jet或其他非SQL Server驅動程序。工會可能會工作,只需要更多的打字。 – rheitzman