2013-10-30 16 views
1

第一次海報在這裏...去容易對我=)SQL:如何:基於文件的順序場的變化值

基本上我所試圖做的是改變的記錄[attachlvl]標記爲「附件」的所有記錄在標記爲「電子郵件」的任何記錄之後在[xMedia]列中標記爲「附件」爲'1'。

然後棘手的部分是將[ATTACHPID]更改爲附件上方的電子郵件以及其下的任何標記爲「附件」的唯一編號,我還想填寫電子郵件的[附件]字段它下面的附件的文件名由分號分隔。

這裏是我的表是什麼樣子:

[docorder] [docid] [filename] [attachpid] [attachlvl] [attach] [xmedia] 

1 | SAM003266 | SAM003266^eMailContent.htm | 0 | 0 | NULL | eMail 
2 | SAM003268 | SAM003268^eMailContent.htm | 0 | 0 | NULL | eMail 
3 | SAM003269 | SAM003269^THEROCKQ12013.pdf | 0 | 0 | NULL | Attachment 
4 | SAM003569 | SAM003269^THEROCKQ12014.pdf | 0 | 0 | NULL | Attachment 
5 | SAM003270 | SAM003270^eMailContent.htm | 0 | 0 | NULL | eMail 
6 | SAM003273 | SAM003273^eMailContent.htm | 0 | 0 | NULL | eMail 
7 | SAM003275 | SAM003275^eMailContent.htm | 0 | 0 | NULL | eMail 
8 | SAM003276 | SAM003276^[email protected]_20130109_093821.pdf | 0 | 0 | NULL | Attachment 
9 | SAM004269 | SAM003269^THEROCKQ12013.pdf | 0 | 0 | NULL | Attachment 

這是我想最後的結果看起來像:

[docorder] [docid] [filename] [attachpid] [attachlvl] [attach] [xmedia] 
1 | SAM003266 | SAM003266^eMailContent.htm | 0 | 0 | NULL | eMail 
2 | SAM003268 | SAM003268^eMailContent.htm | 1234567 | 0 | SAM003269^THEROCKQ12013.pdf ; SAM003269^THEROCKQ12014.pdf | eMail 
3 | SAM003269 | SAM003269^THEROCKQ12013.pdf | 1234567 | 1 | NULL | Attachment 
4 | SAM003569 | SAM003269^THEROCKQ12014.pdf | 1234567 | 1 | NULL | Attachment 
5 | SAM003270 | SAM003270^eMailContent.htm | 0 | 0 | NULL | eMail 
6 | SAM003273 | SAM003273^eMailContent.htm | 0 | 0 | NULL | eMail 
7 | SAM003275 | SAM003275^eMailContent.htm | 1234568 | 0 | SAM003276^[email protected]_20130109_093821.pdf ; SAM003269^THEROCKQ12013.pdf | eMail 
8 | SAM003276 | SAM003276^[email protected]_20130109_093821.pdf | 1234568 | 1 | NULL | Attachment 
9 | SAM004269 | SAM003269^THEROCKQ12013.pdf | 1234568 | 1 | NULL | Attachment 
+4

你嘗試什麼親愛的? – Kaf

+0

我唯一能做的就是設置attachlvl ='1',其中xMedia ='attachment',我不知道如何修改上面的記錄的字段,這是我的問題=) – lyosha

+2

研究窗口化分析函數['LAG '和'LEAD'](http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012 /)。 –

回答

2

,如果你歸它會更容易這就是下面的查詢所做的一個公平的分析。

DECLARE @t TABLE 
(
    [docorder] INT, 
    [docid] VARCHAR(20), 
    [filename] VARCHAR(100), 
    [attachpid] INT, 
    [attachlvl] INT, 
    [attach] VARCHAR(MAX), 
    [xmedia] VARCHAR(20) 
) 

INSERT INTO @t ([docorder],[docid],[filename],[attachpid],[attachlvl],[attach],[xmedia]) 
VALUES 
    (1 , 'SAM003266' , 'SAM003266^eMailContent.htm' , 0 , 0 , NULL , 'eMail'), 
    (2 , 'SAM003268' , 'SAM003268^eMailContent.htm' , 0 , 0 , NULL , 'eMail'), 
    (3 , 'SAM003269' , 'SAM003269^THEROCKQ12013.pdf' , 0 , 0 , NULL , 'Attachment'), 
    (4 , 'SAM003569' , 'SAM003269^THEROCKQ12014.pdf' , 0 , 0 , NULL , 'Attachment'), 
    (5 , 'SAM003270' , 'SAM003270^eMailContent.htm' , 0 , 0 , NULL , 'eMail'), 
    (6 , 'SAM003273' , 'SAM003273^eMailContent.htm' , 0 , 0 , NULL , 'eMail'), 
    (7 , 'SAM003275' , 'SAM003275^eMailContent.htm' , 0 , 0 , NULL , 'eMail'), 
    (8 , 'SAM003276' , 'SAM003276^[email protected]_20130109_093821.pdf' , 0 , 0 , NULL , 'Attachment'), 
    (9 , 'SAM004269' , 'SAM003269^THEROCKQ12013.pdf' , 0 , 0 , NULL , 'Attachment') 

;WITH emails AS 
(
    SELECT 
     emails.docorder, 
     emails.docid, 
     emails.[filename], 
     CHECKSUM(emails.[docid]) emailpid, 
     attachpid, 
     emails.attachlvl, 
     emails.[attach], 
     emails.xmedia 
    FROM @t emails WHERE emails.xmedia='email' 
) 
, attachments AS 
(
    SELECT * 
    FROM (
     SELECT 
      attachments.docorder, 
      attachments.docid, 
      attachments.[filename], 
      CHECKSUM(emails.[docid]) emailpid, 
      1 attachlvl, 
      attachments.[attach], 
      attachments.xmedia, 
      DENSE_RANK() OVER (PARTITION BY attachments.docid ORDER BY emails.docorder DESC) dr 
     FROM emails 
     JOIN (
      SELECT * FROM @t WHERE xmedia='attachment' 
     ) attachments ON attachments.docorder > emails.docorder 
    ) t 
    WHERE dr=1 
) 
, grouped_attachments AS 
(
    SELECT emailpid, LEFT(filenames , LEN(filenames)-1) filenames 
    FROM attachments AS extern 
    CROSS APPLY 
    (
     SELECT [filename] + ';' 
     FROM attachments AS intern 
     WHERE extern.emailpid = intern.emailpid 
     FOR XML PATH('') 
    ) pre_trimmed (filenames) 
    GROUP BY emailpid, filenames 
) 
SELECT 
    emails.docorder, 
    emails.docid, 
    emails.[filename], 
    COALESCE(grouped_attachments.emailpid, emails.attachpid) attachpid, 
    emails.attachlvl, 
    grouped_attachments.filenames [attach], 
    emails.xmedia 
FROM emails 
LEFT JOIN grouped_attachments ON grouped_attachments.emailpid = emails.emailpid 
UNION ALL 
SELECT 
    attachments.docorder, 
    attachments.docid, 
    attachments.[filename], 
    grouped_attachments.emailpid attachpid, 
    attachments.attachlvl, 
    attachments.[attach], 
    attachments.xmedia 
FROM attachments 
LEFT JOIN grouped_attachments ON grouped_attachments.emailpid = attachments.emailpid 
ORDER BY docorder 

sqlfiddle