SQL MS Access查询的排除条件

时间:2016-11-29 02:49:02

标签: sql vb.net vba ms-access

要求:生成一个查询,根据电话号码输出数据,以响应用户输入为StartDate,EndDate和Upper Data Threshold。

这些输入变量是查询的边界。因此,SELECT语句相应地写在下面。

唯一需要注意的是,如果一个电话号码的单个记录超过了上限数据,则不应输出与该违规电话号码相关联的所有电话号码记录,而不管该电话号码的其他记录是什么t违反了数据阈值。以下是样本输入和预期输出:

User input Start Date: 1/15/2015
User input End Date: 11/15/2015
User input Upper Data Threshold in kB: 50

[Master] Table in Access:
Invc Date  Mobile Nbr     PktDtVol   
---------  ----------     --------   
1/15/15   647-409-8206    48kB
2/15/15   647-409-8206    33kB
3/15/15   647-409-8206    8000kB
4/15/15   647-409-8206    20kB
5/15/15   647-409-8206    10kB
6/15/15   647-409-8206    0kB
7/15/15   718-500-2311    3kB
8/15/15   718-500-2311    45kB
9/15/15   718-500-2311    25kB
10/15/15  514-300-3311    33kB
11/15/15  514-300-3311    20kB

[Temp_Table]中的预期输出:

Invc Date  Mobile Nbr     PktDtVol    Difference in Days 
---------  ----------     --------   -------------------
7/15/15    718-500-2311    3kB             304
8/15/15    718-500-2311    45kB            304
9/15/15    718-500-2311    25kB            304
10/15/15   514-300-3311    33kB            304
11/15/15   514-300-3311    20kB            304

我目前的解决方案:

PARAMETERS [Start Date] DateTime, [End Date] DateTime, [Upper Bound
Usage in KB] IEEEDouble;

SELECT [Master].[Invc Date], [Master].PktDtVol, [Master].[Mobile Nbr],
DateDiff("d",[Start Date],[End Date]) AS [Difference in days] INTO
Temp_Table
FROM [Master]
WHERE ((([Master].[Invc Date]) >= [Start Date] And
       ([Master].[Invc Date])<=[End Date]) AND
      (([Master].PktDtVol)<= [Upper Bound Usage in KB]));

647-409-8206记录不会出现在输出中,因为它只用了1个记录就超过了50kB(第3个记录),所以所有647-409-8206记录都会被相应省略。

请欣赏任何帮助!谢谢!

2 个答案:

答案 0 :(得分:1)

首先编写一个子查询,选择行违反阈值的所有(不同)移动号码。然后从表中选择所有行WHERE [Mobile Nbr] NOT IN (subquery)

答案 1 :(得分:1)

这非常粗糙,你的设计需要工作,但它应该给你一个开始。

PARAMETERS [Start Date] DATETIME, [End Date] DATETIME, [Upper Bound Usage in KB]
IEEEDOUBLE;

SELECT m.[invc date],
       m.[mobile nbr],
       m.[pktdtvol],
       Datediff("d", [start date], [end date]) AS [Difference in days]
INTO   temp_table
FROM   master AS m
WHERE  m.[invc date] >= [start date]
       AND m.[invc date] <= [end date]
       AND m.[mobile nbr] NOT IN
           (SELECT q.[mobile nbr]
            FROM   master q
            WHERE  q.pktdtvol >= [upper bound usage in kb])