如何将表与cte和join查询相结合

时间:2015-10-07 12:19:18

标签: sql

如何将这些表与cte和连接查询相结合?

enter image description here

keydown

1 个答案:

答案 0 :(得分:0)

如果您正在寻找组合两个查询,CTE和其他查询。然后你可以这样做:

WITH CTE 
AS
(
    SELECT *,
        Rn = ROW_NUMBER() OVER(PARTITION BY productCode ORDER BY productId)
    FROM sellingprice
), CTE2
AS
( -- you can put here the second query you need to combine with the first cte
    SELECT
      sp.productid, sp.productname, ...
    from sellingPrice sp
    inner join cost ...
)
-- here the two queries are accessible from cte, cte2 
SELECT *,
  productcode =  CASE
               WHEN Rn = 0 THEN productCode
               ELSE productCode+ CHAR(65 + Rn -1)
                END, .... -- you can select other columns
from CTE c1
INNER JOIN CTE2 c2 ON c1.productid = c2.productid