美好的一天。
我有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中进行行号,然后选择第一个号码?
答案 0 :(得分:0)
Group BY
不一定是一个简单的概念。所以是的,请停止分享来自W3学校的链接。它更好地解释它。
如果你需要某种聚合意味着你想知道John出现了多少次,你需要在COUNT(fname)
之后添加一列,然后列出每列group by
选择。
如果您尝试显示最独特的值,请使用DISTINCT
。因此,在SELECT
添加DISTINCT
之后,您将只获得每个列值唯一的行。
答案 1 :(得分: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