Question

I want to create new rows from existing row in SQL server view

Original view is like below

              salecount   

  HP           56
  Dell         32
  Acer         21
  Asus         55
 ..........many rows

Output view is like below

                    salecount
HP Laptop           44.8 (56*0.8)

HP Desktop          11.2 (56*0.2)
  Dell              32
  Acer              21
  Asus              55

 ..........many rows

I don't know how to use insert into inside CTE

,CTE_SalesSeparated
AS(
SELECT [Date]
      ,[Country]
      ,[Year]
      ,[Brand]
     -- ,Insert into 'HP Laptop'  = [SaleCount]*0.8
     --              'HP Desktop' = [SaleCount]*0.2
FROM CTE_Sales
)

Thanks everyone.

Était-ce utile?

La solution

Are you trying to do something like this?

WITH CTE_SalesSeparated AS (
      SELECT s.[Date], s.[Country], s.[Year],
             coalesce(ld.[Brand] + ' ' + which, s.Brand) as Brand,
             s.SalesCount * coalesce(ld.factor, 1) as SalesCOunt
      FROM CTE_Sales s LEFT JOIN
           (SELECT 'HP' as Brand, 'LapTop' as which, 0.8 as factor UNION ALL
            SELECT 'HP' as Brand, 'DeskTop' as which, 0.2 as factor
           ) ld
           ON s.Brand = ld.Brand
     )
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top