Sql 如何为表中的每个公司只获得一个结果?
我有查询,但现在我需要将查询更改为数据库,以便在表Sql 如何为表中的每个公司只获得一个结果?,sql,sql-server-2008,Sql,Sql Server 2008,我有查询,但现在我需要将查询更改为数据库,以便在表Price中为每个公司只获得一个结果 为此,我添加了按p.id\U公司分组的行,然后得到下一个查询: 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_produ
Price
中为每个公司只获得一个结果
为此,我添加了按p.id\U公司分组的行,然后得到下一个查询:
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.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
WHERE p.id_city='73041'
AND p.include='1'
AND p.blocked='0'
AND f.blocked='0'
AND ( f.name LIKE 'Окно%' COLLATE SQL_Latin1_General_Cp1251_CI_AS OR p.name LIKE 'Окно%' COLLATE SQL_Latin1_General_Cp1251_CI_AS )
GROUP by p.id_firm ORDER BY p.name ASC
但如果我使用它,我会得到错误:
Msg 8120,16级,状态1,第2行
列“Price.id\u Price”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中
请告诉我更改此查询或进行其他查询的正确方法
构建所有您可以看到的表格
附言:对不起,不准确。按公司名称和产品名称搜索。如果搜索词包含在产品名称或公司名称中,并且有一些结果具有相同的id公司,则需要选择一个最合适的值,即搜索结果中的id公司应是唯一的。使用而不是分组
对每个公司的行分别编号。当为行编号()对行进行排序时,使用p.name,如'ö敪敪ö%'
条件将具有匹配产品的行放在其他行之前
下面是一个可能的示例:
;
WITH ranked AS (
SELECT
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.address as f_address,
f.phone as f_phone,
city.name as city_name,
pc.name as pc_name,
ROW_NUMBER() OVER (
PARTITION BY p.id_firm
ORDER BY
CASE -- this criterion puts matching products before non-matching ones
WHEN p.name LIKE 'Окно%' COLLATE SQL_Latin1_General_Cp1251_CI_AS
THEN 1 ELSE 2
END,
p.id_price -- you may use any sorting criteria at this point,
-- just ensure it makes the results predictable
) AS rnk
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
WHERE p.id_city='73041'
AND p.include='1'
AND p.blocked='0'
AND f.blocked='0'
AND ( f.name LIKE 'Окно%' COLLATE SQL_Latin1_General_Cp1251_CI_AS
OR p.name LIKE 'Окно%' COLLATE SQL_Latin1_General_Cp1251_CI_AS )
)
SELECT TOP 20
p_id_price,
p_id_service,
p_name,
p_name_original,
p_id_producer_country,
p_id_firm,
f_name,
f_address,
f_phone,
city_name,
pc_name
FROM ranked
WHERE rnk = 1
-- the absence of ORDER BY makes your TOP 20 results indeterminate
;
基本上,这会对每个公司的行进行排序,然后只从所有公司中提取排名为1
的行,最终使每个公司都有一行。您可以尝试选择Distinct进行快速修复
表之间有哪些关系?请参考此示例[在此处输入链接说明][1][1]:好的,如果一家公司有多个人/服务/价格,您想要哪一个?您不能说“这不重要”,因为SQL Server需要知道。@HelloResh:在注释中,链接格式只支持[text](link)
模板。有关详细信息,请参阅。@LeoLoki您仍然需要具体说明。如果一家公司有两种产品,你想要哪种产品?