如何正确选择? (第2部分)

时间:2013-03-22 06:35:01

标签: sql sql-server sql-server-2008

我有疑问

;
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
;
this page 中的

帮助并提出了几个可能的答案。

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

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

真的与否?

Structure tables here

需要获得结果: 当需要 $ 时(优先级顺序,例如名称公司$ 1,名称公司$ 2,名称公司$ 3,...),首先需要获得公司(f.name)的p.name需要与其他公司获得结果(按p.name排序)

0 个答案:

没有答案