简体   繁体   中英

SQL Return different values on the same row, based on a value thats in the same table

I'm having a very difficult time trying to get my result to display they i need them to display... here and example of what my query looks like and below will be the result i need...
iv been at this for a long time trying case statements and joins but have had no luck, if anybody can help i would really appreciate it.

table name dbo.DTLPAYMENTS 

columns   
PTNO    
CD   
AMT   
DESCRIPTION

my query displays my result back to me like so...also there never an equal amount line for credits and debits. so a person can have more debits(>0) than credits(<0) and vice versa....

PTNO       /  CD    / AMT   / DESCRIPTION    
10007558931 30073   688.82    PAYMENT-ME    
10007558931 30073   -704.44   PAYMENT-ME    
10007558931 30073   704.44    PAYMENT-ME    
10007558931 30073   -688.82   PAYMENT-ME    
10007558931 30073   -698.82   PAYMENT-ME

i need this the debit and credit in separate columns
if there any way possible i can have it result back to me like so...

PTNO /      CD /    AMT /   DESCRIPTION / CD /     AMT / DESCRIPTION    
10007558931 30073   688.82  PAYMENT-ME    30073   -688.82  PAYMENT-ME   
10007558931 30073   704.44  PAYMENT-ME    30073   -698.82  PAYMENT-ME   
10007558931                               30073   -704.44  PAYMENT-ME      

and thanks you if anyone can help me

First, use CTEs to separate Credits and Debits into separate data sets, then FULL JOIN them based on PTNO, CD, and inverse value (* -1) of AMT.

Edit: Since joining based on AMT is not a goal, I used BatchSeqID as a way to sort the data chronologically. Using a ROW_NUMBER() to order the two data sets, I then joined then on this value.

;WITH
Credit AS
(   SELECT PTNO,CD,AMT,DESCRIPTION,
    ROW_NUMBER()OVER(PARTITION BY PTNO,CD ORDER BY BatchSeqID) AS Sort
    FROM dbo.DTLPAYMENTS
    WHERE AMT < 0 ),
Debit AS
(   SELECT PTNO,CD,AMT,DESCRIPTION,
    ROW_NUMBER()OVER(PARTITION BY PTNO,CD ORDER BY BatchSeqID) AS Sort
    FROM dbo.DTLPAYMENTS
    WHERE AMT > 0 )

SELECT  ISNULL(c.PTNO,d.PTNO) AS PTNO,
        ISNULL(c.CD,d.CD) AS CD,
        --Credit data
        c.AMT AS CRAMT,
        c.DESCRIPTION AS CRDESCRIPTION,
        --Debit data
        d.AMT AS DBTAMT,
        d.DESCRIPTION AS DBTDESCRIPTION
FROM Credit c
FULL JOIN Debit d 
    ON d.PTNO = c.PTNO
    AND d.CD = c.CD
    AND d.Sort= c.Sort

if you don't like Matt's solution and the risk of duplicated rows, you can have every transaction on a different row:

SELECT PTNO,
       CASE WHEN AMT < 0 THEN cd ELSE null END DBTCD,
       CASE WHEN AMT < 0 THEN amt ELSE null END AMTCD,
       CASE WHEN AMT < 0 THEN DESCRIPTION ELSE null END DBTDESCRIPTION,
       CASE WHEN AMT > 0 THEN cd ELSE null END CRCD,
       CASE WHEN AMT > 0 THEN amt ELSE null END CRAMT,
       CASE WHEN AMT > 0 THEN DESCRIPTION ELSE null END CRDESCRIPTION
FROM TABLE1
order by PTNO, AMT

Here you can see and play wiht the result.

 PTNO           DBTCD   AMTCD   DBTDESCRIPTION  CRCD    CRAMT   CRDESCRIPTION
 10007558931    30073   -704    PAYMENT-ME      (null)  (null)  (null)
 10007558931    30073   -699    PAYMENT-ME      (null)  (null)  (null)
 10007558931    30073   -689    PAYMENT-ME      (null)  (null)  (null)
 10007558931    (null)  (null)  (null)          30073   689     PAYMENT-ME
 10007558931    (null)  (null)  (null)          30073   704     PAYMENT-ME

Assuming you want to sort the output on the magnitude of the amt column, try:

select ptno,
       max(case sign(amt) when 1 then cd end) dr_cd,
       max(case sign(amt) when 1 then amt end) dr_amt,
       max(case sign(amt) when 1 then description end) dr_desc,
       max(case sign(amt) when -1 then cd end) cr_cd,
       max(case sign(amt) when -1 then amt end) cr_amt,
       max(case sign(amt) when -1 then description end) cr_desc
from (select d.*, 
             row_number() over (partition by ptno, cd, sign(amt) 
                                order by abs(amt)) rn
      from DTLPAYMENTS d) sq
group by ptno, cd, rn
order by ptno, cd, rn

SQLFiddle here .

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