检查哪些WHERE标准给出了最大记录

时间:2016-10-18 18:41:42

标签: sql ms-access

我有一个SQL语句:

SELECT * FROM CRMCSVFILE 
WHERE Email_Address_Txt = 'someemail' 
OR TELEPHONE_NUM = '123456789' 
OR DRIVER_LICENSE_NUM = 'fdisa124' 
OR RENTER_NAME_TXT = 'bob'

有没有办法告诉这些OR标准中哪一个吐出最多的记录?

我的表可以有多个上述任何字段的重复,我想知道WHERE语句中列出的哪些字段返回了最大结果,这可能在SQL中吗?

1 个答案:

答案 0 :(得分:1)

使用子查询:

SELECT f.*
FROM (SELECT f.*,
             IIF(Email_Address_Txt = 'someemail', 1, 0) as condition_1,
             IIF(TELEPHONE_NUM = '123456789', 1, 0) as condition_2,
             IIF(DRIVER_LICENSE_NUM = 'fdisa124' , 1, 0) as condition_3,
             IIF(RENTER_NAME_TXT = 'bob', 1, 0) as condition_4
      FROM CRMCSVFILE f
     ) f
WHERE condition_1 > 0 or condition_2 > 0 or condition_3 > 0 or condition_4 > 0;

这为每个条件提供了一个指标,因此您可以看到每行匹配的内容。

编辑:

哦,如果这就是你想要的:

SELECT condition_1, condition_2, condition_3, condition_4, count(*)
FROM (SELECT f.*,
             IIF(Email_Address_Txt = 'someemail', 1, 0) as condition_1,
             IIF(TELEPHONE_NUM = '123456789', 1, 0) as condition_2,
             IIF(DRIVER_LICENSE_NUM = 'fdisa124' , 1, 0) as condition_3,
             IIF(RENTER_NAME_TXT = 'bob', 1, 0) as condition_4
      FROM CRMCSVFILE f
     ) f
WHERE condition_1 > 0 or condition_2 > 0 or condition_3 > 0 or condition_4 > 0
GROUP BY condition_1, condition_2, condition_3, condition_4
ORDER BY count(*) DESC;