Excel/VBA/ADO - SQL 查询未返回正确结果
Excel/VBA/ADO - SQL query not returning correct results
我正在尝试提取出发日期大于 30、60 和 90 天的 Folio ID(全部来自同一个文件)。如果其中一个 ID 还发生了日期小于 30 天的事件,我不想删除该 ID。
仅当该 ID 的出发日期也大于 30、60 或 90 天时,我还希望为具有空白(空?)出发日期的 Folio ID 提取日期。
我尝试了多种左、内、右和嵌套连接的组合(感谢对上一个问题提供的答案)。我已经接近了,但出于某种原因,我的查询没有提取任何空白的出发日期,即使我知道它们存在。到目前为止,我的查询如下:
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"
非常感谢任何帮助。
尝试使用一系列 UNION 查询。每个查询都有其 on 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
您可以将 A 的 SELECT 设为 complex/joined/etc。询问。不确定是否需要将 GROUPing 添加到后面的 SELECT 中。
一种变体是使用 UNION,在 SELECT 中使用 NOT IN 子句来排除已处理的记录。
我正在尝试提取出发日期大于 30、60 和 90 天的 Folio ID(全部来自同一个文件)。如果其中一个 ID 还发生了日期小于 30 天的事件,我不想删除该 ID。 仅当该 ID 的出发日期也大于 30、60 或 90 天时,我还希望为具有空白(空?)出发日期的 Folio ID 提取日期。
我尝试了多种左、内、右和嵌套连接的组合(感谢对上一个问题提供的答案)。我已经接近了,但出于某种原因,我的查询没有提取任何空白的出发日期,即使我知道它们存在。到目前为止,我的查询如下:
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"
非常感谢任何帮助。
尝试使用一系列 UNION 查询。每个查询都有其 on 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
您可以将 A 的 SELECT 设为 complex/joined/etc。询问。不确定是否需要将 GROUPing 添加到后面的 SELECT 中。
一种变体是使用 UNION,在 SELECT 中使用 NOT IN 子句来排除已处理的记录。