0

再会。

我有sql查询:

SELECT TOP 20 
    p.id_price as p_id_price, 
    p.id_service as p_id_service, 
    p.name as p_name, 
    p.name_original as p_name_original, 
    p.id_producer_country as p_id_producer_country, 
    p.id_firm as p_id_firm, 
    f.name as f_name, 
    f.priority as f_priority, 
    f.address as f_address, 
    f.phone as f_phone, 
    city.name as city_name, 
    pc.name as pc_name 
FROM Price p 
    left join Firm f on f.id_service = p.id_service AND f.id_city = p.id_city AND f.id_firm = p.id_firm 
    left join City city on city.id_city = p.id_city left join Producer_country pc on pc.id_producer_country = p.id_producer_country 
    left join Dogovor d on d.id_service=p.id_service AND d.id_city=p.id_city AND d.id_firm=p.id_firm 
WHERE 
    id_price NOT IN ( 
        SELECT TOP 20 p.id_price 
        FROM Price p 
            left join Firm f on f.id_service = p.id_service AND f.id_city = p.id_city AND f.id_firm = p.id_firm 
            left join City city on city.id_city = p.id_city 
            left join Producer_country pc on pc.id_producer_country = p.id_producer_country 
            left join Dogovor d on d.id_service=p.id_service AND d.id_city=p.id_city AND d.id_firm=p.id_firm 
        WHERE p.id_city='73041' AND p.include='1' AND p.blocked='0' AND f.blocked='0' AND f.dogovor='1' 
                AND d.end_date >= '20131108 02:37:03' 
                AND (p.name LIKE '% tes_ %' OR p.name LIKE 'tes_ %' OR p.name LIKE '% _tes_%') 
        ORDER BY f.priority DESC 
                    ) 
AND p.id_city='73041' 
AND p.include='1' 
AND p.blocked='0' 
AND f.blocked='0' 
AND f.dogovor='1' 
AND d.end_date >= '20131108 02:37:03' 
AND (p.name LIKE '% tes_ %' OR p.name LIKE 'tes_ %' OR p.name LIKE '% _tes_%') 

ORDER BY f.priority DESC

但它查询选择并输出具有相同字段f.name值的多行

请告诉我如何从具有相同字段f.name值的多行中仅选择一行 ?

请告诉我如何在同一个 f.name 中做行号,然后选择第一个数字?

4

2 回答 2

0
SELECT Top(20) * FROM (
SELECT  Row_Number()OVER(PARTITION BY f.name ORDER BY f.priority, p.datetime DESC) AS NameOrder, 
    p.id_price as p_id_price, 
    p.id_service as p_id_service, 
    p.name as p_name, 
    p.name_original as p_name_original, 
    p.id_producer_country as p_id_producer_country, 
    p.id_firm as p_id_firm, 
    f.name as f_name, 
    f.priority as f_priority, 
    f.address as f_address, 
    f.phone as f_phone, 
    city.name as city_name, 
    pc.name as pc_name 
FROM Price p 
    left join Firm f on f.id_service = p.id_service AND f.id_city = p.id_city AND f.id_firm = p.id_firm 
    left join City city on city.id_city = p.id_city left join Producer_country pc on pc.id_producer_country = p.id_producer_country 
    left join Dogovor d on d.id_service=p.id_service AND d.id_city=p.id_city AND d.id_firm=p.id_firm 
WHERE 
    id_price NOT IN ( 
        SELECT TOP 20 p.id_price 
        FROM Price p 
            left join Firm f on f.id_service = p.id_service AND f.id_city = p.id_city AND f.id_firm = p.id_firm 
            left join City city on city.id_city = p.id_city 
            left join Producer_country pc on pc.id_producer_country = p.id_producer_country 
            left join Dogovor d on d.id_service=p.id_service AND d.id_city=p.id_city AND d.id_firm=p.id_firm 
        WHERE p.id_city='495' AND p.include='1' AND p.blocked='0' AND f.blocked='0' AND f.dogovor='1' 
                AND d.end_date >= '20131108 02:37:03' 
                AND (p.name LIKE '% tes_ %' OR p.name LIKE 'tes_ %' OR p.name LIKE '% _tes_%') 
        ORDER BY f.priority DESC 
                    ) 
AND p.id_city='495' 
AND p.include='1' 
AND p.blocked='0' 
AND f.blocked='0' 
AND f.dogovor='1' 
AND d.end_date >= '20131108 02:37:03' 
AND (p.name LIKE '% tes_ %' OR p.name LIKE 'tes_ %' OR p.name LIKE '% _tes_%')
)
AS S WHERE S.NameOrder = 1
ORDER BY S.f_priority
于 2013-11-10T08:15:17.830 回答
0

Group BY不一定是一个简单的概念。所以是的,请停止分享 W3 学校的链接。最好解释一下。

如果您需要某种聚合,这意味着您想知道 John 出现了多少次,您需要在末尾添加一列COUNT(fname),然后group by列出您选择的每一列。

如果您只想显示您使用的最独特的值DISTINCT。因此,在您SELECT刚刚添加之后DISTINCT,您将只获得每列值唯一的那些行。

于 2013-11-08T23:06:20.483 回答