2

我有以下查询:

SELECT DISTINCT TOP 20 
    f.id_service as f_id_service,
    f.id_city as f_id_city,
    f.name as f_name,
    f.address as f_address,
    f.business as f_business,
    f.web as f_web,
    f.phone as f_phone,
    f.id_firm as f_id_firm
FROM
    Firm f
        left join
    Price p ON p.id_service = f.id_service
        AND p.id_city = f.id_city
        AND p.id_firm = f.id_firm
WHERE
    f.blocked = '0'
        AND (f.name LIKE 'авто%'
        OR f.phone LIKE 'авто%')
        AND (f.phone != '' OR f.address != '')
        AND f.id_city = '73041'
        AND f.dogovor = '1'
ORDER BY f.name ASC

此查询显示 TOP 20 具有唯一性的行f.name

当我需要选择具有唯一 f.name 的下 20 行时,我使用下一个查询:

SELECT DISTINCT TOP 20
    f.id_service AS f_id_service,
    f.id_city AS f_id_city,
    f.name AS f_name,
    f.address AS f_address,
    f.business AS f_business,
    f.web AS f_web,
    f.phone AS f_phone,
    f.id_firm AS f_id_firm
FROM
    Firm f
        LEFT JOIN
    Price p ON p.id_service = f.id_service
        AND p.id_city = f.id_city
        AND p.id_firm = f.id_firm
WHERE
    f.name NOT IN (SELECT DISTINCT TOP 20
            f.name
        FROM
            Firm f
        WHERE
            f.blocked = '0'
                AND (f.name LIKE '????%'
                OR f.phone LIKE '????%')
                AND (f.phone != '' OR f.address != '')
                AND f.id_city = '73041'
        ORDER BY f.name ASC)
        AND f.dogovor = '1'
        AND f.blocked = '0'
        AND (f.name LIKE '????%'
        OR f.phone LIKE '????%')
        AND (f.phone != '' OR f.address != '')
        AND f.id_city = '73041'
        AND f.dogovor = '1'
ORDER BY f.name ASC

但我看到在最后一个查询中,我从第一个查询中查询具有唯一 f.name 的选择行。

例如:

结果第一个查询(选择 TOP 20 行): 第一次查询

结果第二个查询(选择 NEXT TOP 20 行): 第二次查询

您如何在第二个图像中看到第二个查询具有来自第一个图像的行(结果第一个查询)。

请告诉我哪里错误以及如何写正确?

4

3 回答 3

1

试试这个——

SELECT DISTINCT TOP 20 
    f.id_service AS f_id_service, 
    f.id_city AS f_id_city, 
    f.name AS f_name, 
    f.[address] AS f_address, 
    f.business AS f_business, 
    f.web AS f_web, 
    f.phone AS f_phone, 
    f.id_firm AS f_id_firm 
FROM dbo.Firm f
WHERE f.blocked = '0' 
    AND (f.name LIKE 'авто%' OR f.phone LIKE 'авто%') 
    AND f.phone + f.[address] != ''
    AND f.id_city='73041' 
    AND f.dogovor='1' 
ORDER BY f.name


SELECT *
FROM (
    SELECT DISTINCT 
        f.id_service AS f_id_service, 
        f.id_city AS f_id_city, 
        f.name AS f_name, 
        f.[address] AS f_address, 
        f.business AS f_business, 
        f.web AS f_web, 
        f.phone AS f_phone, 
        f.id_firm AS f_id_firm,
        row_id = ROW_NUMBER() OVER (ORDER BY f.name)
    FROM dbo.Firm f
    WHERE f.blocked = '0' 
        AND (f.name LIKE 'авто%' OR f.phone LIKE 'авто%') 
        AND f.phone + f.[address] != ''
        AND f.id_city='73041' 
        AND f.dogovor='1' 
) d
WHERE d.row_id BETWEEN 21 AND 40
于 2013-05-22T08:36:38.230 回答
0

您使用的是哪种排序规则?也许这就是你的“where f.name not in”子句没有排除字符串的原因

当您选择似乎是表的 ID 时,您可以不使用“Distinct”,这将允许您按 ID 过滤掉前 20 条记录。

其中 f.id_service 不在(从公司 f 中选择前 20 个 f.id_service WHERE f.blocked='0' ...

于 2013-05-22T08:37:55.367 回答
0

首先,这可能是语言障碍,但让我们明确一点,您的第一个查询不会显示唯一的 f.name 而是所有列的唯一组合。

此外,以这种方式使用 TOP 和 DISTINCT 是一个非常糟糕的主意。无法确保您每次都能获得相同的结果。

最后你的 NOT IN 子句不见了AND f.dogovor='1'

于 2013-05-22T08:38:38.060 回答