2016-08-04 171 views
1

我在根據列值創建其他行時遇到問題。根據列值添加行

如果我的PageCount = 3,那麼我需要有2個額外的行,其中PONo被重複,但ImagePath爲每個新行增加1。

我能夠獲得第一行,但創建ImagePath增加1的附加行是我卡住的地方。

我的結果:
enter image description here

預期結果:
enter image description here

成品:finished values

當前Select語句:

 SELECT PO, CASE WHEN LEFT(u.Path,3)= 'M:\' THEN '\\ServerName\'+RIGHT(u.Path,LEN(u.Path)-3) ELSE u.Path END AS [Imagepath],PAGECOUNT 
FROM OPENQUERY([LinkedServer],'select * from data.vw_purchasing_docs_unc') AS u INNER JOIN 
OPENQUERY([LinkedServer],'select * from data.purchasing_docs') AS d ON u.docid=d.docid 
WHERE (CONVERT(VARCHAR(10),d.STATUS_DATE,120)=CONVERT(VARCHAR(10),GETDATE(),120)) 

批處理文件:

bcp "select d.DOCID,DOC_TYPE,PO,d.STATUS, CASE WHEN LEFT(Path,3)= 'M:\' THEN '\\ServerName'+RIGHT(DWPath,LEN(Path)-3) ELSE Path END AS ImagePath, STATUS_DATE,'No' AS dwimport from openquery([LinkedServer],'select * from data.vw_purchasing_docs_unc') as u INNER JOIN openquery([LinkedServer],'select * from dwdata.purchasing_docs') as d ON u.docid=d.docid WHERE (CONVERT(varchar(10),STATUS_DATE,120)=CONVERT(varchar(10),GETDATE(),120)) AND d.STATUS IN ('FILED - Processing Complete','FILED - Partial Payment','FILED - Confirming') AND DOC_TYPE IN ('CO = Change Order','Purchase Order','CP = Capital Projects','Change Order','PO = Purchase Order','PO','PR = General Operating')" queryout "E:\Data\PO Trigger CSV\PO_Trigger_Doc.csv" -r \n -T -c -t"," -Umv -Smtvwrtst -Pm -q -k 

回答

3
Select ponumber,b.rplc,pagecount 
from table t 
cross apply 
(select replace(imagepath,'f'+cast(n-1) as varchar(100),'f0') as rplc from numbers n where n<=t.pagecount)b 

要創建一個數字表,如果你想知道爲什麼你需要it.Look here

CREATE TABLE Number (N INT IDENTITY(1,1) PRIMARY KEY NOT NULL); 
GO 

INSERT INTO Number DEFAULT VALUES; 
GO 10000 

使用SELECT語句更新後:

;With cte(ponumber,imagepath,pagecount) 
as 
SELECT PO, CASE WHEN LEFT(u.Path,3)= 'M:\' THEN '\\ServerName\'+RIGHT(u.Path,LEN(u.Path)-3) ELSE u.Path END AS [Imagepath],PAGECOUNT 
FROM OPENQUERY([LinkedServer],'select * from data.vw_purchasing_docs_unc') AS u INNER JOIN 
OPENQUERY([LinkedServer],'select * from data.purchasing_docs') AS d ON u.docid=d.docid 
WHERE (CONVERT(VARCHAR(10),d.STATUS_DATE,120)=CONVERT(VARCHAR(10),GETDATE(),120)) 
) 
select Ponumber,b.rplc,pagecount from cte c 
cross apply 
(select replace(imagepath,'f'+cast((n-1) as varchar(100)),'f0') as rplc from numbers n where n<=c.pagecount)b 
+1

@downvoters:Pease發表評論當你downvote – TheGameiswar

+0

不知道爲什麼這得到了downvote,但這是SO的本質。許多人會冷靜下來,不說話。數字或理貨表是正確的做法。 –

+0

謝謝@SeanLange – TheGameiswar

0

如果您想要avoi d附加表格,您可以使用CTE:

WITH Images AS 
(
    SELECT * FROM (VALUES 
    ('C:\Folder', 2), 
    ('D:\Folder', 3)) T(ImagePath, Val) 
), Numbers AS 
(
    SELECT * FROM (VALUES (1),(2),(3),(4)) T(N) 
    UNION ALL 
    SELECT N1.N*4+T.N N FROM (VALUES(1),(2),(3),(4)) T(N) CROSS JOIN Numbers N1 
    WHERE N1.N*4+T.N<=100 
) 
SELECT ImagePath + '\f' + CONVERT(nvarchar(10) ,ROW_NUMBER() OVER (PARTITION BY ImagePath ORDER BY (SELECT 1))) NewPath 
FROM Images 
CROSS APPLY (SELECT TOP(Val) * FROM Numbers) T(N) 

圖片是您的源表格。它可以是任何東西,即OPENQUERY。它產生:

NewPath 
------- 
C:\Folder\f1 
C:\Folder\f2 
D:\Folder\f1 
D:\Folder\f2 
D:\Folder\f3