重构 SQL 查询以将结果返回到行而不是列中
我有一个需要重构的 SQL 查询。基本上,查询获取指定客户订购的所有产品类型。问题在于结果以列而不是行的形式返回。这需要以相反的方式进行更改以使查询更加通用。
这就是查询返回的内容:
Name ProductType1 ProductType2 ProductType3
--------------------------------------------------
Marc PT09 P15 PT33
这就是它应该的样子:
Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33
这是我简化了一点的查询:
SELECT
CustomerData.Name as Name
Product1.productType as ProductType1,
Product2.productType as ProductType2,
Product3.productType as ProductType3
FROM
(SELECT ProductID, Name
FROM
Customer
Orders
WHERE Customer.ID = 111
) as CustomerData
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'A'
AND CP.ProductCategoryID = PC.ID
) as Product1
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'B'
AND CP.ProductCategoryID = PC.ID
) as Product2
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'C'
AND CP.ProductCategoryID = PC.ID
) as Product3
on CustomerData.ProductID = Product1.ProductID
所以我一直在考虑将连接分割成一个单独的存储过程,然后调用它,因为我需要更多的产品类型,但我似乎无法让它工作。有人知道如何让它发挥作用吗?
I have a SQL query that need to be refactored. Basically the query gets all the producttypes ordered by a specified customer. The problem is that the results are returned in columns instead of rows. This needs to be changed the other way around to make the query more generic.
So this is what the query returns:
Name ProductType1 ProductType2 ProductType3
--------------------------------------------------
Marc PT09 P15 PT33
And this is what it should be:
Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33
This is the query which I have simplified a bit:
SELECT
CustomerData.Name as Name
Product1.productType as ProductType1,
Product2.productType as ProductType2,
Product3.productType as ProductType3
FROM
(SELECT ProductID, Name
FROM
Customer
Orders
WHERE Customer.ID = 111
) as CustomerData
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'A'
AND CP.ProductCategoryID = PC.ID
) as Product1
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'B'
AND CP.ProductCategoryID = PC.ID
) as Product2
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'C'
AND CP.ProductCategoryID = PC.ID
) as Product3
on CustomerData.ProductID = Product1.ProductID
So I have been thinking about splitting the joins into a separate stored proc and then call this as I need more productTypes but I can't seem to get this working. Anyone an idea on how to get this working ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在列中做事实际上通常要困难得多。
假设规范化表“客户”、“产品”和“订单”,您不需要做任何其他事情,而只是:
如果这不起作用,请列出相关表的结构。
Doing things in columns is actually usually much more difficult.
Assuming normalized tables Customers, Products and Orders, you shouldn't need to do anything more than just:
If this doesn't work, please list structures of the involved tables.
我假设您的表看起来像这样
Customer
Products
Orders
那么您的查询是
I'm going to assume your tables looks something like this
Customer
Products
Orders
Then your query is