2010-01-28 115 views
0

上週Damir Sudarevic在此社區幫助查詢此查詢以生成seq編號。 我有一個與這個問題的幾個問題。由於某些原因,OrderDetailsID不會顯示記錄,儘管按順序指定它。如下圖所示SQL Server序列號問題

Seq SeqNO 
1A 1 
2A 2 
2B 2A 
2C 2B 
3A 3 
3B 3A 
3C 3B 

相反,我怎麼得到它

在下面序列和SEQNO顯示記錄的查詢中的兩列如下圖所示

SeqNo 
1 
2A 
2B 
2C 
3A 
3B 
3C 

WITH OrderDetails 
     AS (SELECT prodcode 
        ,prodDesc 
        ,orderID 
        ,OrderDetailID 
        ,DENSE_RANK() OVER (ORDER BY prodCode) AS [RnkSeq] 
        ,ROW_NUMBER() OVER (PARTITION BY prodCode ORDER BY OrderDetailID) AS [NumSeq] 
      FROM OrderDetails where orderID=65303 
      ) 
    SELECT OrderDetailID 
     ,prodcode 
     ,CAST(RnkSeq AS varchar(10)) + CHAR(64 + NumSeq) as Seq 
     ,Replace(CAST(RnkSeq AS varchar(10)) + CHAR(63 + NumSeq),'@','') AS SeqNo 
     ,orderID 
    FROM OrderDetails 

回答

0

我沒有看到ORDER BY

+0

Lucero,它不存在於我發佈的查詢中,但是,即使我添加它也不顯示OrderDetailsID的順序 – acadia 2010-01-28 15:21:16

0

末添加這個,以後FROM聲明:

ORDER BY SeqNo 

希望這有助於。