简体   繁体   中英

Pivoting a SQL Server table

Sample data from a SQL Server table is shown below:

+------------+---------+---------+-----+-------+--------+--------+
|    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. 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.

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.

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.

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.

;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:

产量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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