用于将单个数据行导出到多个CSV行的SQL查询

时间:2013-02-06 17:45:32

标签: sql sql-server-2008-r2 export-to-csv

我们正在尝试自动化一些步骤,我需要设置一个查询来按计划转储数据。我遇到的问题是从SQL表中获取单行数据并将其转储到多行数据。 SQL表用于零件订单表格,并且包含最多10个零件的输入(Part01 - Part10)和每个零件数量的字段(Qty01-Qty10)。与大多数订单一样,我们并未在每个订单上使用所有10行,因此在导出时,我们还需要测试零件字段(PartXX<>'')中是否有数据仅生成实际行的行有数据。表中还有一些字段需要在每个输出行中填充,即使它没有更改。对第一个“行”的查询是直截了当的,但我的障碍是后续行的“IF / THEN”部分并为

生成“行”

我正在寻找的输出是这样的:

Ticket# CustID  Account  Line  ShipAttn  ShipAdd  ShipCity ShipState  ShipZip  Part Qty
123456  Cust01  987465   1     Joe Smith Address  AnyTown  IL         01234    Key  2
123456  Cust01  987456   2     Joe Smith Address  AnyTown  IL         01234    Lock 2

任何方向都表示赞赏。

3 个答案:

答案 0 :(得分:0)

不知道您的确切架构,您可能会尝试这样的东西来取消您的记录:

;with cte as (
    -- Select for each Part/Qty:
    select Ticket#, CustID, Account, ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part01 as Part, Qty01 as Qty, 1 as Line
    from Orders
    where Part01 is not null and Qty01 is not null
    union all
    select Ticket#, CustID, Account, ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part02 as Part, Qty02 as Qty, 2 as Line -- 02!
    from Orders
    where Part02 is not null and Qty02 is not null -- 02!
    union all
    select Ticket#, CustID, Account, ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part03 as Part, Qty03 as Qty, 3 as Line -- 03!
    from Orders
    where Part03 is not null and Qty03 is not null -- 03!
    --union all...keep going with 04 - 10
)
select Ticket#, CustID, Account
    , row_number() over (partition by Ticket# order by Line) as Line
    , ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part, Qty
from cte
order by Ticket#, Line

答案 1 :(得分:0)

您要查找的是 pivot 表(或“交叉表”)。每个RDBMS都有自己的解决方案,你没有提供你的解决方案。确切的语法还取决于您的RDBMS。

标准SQL 查询可能如下所示:

SELECT Ticket#, CustID, Account, 1 AS Line, ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part01 AS Part, Qty01 AS Qty
FROM   tbl
WHERE  Part01 IS NOT NULL

UNION ALL
SELECT Ticket#, CustID, Account, 2 AS Line, ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part02 AS Part, Qty02 AS Qty
FROM   tbl
WHERE  Part02 IS NOT NULL

UNION ALL ...

ORDER BY Ticket#, CustID, Account, Line

ORDER BY应用于完整结果,而不仅仅是查询的最后一段。因此,您不需要子选择或CTE。

答案 2 :(得分:0)

上述解决方案都需要Union,因为您正在使用SQL Server 2008 R2,您可能希望使用UnPivot的内置功能......它仍未经过测试,但它应该可以正常工作。

它基于所描述的解决方案:http://mangalpardeshi.blogspot.com/2009/04/unpivot-multiple-columns.html

SELECT Ticket#, CustID, Account
    , row_number() over (partition by Ticket# order by Parts) as Line
    , ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip, Part, Qty 
FROM 
( 
SELECT Ticket#, CustID, Account , ShipAttn, ShipAdd, ShipCity, ShipState, ShipZip,
       Part01, Part02, Part03, Part04, Part05, Part06, Part07, Part08, Part09, Part10,
       Qty01, Qty02, Qty03, Qty04, Qty05, Qty06, Qty07, Qty08, Qty09, Qty10
FROM Suppliers 
) Main
UNPIVOT 
( 
Part FOR Parts IN (Part01, Part02, Part03, Part04, Part05, Part06, Part07, Part08, Part09, Part10) 
) P


UNPIVOT 
( 
Qty For Qtys IN (Qty01, Qty02, Qty03, Qty04, Qty05, Qty06, Qty07, Qty08, Qty09, Qty10) 
) Q

WHERE RIGHT(Parts,2) =  RIGHT(Qtys,2)