0

我有疑问:

;
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 '%test%' 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 '%test%' COLLATE SQL_Latin1_General_Cp1251_CI_AS
       OR p.name like '%test%' COLLATE SQL_Latin1_General_Cp1251_CI_AS )
)
SELECT p.name
FROM ranked
WHERE rnk = 1
;

此页面中,用户提供帮助并提出了几个可能的答案。

但在这种情况下,我们有一个非常大且复杂的查询。

请告诉我如何修改这个查询,首先我在有 $(在 $ 符号之后的序列上)时得到公司的结果,然后我得到其他公司的结果(按名称 asc 排序)?

真的还是假的?

结构表在这里

需要得到结果: 首先需要得到 p.name 和公司(f.name)当有$时(按优先级顺序,例如命名公司 $1,命名公司 $2,命名公司 $3,...),然后需要得到结果与其他公司(按 p.name 排序)

4

0 回答 0