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

このクエリは、一意の上位 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 を持つ行を選択してクエリを実行していることがわかります。

例えば:

結果の最初のクエリ (上位 20 行を選択): 最初のクエリ

結果の 2 番目のクエリ (NEXT TOP 20 行を選択): 2 番目のクエリ

2 番目の画像でどのように確認できるか 2 番目のクエリには、最初の画像 (最初のクエリの結果) からの行があります。

エラーの場所と正しい書き方を教えてください。

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

まず、言語の壁かもしれませんが、明確にしましょう。最初のクエリでは、一意の f.name は表示されませんが、すべての列の一意の組み合わせが表示されます。

また、そのような方法で TOP と DISTINCT を使用することは非常に悪い考えです。毎回同じ結果が得られることを確認する方法はありません。

最後に、NOT IN 句がありませんAND f.dogovor='1'

于 2013-05-22T08:38:38.060 に答える