简体   繁体   English

透视SQL Server表

[英]Pivoting a SQL Server table

Sample data from a SQL Server table is shown below: SQL Server表中的示例数据如下所示:

+------------+---------+---------+-----+-------+--------+--------+
|    Date    | Account |  Head   | D_C | TxnID | AmCode | Amount |
+------------+---------+---------+-----+-------+--------+--------+
| 15-05-2019 |     123 | Savings | D   |     5 | SV     |    150 |
| 15-05-2019 |     123 | Cash    | C   |     5 | SV     |    150 |
| 16-05-2019 |     367 | Loan    | D   |     6 | LN     |    200 |
| 16-05-2019 |     367 | Cash    | C   |     6 | LN     |    200 |
+------------+---------+---------+-----+-------+--------+--------+

There are two pairs (4 rows) shown above. 上面显示了两对(4行)。 All the fields in each pair are the same (even when the data is unsorted - although it is sorted above) except for the Head and D_C Column which are different for the pair. 每对中的所有字段都是相同的(即使数据未排序 - 尽管它在上面排序),除了HeadD_C Column ,对于该对不同。

I am looking for a report as below from the above sample: 我正在从上面的示例中寻找以下报告:

+------------+---------+---------+--------+-------+--------+--------+
|    Date    | Account | D Head  | C Head | TxnID | AmCode | Amount |
+------------+---------+---------+--------+-------+--------+--------+
| 15-05-2019 |     123 | Savings | Cash   |     5 | SV     |    150 |
| 16-05-2019 |     367 | Loan    | Cash   |     6 | LN     |    200 |
+------------+---------+---------+--------+-------+--------+--------+

That is one row in the report for the pair with the Head + D combination in one column and Head + C combination in another. 这是报告中的一行,其中Head + D组合在一列中,Head + C组合在另一列中。

Please note that in the actual data the pairs may not appear together as above. 请注意,在实际数据中,对可能不会像上面那样一起出现。 However, the concatenation of fields other than Head + D_C makes the concatenated string unique for a pair of rows. 但是,除Head + D_C之外的字段串联使得连接字符串对于一对行是唯一的。

Thanks in advance for the help. 在此先感谢您的帮助。

Try with conditional aggregation: 尝试使用条件聚合:

declare @tmp table
(
     [Date]  date,  
     Account int,   
     Head    varchar(50),  
     D_C     char(1),  
     TxnID   int,  
     AmCode  char(2),  
     Amount  int
)

insert into @tmp
values
 ('2019-05-15', 123, 'Savings', 'D', 5, 'SV', 150) 
,('2019-05-15', 123, 'Cash'   , 'C', 5, 'SV', 150) 
,('2019-05-16', 367, 'Loan'   , 'D', 6, 'LN', 200) 
,('2019-05-16', 367, 'Cash'   , 'C', 6, 'LN', 200) 

select  
  [Date]
, Account
, max(case when D_C='D' then Head end )as [D Head]
, max(case when D_C='C' then Head end )as [C Head]
, TxnID
, AmCode
, Amount 
From @tmp
group by [Date], Account, TxnID, AmCode, Amount

Results: 结果:

在此输入图像描述

If I'm understanding this correctly, you want to merge debit and credit pairs which have the same: 如果我正确理解这一点,您希望合并具有相同的借方和贷方对:

  • Date
  • Account
  • TxnID
  • AmCode
  • Amount

While still retaining both Head descriptions for the debits and credits. 虽然仍然保留了借方和贷方的两个Head描述。

;with Debit --Define Debit CTEas only debit lines
as
(
    SELECT *
    FROM datatable
    WHERE D_C='D'
),
Credit  --Define Credit CTE as only debit lines
as
(
    SELECT *
    FROM datatable
    WHERE D_C='C'
)
SELECT DISTINCT --Get only unique lines
    d.[Date],
    d.[Account],
    d.Head as [D Head],
    c.Head as [C Head],
    d.TxnID,
    d.AmCode,
    d.Amount
From [Debit] d
INNER JOIN --Join on all the same fields
    [Credit] c
ON 
    d.Date = c.Date
AND 
    d.Account = c.Account
AND 
    d.TxnID = c.TxnID
AND 
    d.AMCode = c.AmCode
AND 
    c.Amount = d.Amount

This code above is based on the fact that you will only ever have one debit and one credit line per pair. 上面的代码基于这样一个事实,即每对只有一个借方和一个贷方。 If this is not the case, the code will need to be modified. 如果不是这种情况,则需要修改代码。

Output: 输出:

产量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM