如何在T-SQL查询输出中以交替顺序排列行组

时间:2018-07-04 16:19:35

标签: sql-server tsql

我需要一些人让我知道如何排列这些行。

我希望各行遵循以下模式:

  1. TRNS
  2. SPL
  3. ENDTRNS

这就是我所拥有的

+-------+------------+----------+------------+--------+
|  Id   | Trans Type | Document |    Date    | Amount |
+-------+------------+----------+------------+--------+
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -745   |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -800   |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -860   |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -1170  |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 745    |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 800    |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 860    |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 1170   |
+-------+------------+----------+------------+--------+

我正在尝试获得以下最终结果

+-------+------------+----------+------------+--------+
|  Id   | Trans Type | Document |    Date    | Amount |
+-------+------------+----------+------------+--------+
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 1170   |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -1170  |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 860    |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -860   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 800    |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -800   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
| 14184 | TRNS       | INVOICE  | 01/07/1990 | 745    |
| 14184 | SPL        | INVOICE  | 01/07/1990 | -745   |
| 14184 | ENDTRNS    | NULL     | NULL       | NULL   |
+-------+------------+----------+------------+--------+

我想很近,这就是我正在努力的事情

;WITH CTE_Sample AS 
(
    SELECT 
        '14184' AS [Id],
        'ENDTRNS' AS [Trans Type],
        'NULL' AS [Document],
        'NULL' AS [Date],
        'NULL' AS [Amount]
    UNION  ALL
    SELECT 
        '14184' AS [Id],
        'ENDTRNS' AS [Trans Type],
        'NULL' AS [Document],
        'NULL' AS [Date],
        'NULL' AS [Amount]
    UNION  ALL
    SELECT 
        '14184' AS [Id],
        'ENDTRNS' AS [Trans Type],
        'NULL' AS [Document],
        'NULL' AS [Date], 
        'NULL' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'ENDTRNS' AS [Trans Type],'NULL' AS [Document],'NULL' AS [Date],'NULL' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'SPL' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'-745' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'SPL' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'-800' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'SPL' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'-860' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'SPL' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'-1170' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'TRNS' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'745' AS [Amount]
UNION  ALL
SELECT '14184' AS [Id],'TRNS' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'800' AS [Amount]
UNION ALL
SELECT '14184' AS [Id],'TRNS' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'860' AS [Amount]
UNION ALL
SELECT '14184' AS [Id],'TRNS' AS [Trans Type],'INVOICE' AS [Document],'01/07/1990' AS [Date],'1170' AS [Amount]
)
SELECT 
    ID, [Trans Type], Document, [Date], Amount,
    ROW_NUMBER() OVER (PARTITION BY ID, [Trans Type] ORDER BY Amount ASC) AS rownum
FROM 
    CTE_Sample
ORDER BY 
    ROWNUM

我不知道如何从上到下在每行行的末尾得到ENDTRNS

4 个答案:

答案 0 :(得分:0)

我想您希望通过交易获得一个ID:

SELECT ID,[Trans Type],Document,[Date],Amount 
FROM CTE_Sample
ORDER BY ID_BY_TRANSACTION, CASE [Trans Type] 
                             WHEN 'TRNS' THEN 1 
                             WHEN 'SPL' THEN 2
                             WHEN 'ENDTRNS' THEN 3 END

您的系统中某处必须有一个交易ID。

答案 1 :(得分:0)

您有金额作为文本(varchar)。搞砸了

SELECT ID, [Trans Type], Document,[Date], Amount 
  , ROW_NUMBER() OVER (PARTITION BY ID, [Trans Type] 
                       ORDER BY abs(isnull(Amount, '0')) desc ) AS rownum
FROM CTE_Sample
ORDER BY ROWNUM, [Trans Type] desc 

答案 2 :(得分:0)

尝试一下

#index
|__<body data-spy="scroll" data-target=".navbar" data-offset="100">
      |__<nav ...>...</nav>
      |__<div id="myDiv" ...>...</div>

#second_page
|__<body ...>
      |__<a href="index.html#myDiv">Label</a>

答案 3 :(得分:-1)

我发布的查询是一个较大查询的一部分。对于我使用的这个特定示例,

SELECT ID,[Trans Type],Document,[Date],Amount 
,ROW_NUMBER() OVER (PARTITION BY ID,[Trans Type] ORDER BY Amount  asc ) AS rownum
FROM CTE_Sample
ORDER BY rownum,CASE [Trans Type] 
                             WHEN 'TRNS' THEN 1 
                             WHEN 'SPL' THEN 2
                             WHEN 'ENDTRNS' THEN 3 END

但是我需要对其进行进一步测试,以防止将来失败。