• Lokesh Vij (2/4/2013)


    Here you go:

    CREATE TABLE Payment

    (

    PaymentHistoryID INT,

    paymentID INT,

    Amount DECIMAL(10, 2),

    receivedType CHAR(2),

    Reason VARCHAR(10)

    );

    INSERT INTO Payment

    VALUES

    (1,11, 5000.00,'Dr','ASD'),

    (2,12, 4000.00,'Cr', 'BG'),

    (3,11, 8000.00,'Cr', 'BG'),

    (4,11, 3000.00,'Dr', 'SD'),

    (5,11, 3000.00,'Cr','ASD'),

    (6,13,50000.00,'Cr','ASD'),

    (7,13,50000.00,'Cr','ASD'),

    (8,13, 1000.00,'Cr','ASD'),

    (9,13, 500.00,'Cr','ASD')

    SELECT paymentID,

    Sum(CASE receivedType

    WHEN 'Dr' THEN Amount

    ELSE -1 * Amount

    END) Amount,

    Reason

    FROM Payment

    GROUP BY paymentID,

    Reason

    demonfox (2/4/2013)


    Please provide the ddl ..

    follow this link for best practices to post :

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Always provide DDL along with test data and expected result, as mentioned by demonfox...

    And the best part ; Float hasa higher precedence over int , so multiplying Amount with -1 doesn't convert the data type of amount ...

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm: