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"
任何幫助,非常感謝。
OP用於連接到Excel工作簿的Jet/ACE SQL引擎不支持窗口函數。 WITH()不起作用。 – Parfait
你的權利 - 排序 - WITH以上是一個SQL函數,而不是Windows函數。但是你很可能是正確的,不支持Jet或其他非SQL Server驅動程序。工會可能會工作,只需要更多的打字。 – rheitzman