refactoring - Refactor SQL query to return results into rows instead of columns -


I have a SQL query that needs reconfiguration. Basically all the product types ordered by the specified customer receive the query. The problem is that the results are returned in the columns instead of the rows, to make the query more common, it needs to be replaced in another way.

So this is the query that returns:

  Name ProductType1 ProductType2 ProductType3 ------------- - ------------------------------------ Mark PT09 P15 PT33   < P>  And this is what it should be:   
  Name ProductType ---------------- Mark PT09 Mark P15 Marc PT33   

This is the query that I've simplified a bit:

  Select CustomerData.Name Product1.productType as ProductType1, Product2.productType ProductType2, ProductType3 as Product3.productType (Selection ProductID, Name Customer Order Where Custome R.ID = 111) Join CustomerData as Left (ProductID, ProductType as PC.Type CustomerProduct CP, as a CP.ProductID Selection, ProductCategory PC Where CustomerData.ProductID = Product1.ProductID on PC. Category = 'A' and CP.ProductCategoryID = PC.ID) Product Name as Product Type, ProductCype PC Where CustomerData.ProductID = Product1 PC.Category = 'B' and CP.ProductCategoryID = PC.ID on PRODUCTID) left as PRODUCT2 in the form of a special CP.ProductID as a JOIN (ProductID, PC.Type; ProductType; CustomerProduct from CP, ProductCategoryP Where is PC.Category = 'C' and CP.ProductCategoryID = PC.ID) Product3 as CustomerData.ProductID = Product1.ProductID on   

I have been thinking about splitting Joins in a separate stored proc and then call it according to the requirement of more product type, but I can not get this work. How should anyone get this job?

Things are really hard to do in columns

Assume that normalized Table customers, products and orders, you should not need to do anything more than just:

  Select C.customer_name, join P.product_type from customers, on C order o O.customer_id = C.customer_id Join Product P on O.product_id = P.product_id Where C.ID = 111   

If it does not work, include the table of contents listed.

Comments