我正在使用volusion cart软件,因此我只有导出访问权限。我有以下表格(带有一些示例数据):
orders
orderid, shippingmethodid, orderstatus
12345, 218, Ready to Ship
12346, 152, Ready to Ship
12347, 602, Ready to Ship
12348, 10151, Processing
orderdetails
orderid, productcode, productname, qtyonpackingslip, qty
12345, proda-12, product a twelve, 1, 1
12346, prodb-14, product b fourteen, , 1
12346, prodc_15, product c fifteen, 0, 1
12347, prodd-21, product d twenty one, , 1
12347, prode-17, product e seventeen, , 1
12348, prodf-19, product f nineteen, , 1
我需要创建一个导出查询,将产品代码列为列而不是行。意思是如果订单上有多个产品代码,那个orderid应该有1行,&然而,许多列,因为有顺序的产品,但只有当qtyonpackingslip是> 0或null且数量> 0.结果需要看的示例:
应该有10个项目列。我很犹豫发布我尝试过的内容,部分来自于尴尬和尴尬。部分是因为它是如此错误,我可能会对我正在尝试做的事情造成混淆,但这里有一些只是让你们都知道我真的在尝试:
SELECT
o.orderid
, (SELECT productcode, row_number() over (order by productcode) as item1pc
FROM orderdetails od WHERE item1pcv= 1) As Item1code
, (SELECT productname, row_number() over (order by productname) as item1name
FROM orderdetails od WHERE item1name = 1) As Item1name
, (SELECT qtyonpackingslip row_number() over (order by qtyonpackingslip) as item1qty
FROM orderdetails od WHERE item1qty= 1) As Item1qty
, shippingmethodid
FROM orders o
WHERE EXISTS (SELECT productcode, productname, qtyonpackingslip
FROM orderdetails od
LEFT JOIN orders on o.orderid = od.orderid
WHERE od.qtyonpackingslip > 0)
WHERE o.orderstatus = 'Ready to Ship'
甚至没有关闭,也试过这个:
SELECT
o.orderid
, pd1.productcode As item1
, pd1.productname As item1name
, pd1.quantity As item1qty
, o.shippingmethodid
FROM orders o
LEFT JOIN (SELECT orderid, productcode, productname, qtyonpackingslip, quantity FROM
(SELECT ROW_NUMBER() OVER (ORDER BY orderid) AS Row
, orderid
, productcode
, productname
, quantity
FROM orderdetails WHERE qtyonpackingslip > 0
OR (qtyonpackingslip IS NULL AND quantity > 0)
) As pd1
ON o.orderid = pd1.orderid AND Row = 1
WHERE o.orderstatus = 'Ready to Ship'
再次,没有骰子。我以前从来没有得过具体的行,所以我不知道我在做什么!非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
您需要使用数据透视表。请参阅静态列的示例。您还可以进行动态列透视。
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [250], [251], [256], [257], [260] )
) AS pvt
ORDER BY pvt.VendorID;
答案 1 :(得分:0)
正如我们所讨论的,你真正想要的不是PIVOT数据。透视将数据集中的单元格中的值转换为列,在您的情况下,您仍然希望将这些值作为值。不同之处在于,您希望将与同一订单关联的值作为单行返回,将列附加到结果集中,以获取订单所具有的任何数量的产品(及其详细信息)。
以下是我们提出的解决方案:
{
"line_numbers": true,
"gutter": true,
"column_number": true,
}