我正在尝试提取出发日期超过30天,60天和90天的Folio ID(全部来自同一个文件)。如果其中一个ID也有一个日期少于30天的事件,我不希望该ID被拉出。 我还希望仅当该ID的出发日期大于30,60或90天时,为Folio ID提取空白(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 & " 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"
非常感谢任何帮助。
答案 0 :(得分: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
您可以使SELECT for A成为复杂/加入/等。查询。不确定GROUPing是否需要将其添加到以后的SELECT中。
变体是使用UNIONs并在SELECT中使用NOT IN子句来排除已经处理的记录。