0

Please see table below:

For each CustomerID + PaymentID + ClaimNum, there are 3 NULL values for the ItemID & Qty.

I want to take these 3 NULL values and put them as COLUMNS.

DeductionID 1085 = Service Fee (Column Name)

DeductionID 486 = Freight Charge (Column Name)

DeductionID 559 = Handling Charge (Column Name)

For example, let's take the ClaimNum for the highlighted NULLs:

Doing what I want to do, it would end up with the following columns:

CustomerID, PaymentID, ClaimNum, ItemID, Qty, Amount, Service Fee, Freight Charge, Handling Charge

I'm not sure if I need to do a CROSS JOIN, PIVOT or UNPIVOT here. I've never done this in the past and reading Microsoft's website on this isn't really helping. Can anyone give me a hand?

Let me know if you need more info. I appreciate all the help!!

1
  • Can you show us an example of your desired result? Commented Mar 4, 2014 at 16:05

1 Answer 1

1

You can pivot, then join back to your original data.

ie:

select 
    yourtable.* , Service, handling, freight
from yourtable
    inner join 
(
    select CustomerID, PaymentID, ClaimNum, 
            [1085] as Service, [486] as freight, [559] as handling
    from 
            (select customerid, paymentid, claimnum, DeductionID, Amount from yourtable) t
                pivot 
        (sum (amount) for DeductionID in ([1085],[486],[559]))p
        ) pt 
    on yourtable.CustomerID = pt.CustomerID
    and yourtable.PaymentID = pt.PaymentID
    and yourtable.ClaimNum = pt.ClaimNum
where ItemID is not null
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.